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 139399c87eSsplitbrainuse dokuwiki\Ip; 141cedacf2SAndreas Gohruse dokuwiki\ErrorHandler; 15cf927d07Ssplitbrainuse dokuwiki\JWT; 1673dc0a89SAndreas Gohruse dokuwiki\MailUtils; 1724870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1896348f27SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 1996348f27SAndreas Gohruse dokuwiki\Extension\Event; 2096348f27SAndreas Gohruse dokuwiki\Extension\PluginController; 21c3cc6e05SAndreas Gohruse dokuwiki\PassHash; 2275d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 23927933f5SAndreas Gohruse phpseclib3\Crypt\AES; 24927933f5SAndreas Gohruse phpseclib3\Crypt\Common\SymmetricKey; 251cedacf2SAndreas Gohruse phpseclib3\Exception\BadDecryptionException; 26c3cc6e05SAndreas Gohr 2716905344SAndreas Gohr/** 2816905344SAndreas Gohr * Initialize the auth system. 2916905344SAndreas Gohr * 3016905344SAndreas Gohr * This function is automatically called at the end of init.php 3116905344SAndreas Gohr * 3216905344SAndreas Gohr * This used to be the main() of the auth.php 3316905344SAndreas Gohr * 3416905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 3516905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 36ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 37ab5d26daSAndreas Gohr * @return bool 3816905344SAndreas Gohr */ 39d868eb89SAndreas Gohrfunction auth_setup() 40d868eb89SAndreas Gohr{ 41742c66f8Schris global $conf; 42e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4303c4aec3Schris global $auth; 44bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 45bcc94b2cSAndreas Gohr global $INPUT; 469a9714acSDominik Eckelmann global $AUTH_ACL; 479a9714acSDominik Eckelmann global $lang; 483a7140a1SAndreas Gohr /* @var PluginController $plugin_controller */ 499c29eea5SJan Schumann global $plugin_controller; 5024870174SAndreas Gohr $AUTH_ACL = []; 5103c4aec3Schris 52b9cda918SAndreas Gohr // unset REMOTE_USER if empty 53b9cda918SAndreas Gohr if ($INPUT->server->str('REMOTE_USER') === '') { 54b9cda918SAndreas Gohr $INPUT->server->remove('REMOTE_USER'); 55b9cda918SAndreas Gohr } 56b9cda918SAndreas Gohr 5716905344SAndreas Gohr if (!$conf['useacl']) return false; 5816905344SAndreas Gohr 599c29eea5SJan Schumann // try to load auth backend from plugins 609c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 619c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 62f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 639c29eea5SJan Schumann break; 649c29eea5SJan Schumann } 659c29eea5SJan Schumann } 668b06d178Schris 676547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) { 6871c734a9SGerrit Uitslag msg($lang['authtempfail'], -1); 693094e817SAndreas Gohr return false; 703094e817SAndreas Gohr } 718b06d178Schris 726416b708SMichael Hamann if ($auth->success == false) { 730f4f4adfSAndreas Gohr // degrade to unauthenticated user 74ecad51ddSAndreas Gohr $auth = null; 750f4f4adfSAndreas Gohr auth_logoff(); 76cd52f92dSchris msg($lang['authtempfail'], -1); 776416b708SMichael Hamann return false; 78d2dde4ebSMatthias Grimm } 7916905344SAndreas Gohr 8016905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 81bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 82bcc94b2cSAndreas Gohr if (!$conf['rememberme']) $INPUT->set('r', false); 83bbbd6568SAndreas Gohr 8462bf3ac0SDamien Regad // Populate Basic Auth user/password from Authorization header 8562bf3ac0SDamien Regad // Note: with FastCGI, data is in REDIRECT_HTTP_AUTHORIZATION instead of HTTP_AUTHORIZATION 8662bf3ac0SDamien Regad $header = $INPUT->server->str('HTTP_AUTHORIZATION') ?: $INPUT->server->str('REDIRECT_HTTP_AUTHORIZATION'); 8762bf3ac0SDamien Regad if (preg_match('~^Basic ([a-z\d/+]*={0,2})$~i', $header, $matches)) { 8862bf3ac0SDamien Regad $userpass = explode(':', base64_decode($matches[1])); 8924870174SAndreas Gohr [$_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']] = $userpass; 90528ddc7cSAndreas Gohr } 91528ddc7cSAndreas Gohr 921e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 9303062864SAndreas Gohr if (!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($INPUT->server->str('PHP_AUTH_USER'))) { 9403062864SAndreas Gohr $INPUT->set('u', $INPUT->server->str('PHP_AUTH_USER')); 9503062864SAndreas Gohr $INPUT->set('p', $INPUT->server->str('PHP_AUTH_PW')); 96bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 971e8c9c90SAndreas Gohr } 981e8c9c90SAndreas Gohr 99395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 10093a7873eSAndreas Gohr if (true === $auth->success) { 101395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 102395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 103f4476bd9SJan Schumann } 104191bb90aSAndreas Gohr 105455aa67eSAndreas Gohr if (!auth_tokenlogin()) { 10681e99965SPhy $ok = null; 107455aa67eSAndreas Gohr 1088407f251Ssplitbrain if ($auth->canDo('external')) { 10981e99965SPhy $ok = $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 11081e99965SPhy } 11181e99965SPhy 11281e99965SPhy if ($ok === null) { 11381e99965SPhy // external trust mechanism not in place, or returns no result, 11481e99965SPhy // then attempt auth_login 11524870174SAndreas Gohr $evdata = [ 116bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 117bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 118bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 119bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 12024870174SAndreas Gohr ]; 121cbb44eabSAndreas Gohr Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 122f5cb575dSAndreas Gohr } 123455aa67eSAndreas Gohr } 124f5cb575dSAndreas Gohr 12516905344SAndreas Gohr //load ACL into a global array XXX 12675c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 127ab5d26daSAndreas Gohr 128ab5d26daSAndreas Gohr return true; 12975c93b77SAndreas Gohr} 13075c93b77SAndreas Gohr 13175c93b77SAndreas Gohr/** 13275c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 13375c93b77SAndreas Gohr * 13475c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 13542ea7f44SGerrit Uitslag * 136ab5d26daSAndreas Gohr * @return array 13775c93b77SAndreas Gohr */ 138d868eb89SAndreas Gohrfunction auth_loadACL() 139d868eb89SAndreas Gohr{ 14075c93b77SAndreas Gohr global $config_cascade; 141b78bf706Sromain global $USERINFO; 142585bf44eSChristopher Smith /* @var Input $INPUT */ 143585bf44eSChristopher Smith global $INPUT; 14475c93b77SAndreas Gohr 14524870174SAndreas Gohr if (!is_readable($config_cascade['acl']['default'])) return []; 14675c93b77SAndreas Gohr 14775c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 14875c93b77SAndreas Gohr 14924870174SAndreas Gohr $out = []; 1509ce556d2SAndreas Gohr foreach ($acl as $line) { 1519ce556d2SAndreas Gohr $line = trim($line); 1522401f18dSSyntaxseed if (empty($line) || ($line[0] == '#')) continue; // skip blank lines & comments 15324870174SAndreas Gohr [$id, $rest] = preg_split('/[ \t]+/', $line, 2); 15432e82180SAndreas Gohr 155443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 156ad3d68d7SChristopher Smith if (strstr($line, '%USER%')) { 157ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 158585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 159ad3d68d7SChristopher Smith 160585bf44eSChristopher Smith $id = str_replace('%USER%', cleanID($INPUT->server->str('REMOTE_USER')), $id); 161585bf44eSChristopher Smith $rest = str_replace('%USER%', auth_nameencode($INPUT->server->str('REMOTE_USER')), $rest); 162ad3d68d7SChristopher Smith } 163ad3d68d7SChristopher Smith 164ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1659ce556d2SAndreas Gohr if (strstr($line, '%GROUP%')) { 166ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 16706f34f54SPhy if (isset($USERINFO['grps'])) { 1689ce556d2SAndreas Gohr foreach ((array) $USERINFO['grps'] as $grp) { 169b78bf706Sromain $nid = str_replace('%GROUP%', cleanID($grp), $id); 17032e82180SAndreas Gohr $nrest = str_replace('%GROUP%', '@' . auth_nameencode($grp), $rest); 17132e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 172b78bf706Sromain } 17306f34f54SPhy } 17432e82180SAndreas Gohr } else { 17532e82180SAndreas Gohr $out[] = "$id\t$rest"; 176a8fe108bSGuy Brand } 17711799630Sandi } 1789ce556d2SAndreas Gohr 17932e82180SAndreas Gohr return $out; 180f3f0262cSandi} 181f3f0262cSandi 182ab5d26daSAndreas Gohr/** 183455aa67eSAndreas Gohr * Try a token login 184455aa67eSAndreas Gohr * 185455aa67eSAndreas Gohr * @return bool true if token login succeeded 186455aa67eSAndreas Gohr */ 187cf927d07Ssplitbrainfunction auth_tokenlogin() 188cf927d07Ssplitbrain{ 189455aa67eSAndreas Gohr global $USERINFO; 190455aa67eSAndreas Gohr global $INPUT; 191455aa67eSAndreas Gohr /** @var DokuWiki_Auth_Plugin $auth */ 192455aa67eSAndreas Gohr global $auth; 193455aa67eSAndreas Gohr if (!$auth) return false; 194455aa67eSAndreas Gohr 1957ffd5bd2SAndreas Gohr $headers = []; 1960a302752SAndreas Gohr 1970a302752SAndreas Gohr // try to get the headers from Apache 1980a302752SAndreas Gohr if (function_exists('getallheaders')) { 1990a302752SAndreas Gohr $headers = getallheaders(); 2000a302752SAndreas Gohr if (is_array($headers)) { 2010a302752SAndreas Gohr $headers = array_change_key_case($headers); 2020a302752SAndreas Gohr } 2030a302752SAndreas Gohr } 2040a302752SAndreas Gohr 2050a302752SAndreas Gohr // get the headers from $_SERVER 2060a302752SAndreas Gohr if (!$headers) { 2077ffd5bd2SAndreas Gohr foreach ($_SERVER as $key => $value) { 208093fe67eSAndreas Gohr if (str_starts_with($key, 'HTTP_')) { 2097ffd5bd2SAndreas Gohr $headers[strtolower(substr($key, 5))] = $value; 210455aa67eSAndreas Gohr } 2117ffd5bd2SAndreas Gohr } 2127ffd5bd2SAndreas Gohr } 2137ffd5bd2SAndreas Gohr 2147ffd5bd2SAndreas Gohr // check authorization header 2157ffd5bd2SAndreas Gohr if (isset($headers['authorization'])) { 2167ffd5bd2SAndreas Gohr [$type, $token] = sexplode(' ', $headers['authorization'], 2); 2177ffd5bd2SAndreas Gohr if ($type !== 'Bearer') $token = ''; // not the token we want 2187ffd5bd2SAndreas Gohr } 2197ffd5bd2SAndreas Gohr 2207ffd5bd2SAndreas Gohr // check x-dokuwiki-token header 2217ffd5bd2SAndreas Gohr if (isset($headers['x-dokuwiki-token'])) { 2227ffd5bd2SAndreas Gohr $token = $headers['x-dokuwiki-token']; 2237ffd5bd2SAndreas Gohr } 2247ffd5bd2SAndreas Gohr 2257ffd5bd2SAndreas Gohr if (empty($token)) return false; 226455aa67eSAndreas Gohr 227455aa67eSAndreas Gohr // check token 228455aa67eSAndreas Gohr try { 229cf927d07Ssplitbrain $authtoken = JWT::validate($token); 230455aa67eSAndreas Gohr } catch (Exception $e) { 231455aa67eSAndreas Gohr msg(hsc($e->getMessage()), -1); 232455aa67eSAndreas Gohr return false; 233455aa67eSAndreas Gohr } 234455aa67eSAndreas Gohr 235455aa67eSAndreas Gohr // fetch user info from backend 236455aa67eSAndreas Gohr $user = $authtoken->getUser(); 237455aa67eSAndreas Gohr $USERINFO = $auth->getUserData($user); 238455aa67eSAndreas Gohr if (!$USERINFO) return false; 239455aa67eSAndreas Gohr 240455aa67eSAndreas Gohr // the code is correct, set up user 241455aa67eSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 242455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 243455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = 'nope'; 244455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 2459cdd189dSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 246455aa67eSAndreas Gohr 247455aa67eSAndreas Gohr return true; 248455aa67eSAndreas Gohr} 249455aa67eSAndreas Gohr 250455aa67eSAndreas Gohr/** 251ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 252ab5d26daSAndreas Gohr * 25342ea7f44SGerrit Uitslag * @param array $evdata 254ab5d26daSAndreas Gohr * @return bool 2554dc42f7fSGerrit Uitslag * @throws Exception 256ab5d26daSAndreas Gohr */ 257d868eb89SAndreas Gohrfunction auth_login_wrapper($evdata) 258d868eb89SAndreas Gohr{ 259ab5d26daSAndreas Gohr return auth_login( 260ab5d26daSAndreas Gohr $evdata['user'], 261b5ee21aaSAdrian Lang $evdata['password'], 262b5ee21aaSAdrian Lang $evdata['sticky'], 263ab5d26daSAndreas Gohr $evdata['silent'] 264ab5d26daSAndreas Gohr ); 265b5ee21aaSAdrian Lang} 266b5ee21aaSAdrian Lang 267f3f0262cSandi/** 268f3f0262cSandi * This tries to login the user based on the sent auth credentials 269f3f0262cSandi * 270f3f0262cSandi * The authentication works like this: if a username was given 27115fae107Sandi * a new login is assumed and user/password are checked. If they 27215fae107Sandi * are correct the password is encrypted with blowfish and stored 27315fae107Sandi * together with the username in a cookie - the same info is stored 27415fae107Sandi * in the session, too. Additonally a browserID is stored in the 27515fae107Sandi * session. 27615fae107Sandi * 27715fae107Sandi * If no username was given the cookie is checked: if the username, 27815fae107Sandi * crypted password and browserID match between session and cookie 27915fae107Sandi * no further testing is done and the user is accepted 28015fae107Sandi * 28115fae107Sandi * If a cookie was found but no session info was availabe the 282136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 28315fae107Sandi * together with username rechecked by calling this function again. 284f3f0262cSandi * 285f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 286f3f0262cSandi * are set. 28715fae107Sandi * 28815fae107Sandi * @param string $user Username 28915fae107Sandi * @param string $pass Cleartext Password 29015fae107Sandi * @param bool $sticky Cookie should not expire 291f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 29215fae107Sandi * @return bool true on successful auth 2934dc42f7fSGerrit Uitslag * @throws Exception 2944dc42f7fSGerrit Uitslag * 2954dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 296f3f0262cSandi */ 297d868eb89SAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) 298d868eb89SAndreas Gohr{ 299f3f0262cSandi global $USERINFO; 300f3f0262cSandi global $conf; 301f3f0262cSandi global $lang; 302e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 303cd52f92dSchris global $auth; 304585bf44eSChristopher Smith /* @var Input $INPUT */ 305585bf44eSChristopher Smith global $INPUT; 306ab5d26daSAndreas Gohr 3076547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 308beca106aSAdrian Lang 309bbbd6568SAndreas Gohr if (!empty($user)) { 310132bdbfeSandi //usual login 311f7f6f5fcSsplitbrain if (!empty($pass)) usleep(random_int(0, 250)); // add a random delay to prevent timing attacks #4491 3125e9e1054SAndreas Gohr if (!empty($pass) && $auth->checkPass($user, $pass)) { 313132bdbfeSandi // make logininfo globally available 314585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 31530d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 31604369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 317132bdbfeSandi return true; 318f3f0262cSandi } else { 319f3f0262cSandi //invalid credentials - log off 320f8b1e4e7SAndreas Gohr if (!$silent) { 321f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 322f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 323f8b1e4e7SAndreas Gohr } 324f3f0262cSandi auth_logoff(); 325132bdbfeSandi return false; 326f3f0262cSandi } 327f3f0262cSandi } else { 328132bdbfeSandi // read cookie information 32924870174SAndreas Gohr [$user, $sticky, $pass] = auth_getCookie(); 330132bdbfeSandi if ($user && $pass) { 331132bdbfeSandi // we got a cookie - see if we can trust it 332fa7c70ffSAdrian Lang 333fa7c70ffSAdrian Lang // get session info 3340058ae75SDamien Regad if (isset($_SESSION[DOKU_COOKIE])) { 335bc6b1759SAndreas Gohr $session = $_SESSION[DOKU_COOKIE]['auth'] ?? []; 3367d34963bSAndreas Gohr if ( 3379d1b6472SEduardo Mozart de Oliveira isset($session['user']) && 3389d1b6472SEduardo Mozart de Oliveira isset($session['pass']) && 3397172dbc0SAndreas Gohr $auth->useSessionCache($user) && 3404c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 341e4b0c5a0SAndreas Gohr ($session['user'] === $user) && 342e4b0c5a0SAndreas Gohr ($session['pass'] === sha1($pass)) && //still crypted 343e4b0c5a0SAndreas Gohr ($session['buid'] === auth_browseruid()) 344ab5d26daSAndreas Gohr ) { 345132bdbfeSandi // he has session, cookie and browser right - let him in 346585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 347132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 348132bdbfeSandi return true; 349132bdbfeSandi } 3500058ae75SDamien Regad } 351f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 35230d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 35304369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 354f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 355132bdbfeSandi } 356132bdbfeSandi } 357f3f0262cSandi //just to be sure 358883179a4SAndreas Gohr auth_logoff(true); 359132bdbfeSandi return false; 360f3f0262cSandi} 361132bdbfeSandi 362132bdbfeSandi/** 363136ce040Sandi * Builds a pseudo UID from browser and IP data 364132bdbfeSandi * 365132bdbfeSandi * This is neither unique nor unfakable - still it adds some 366136ce040Sandi * security. Using the first part of the IP makes sure 36780b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 36815fae107Sandi * 36915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 37015fae107Sandi * 371b13c0e1aSAdaKaleh * @return string a SHA256 sum of various browser headers 372132bdbfeSandi */ 373d868eb89SAndreas Gohrfunction auth_browseruid() 374d868eb89SAndreas Gohr{ 375585bf44eSChristopher Smith /* @var Input $INPUT */ 376585bf44eSChristopher Smith global $INPUT; 377585bf44eSChristopher Smith 3782f9daf16SAndreas Gohr $ip = clientIP(true); 379b13c0e1aSAdaKaleh // convert IP string to packed binary representation 380b13c0e1aSAdaKaleh $pip = inet_pton($ip); 381b7c67f83SAndreas Gohr 382b7c67f83SAndreas Gohr $uid = implode("\n", [ 383b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_USER_AGENT'), 384b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_ACCEPT_LANGUAGE'), 385b7c67f83SAndreas Gohr substr($pip, 0, strlen($pip) / 2), // use half of the IP address (works for both IPv4 and IPv6) 386b7c67f83SAndreas Gohr ]); 387b13c0e1aSAdaKaleh return hash('sha256', $uid); 388132bdbfeSandi} 389132bdbfeSandi 390132bdbfeSandi/** 391132bdbfeSandi * Creates a random key to encrypt the password in cookies 39215fae107Sandi * 39315fae107Sandi * This function tries to read the password for encrypting 39498407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 39515fae107Sandi * if no such file is found a random key is created and 39615fae107Sandi * and stored in this file. 39715fae107Sandi * 39832ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 39930d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 40015fae107Sandi * @return string 4014dc42f7fSGerrit Uitslag * @throws Exception 4024dc42f7fSGerrit Uitslag * 4034dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 404132bdbfeSandi */ 405d868eb89SAndreas Gohrfunction auth_cookiesalt($addsession = false, $secure = false) 406d868eb89SAndreas Gohr{ 407a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 408fe745becSMichael Große return 'test'; 409a1fe3c9cSMichael Große } 410132bdbfeSandi global $conf; 41198407a7aSandi $file = $conf['metadir'] . '/_htcookiesalt'; 41230d544a4SMichael Hamann if ($secure || !file_exists($file)) { 41330d544a4SMichael Hamann $file = $conf['metadir'] . '/_htcookiesalt2'; 41430d544a4SMichael Hamann } 415132bdbfeSandi $salt = io_readFile($file); 416132bdbfeSandi if (empty($salt)) { 41730d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 418132bdbfeSandi io_saveFile($file, $salt); 419132bdbfeSandi } 42032ed2b36SAndreas Gohr if ($addsession) { 42132ed2b36SAndreas Gohr $salt .= session_id(); 42232ed2b36SAndreas Gohr } 423132bdbfeSandi return $salt; 424f3f0262cSandi} 425f3f0262cSandi 426f3f0262cSandi/** 4277a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 428483b6238SMichael Hamann * 4297a33d2f8SNiklas Keller * @param int $length number of bytes 4307a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 4314dc42f7fSGerrit Uitslag * @throws Exception 4324dc42f7fSGerrit Uitslag * 4334dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 434483b6238SMichael Hamann */ 435d868eb89SAndreas Gohrfunction auth_randombytes($length) 436d868eb89SAndreas Gohr{ 4377a33d2f8SNiklas Keller return random_bytes($length); 438483b6238SMichael Hamann} 439483b6238SMichael Hamann 440483b6238SMichael Hamann/** 4417a33d2f8SNiklas Keller * Cryptographically secure random number generator. 442483b6238SMichael Hamann * 443483b6238SMichael Hamann * @param int $min 444483b6238SMichael Hamann * @param int $max 445483b6238SMichael Hamann * @return int 4464dc42f7fSGerrit Uitslag * @throws Exception 4474dc42f7fSGerrit Uitslag * 4484dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 449483b6238SMichael Hamann */ 450d868eb89SAndreas Gohrfunction auth_random($min, $max) 451d868eb89SAndreas Gohr{ 4527a33d2f8SNiklas Keller return random_int($min, $max); 453483b6238SMichael Hamann} 454483b6238SMichael Hamann 455483b6238SMichael Hamann/** 45604369c3eSMichael Hamann * Encrypt data using the given secret using AES 45704369c3eSMichael Hamann * 45804369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 45904369c3eSMichael Hamann * using pbkdf2. 46004369c3eSMichael Hamann * 46104369c3eSMichael Hamann * @param string $data The data that shall be encrypted 46204369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 46304369c3eSMichael Hamann * @return string The ciphertext 4644dc42f7fSGerrit Uitslag * @throws Exception 46504369c3eSMichael Hamann */ 466d868eb89SAndreas Gohrfunction auth_encrypt($data, $secret) 467d868eb89SAndreas Gohr{ 46804369c3eSMichael Hamann $iv = auth_randombytes(16); 469927933f5SAndreas Gohr $cipher = new AES('cbc'); 47047e9ed0eSAndreas Gohr $cipher->setPassword($secret, 'pbkdf2', 'sha1', 'phpseclib'); 471927933f5SAndreas Gohr $cipher->setIV($iv); 47204369c3eSMichael Hamann 4737b650cefSMichael Hamann /* 4747b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 4757b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 4767b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 4777b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 4787b650cefSMichael Hamann */ 47904369c3eSMichael Hamann return $cipher->encrypt($iv . $data); 48004369c3eSMichael Hamann} 48104369c3eSMichael Hamann 48204369c3eSMichael Hamann/** 48304369c3eSMichael Hamann * Decrypt the given AES ciphertext 48404369c3eSMichael Hamann * 48504369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 48604369c3eSMichael Hamann * 48704369c3eSMichael Hamann * @param string $ciphertext The encrypted data 48804369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 4891cedacf2SAndreas Gohr * @return string|null The decrypted data 49004369c3eSMichael Hamann */ 491d868eb89SAndreas Gohrfunction auth_decrypt($ciphertext, $secret) 492d868eb89SAndreas Gohr{ 4937b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 494927933f5SAndreas Gohr $cipher = new AES('cbc'); 49547e9ed0eSAndreas Gohr $cipher->setPassword($secret, 'pbkdf2', 'sha1', 'phpseclib'); 4967b650cefSMichael Hamann $cipher->setIV($iv); 49704369c3eSMichael Hamann 4981cedacf2SAndreas Gohr try { 4997b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 5001cedacf2SAndreas Gohr } catch (BadDecryptionException $e) { 5011cedacf2SAndreas Gohr ErrorHandler::logException($e); 5021cedacf2SAndreas Gohr return null; 5031cedacf2SAndreas Gohr } 50404369c3eSMichael Hamann} 50504369c3eSMichael Hamann 50604369c3eSMichael Hamann/** 507883179a4SAndreas Gohr * Log out the current user 508883179a4SAndreas Gohr * 509f3f0262cSandi * This clears all authentication data and thus log the user 510883179a4SAndreas Gohr * off. It also clears session data. 51115fae107Sandi * 51215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 51342ea7f44SGerrit Uitslag * 514883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 515f3f0262cSandi */ 516d868eb89SAndreas Gohrfunction auth_logoff($keepbc = false) 517d868eb89SAndreas Gohr{ 518f3f0262cSandi global $conf; 519f3f0262cSandi global $USERINFO; 520e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 5215298a619SAndreas Gohr global $auth; 522585bf44eSChristopher Smith /* @var Input $INPUT */ 523585bf44eSChristopher Smith global $INPUT; 52437065e65Sandi 525d4869846SAndreas Gohr // make sure the session is writable (it usually is) 526e9621d07SAndreas Gohr @session_start(); 527e9621d07SAndreas Gohr 528e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 529e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 530e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 531e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 532e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 533e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 534883179a4SAndreas Gohr if (!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 535e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 536585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 537132bdbfeSandi $USERINFO = null; //FIXME 538f5c6743cSAndreas Gohr 53973ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 540bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, '', [ 541bf8392ebSAndreas Gohr 'expires' => time() - 600000, 542bf8392ebSAndreas Gohr 'path' => $cookieDir, 5439399c87eSsplitbrain 'secure' => ($conf['securecookie'] && Ip::isSsl()), 544bf8392ebSAndreas Gohr 'httponly' => true, 545486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 546bf8392ebSAndreas Gohr ]); 5475298a619SAndreas Gohr 5486547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin) { 5496547cfc7SGerrit Uitslag $auth->logOff(); 5506547cfc7SGerrit Uitslag } 551f3f0262cSandi} 552f3f0262cSandi 553f3f0262cSandi/** 554f8cc712eSAndreas Gohr * Check if a user is a manager 555f8cc712eSAndreas Gohr * 556f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 557f8cc712eSAndreas Gohr * user. 558f8cc712eSAndreas Gohr * 559f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 560f8cc712eSAndreas Gohr * 561ab5d26daSAndreas Gohr * @param string $user Username 562ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 563ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 56410396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 565ab5d26daSAndreas Gohr * @return bool 56696348f27SAndreas Gohr * @see auth_isadmin 56796348f27SAndreas Gohr * 56896348f27SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 569f8cc712eSAndreas Gohr */ 570d868eb89SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache = false) 571d868eb89SAndreas Gohr{ 572f8cc712eSAndreas Gohr global $conf; 573f8cc712eSAndreas Gohr global $USERINFO; 574e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 575d752aedeSAndreas Gohr global $auth; 576585bf44eSChristopher Smith /* @var Input $INPUT */ 577585bf44eSChristopher Smith global $INPUT; 578585bf44eSChristopher Smith 579f8cc712eSAndreas Gohr 5806547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 581c66972f2SAdrian Lang if (is_null($user)) { 582585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) { 583c66972f2SAdrian Lang return false; 584c66972f2SAdrian Lang } else { 585585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 586c66972f2SAdrian Lang } 587c66972f2SAdrian Lang } 588d6dc956fSAndreas Gohr if (is_null($groups)) { 5891525c228SAnna Dabrowska // checking the logged in user, or another one? 5901525c228SAnna Dabrowska if ($USERINFO && $user === $INPUT->server->str('REMOTE_USER')) { 5911525c228SAnna Dabrowska $groups = (array) $USERINFO['grps']; 59266b108d6SAnna Dabrowska } else { 5936cf7b139SAndreas Gohr $groups = $auth->getUserData($user); 5946cf7b139SAndreas Gohr $groups = $groups ? $groups['grps'] : []; 59566b108d6SAnna Dabrowska } 596e259aa79SAndreas Gohr } 597e259aa79SAndreas Gohr 59896348f27SAndreas Gohr // prefer cached result 59996348f27SAndreas Gohr static $cache = []; 60010396f77SAndreas Gohr $cachekey = serialize([$user, $adminonly, $groups]); 60196348f27SAndreas Gohr if (!isset($cache[$cachekey]) || $recache) { 602d6dc956fSAndreas Gohr // check superuser match 60396348f27SAndreas Gohr $ok = auth_isMember($conf['superuser'], $user, $groups); 60400ce12daSChris Smith 60596348f27SAndreas Gohr // check managers 60696348f27SAndreas Gohr if (!$ok && !$adminonly) { 60796348f27SAndreas Gohr $ok = auth_isMember($conf['manager'], $user, $groups); 60896348f27SAndreas Gohr } 60996348f27SAndreas Gohr 61096348f27SAndreas Gohr $cache[$cachekey] = $ok; 61196348f27SAndreas Gohr } 61296348f27SAndreas Gohr 61396348f27SAndreas Gohr return $cache[$cachekey]; 614f8cc712eSAndreas Gohr} 615f8cc712eSAndreas Gohr 616f8cc712eSAndreas Gohr/** 617f8cc712eSAndreas Gohr * Check if a user is admin 618f8cc712eSAndreas Gohr * 619f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 620f8cc712eSAndreas Gohr * 621f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 622f8cc712eSAndreas Gohr * 62396348f27SAndreas Gohr * @param string $user Username 62496348f27SAndreas Gohr * @param array $groups List of groups the user is in 62510396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 62696348f27SAndreas Gohr * @return bool 627f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 628ab5d26daSAndreas Gohr * @see auth_ismanager() 62942ea7f44SGerrit Uitslag * 630f8cc712eSAndreas Gohr */ 631d868eb89SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache = false) 632d868eb89SAndreas Gohr{ 63396348f27SAndreas Gohr return auth_ismanager($user, $groups, true, $recache); 634f8cc712eSAndreas Gohr} 635f8cc712eSAndreas Gohr 636d6dc956fSAndreas Gohr/** 637d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 638d6dc956fSAndreas Gohr * users and groups to determine membership status 639d6dc956fSAndreas Gohr * 640d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 641d6dc956fSAndreas Gohr * 64242ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 64342ea7f44SGerrit Uitslag * @param string $user user to match against 64442ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 6455446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 646d6dc956fSAndreas Gohr */ 647d868eb89SAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) 648d868eb89SAndreas Gohr{ 649e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 650d6dc956fSAndreas Gohr global $auth; 6516547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 652d6dc956fSAndreas Gohr 653d6dc956fSAndreas Gohr // clean user and groups 6544f56ecbfSAdrian Lang if (!$auth->isCaseSensitive()) { 65524870174SAndreas Gohr $user = PhpString::strtolower($user); 656093fe67eSAndreas Gohr $groups = array_map(PhpString::strtolower(...), $groups); 657d6dc956fSAndreas Gohr } 658d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 659093fe67eSAndreas Gohr $groups = array_map($auth->cleanGroup(...), $groups); 660d6dc956fSAndreas Gohr 661d6dc956fSAndreas Gohr // extract the memberlist 662d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 663093fe67eSAndreas Gohr $members = array_map(trim(...), $members); 664d6dc956fSAndreas Gohr $members = array_unique($members); 665d6dc956fSAndreas Gohr $members = array_filter($members); 666d6dc956fSAndreas Gohr 667d6dc956fSAndreas Gohr // compare cleaned values 668d6dc956fSAndreas Gohr foreach ($members as $member) { 669e5204a12SJurgen Hart if ($member == '@ALL') return true; 67024870174SAndreas Gohr if (!$auth->isCaseSensitive()) $member = PhpString::strtolower($member); 671d6dc956fSAndreas Gohr if ($member[0] == '@') { 672d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 673d6dc956fSAndreas Gohr if (in_array($member, $groups)) return true; 674d6dc956fSAndreas Gohr } else { 675d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 676d6dc956fSAndreas Gohr if ($member == $user) return true; 677d6dc956fSAndreas Gohr } 678d6dc956fSAndreas Gohr } 679d6dc956fSAndreas Gohr 680d6dc956fSAndreas Gohr // still here? not a member! 681d6dc956fSAndreas Gohr return false; 682d6dc956fSAndreas Gohr} 683d6dc956fSAndreas Gohr 684f8cc712eSAndreas Gohr/** 68515fae107Sandi * Convinience function for auth_aclcheck() 68615fae107Sandi * 68715fae107Sandi * This checks the permissions for the current user 68815fae107Sandi * 68915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 69015fae107Sandi * 6911698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 69215fae107Sandi * @return int permission level 693f3f0262cSandi */ 694d868eb89SAndreas Gohrfunction auth_quickaclcheck($id) 695d868eb89SAndreas Gohr{ 696f3f0262cSandi global $conf; 697f3f0262cSandi global $USERINFO; 698585bf44eSChristopher Smith /* @var Input $INPUT */ 699585bf44eSChristopher Smith global $INPUT; 700f3f0262cSandi # if no ACL is used always return upload rights 701f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 70224870174SAndreas Gohr return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), is_array($USERINFO) ? $USERINFO['grps'] : []); 703f3f0262cSandi} 704f3f0262cSandi 705f3f0262cSandi/** 706*7e687fd8SAndreas Gohr * Build the ACL path for a media file. 707*7e687fd8SAndreas Gohr * 708*7e687fd8SAndreas Gohr * Media files do not have per-file ACLs; permissions are always evaluated against the namespace 709*7e687fd8SAndreas Gohr * they live in. This returns the namespace wildcard path (e.g. "wiki:*" or "*" for root-namespace 710*7e687fd8SAndreas Gohr * media) suitable for passing to auth_quickaclcheck() or auth_aclcheck(). 711*7e687fd8SAndreas Gohr * 712*7e687fd8SAndreas Gohr * @param string $id media ID (needs to be resolved and cleaned) 713*7e687fd8SAndreas Gohr * @return string the ACL path to check 714*7e687fd8SAndreas Gohr */ 715*7e687fd8SAndreas Gohrfunction mediaAclPath($id) 716*7e687fd8SAndreas Gohr{ 717*7e687fd8SAndreas Gohr return ltrim(getNS($id) . ':*', ':'); 718*7e687fd8SAndreas Gohr} 719*7e687fd8SAndreas Gohr 720*7e687fd8SAndreas Gohr/** 721c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 72215fae107Sandi * 72315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 72442ea7f44SGerrit Uitslag * 725c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 7261698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 72715fae107Sandi * @param string $user Username 7283272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 72915fae107Sandi * @return int permission level 730f3f0262cSandi */ 731d868eb89SAndreas Gohrfunction auth_aclcheck($id, $user, $groups) 732d868eb89SAndreas Gohr{ 73324870174SAndreas Gohr $data = [ 734bf8f8509SAndreas Gohr 'id' => $id ?? '', 735c17acc9fSAndreas Gohr 'user' => $user, 736c17acc9fSAndreas Gohr 'groups' => $groups 73724870174SAndreas Gohr ]; 738c17acc9fSAndreas Gohr 739cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 740c17acc9fSAndreas Gohr} 741c17acc9fSAndreas Gohr 742c17acc9fSAndreas Gohr/** 743c17acc9fSAndreas Gohr * default ACL check method 744c17acc9fSAndreas Gohr * 745c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 746c17acc9fSAndreas Gohr * 747c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 74842ea7f44SGerrit Uitslag * 749c17acc9fSAndreas Gohr * @param array $data event data 750c17acc9fSAndreas Gohr * @return int permission level 751c17acc9fSAndreas Gohr */ 752d868eb89SAndreas Gohrfunction auth_aclcheck_cb($data) 753d868eb89SAndreas Gohr{ 754c17acc9fSAndreas Gohr $id =& $data['id']; 755c17acc9fSAndreas Gohr $user =& $data['user']; 756c17acc9fSAndreas Gohr $groups =& $data['groups']; 757c17acc9fSAndreas Gohr 758f3f0262cSandi global $conf; 759f3f0262cSandi global $AUTH_ACL; 760e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 761d752aedeSAndreas Gohr global $auth; 762f3f0262cSandi 76385d03f68SAndreas Gohr // if no ACL is used always return upload rights 764f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 7656547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return AUTH_NONE; 766bf8f8509SAndreas Gohr if (!is_array($AUTH_ACL)) return AUTH_NONE; 767f3f0262cSandi 768074cf26bSandi //make sure groups is an array 76924870174SAndreas Gohr if (!is_array($groups)) $groups = []; 770074cf26bSandi 77185d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 772ab5d26daSAndreas Gohr if (auth_isadmin($user, $groups)) { 773ab5d26daSAndreas Gohr return AUTH_ADMIN; 774ab5d26daSAndreas Gohr } 77585d03f68SAndreas Gohr 776eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive()) { 77724870174SAndreas Gohr $user = PhpString::strtolower($user); 778093fe67eSAndreas Gohr $groups = array_map(PhpString::strtolower(...), $groups); 779eb3ce0d5SKazutaka Miyasaka } 78037ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 781093fe67eSAndreas Gohr $groups = array_map($auth->cleanGroup(...), $groups); 78285d03f68SAndreas Gohr 7836c2bb100SAndreas Gohr //prepend groups with @ and nameencode 78437ff2261SSascha Klopp foreach ($groups as &$group) { 78537ff2261SSascha Klopp $group = '@' . auth_nameencode($group); 78610a76f6fSfrank } 78710a76f6fSfrank 788f3f0262cSandi $ns = getNS($id); 789f3f0262cSandi $perm = -1; 790f3f0262cSandi 791f3f0262cSandi //add ALL group 792f3f0262cSandi $groups[] = '@ALL'; 79337ff2261SSascha Klopp 794f3f0262cSandi //add User 79534aeb4afSAndreas Gohr if ($user) $groups[] = $user; 796f3f0262cSandi 797f3f0262cSandi //check exact match first 79821c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($id, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 799f3f0262cSandi if (count($matches)) { 800f3f0262cSandi foreach ($matches as $match) { 801f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 80221c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 803eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 80424870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 805eb3ce0d5SKazutaka Miyasaka } 80648d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 80748d7b7a6SDominik Eckelmann continue; 80848d7b7a6SDominik Eckelmann } 8098ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 810f3f0262cSandi if ($acl[2] > $perm) { 811f3f0262cSandi $perm = $acl[2]; 812f3f0262cSandi } 813f3f0262cSandi } 814f3f0262cSandi if ($perm > -1) { 815f3f0262cSandi //we had a match - return it 816def492a2SGuillaume Turri return (int) $perm; 817f3f0262cSandi } 818f3f0262cSandi } 819f3f0262cSandi 820f3f0262cSandi //still here? do the namespace checks 821f3f0262cSandi if ($ns) { 8223e304b55SMichael Hamann $path = $ns . ':*'; 823f3f0262cSandi } else { 8243e304b55SMichael Hamann $path = '*'; //root document 825f3f0262cSandi } 826f3f0262cSandi 827f3f0262cSandi do { 82821c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($path, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 829f3f0262cSandi if (count($matches)) { 830f3f0262cSandi foreach ($matches as $match) { 831f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 83221c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 833eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 83424870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 835eb3ce0d5SKazutaka Miyasaka } 83648d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 83748d7b7a6SDominik Eckelmann continue; 83848d7b7a6SDominik Eckelmann } 8398ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 840f3f0262cSandi if ($acl[2] > $perm) { 841f3f0262cSandi $perm = $acl[2]; 842f3f0262cSandi } 843f3f0262cSandi } 844f3f0262cSandi //we had a match - return it 84548d7b7a6SDominik Eckelmann if ($perm != -1) { 846def492a2SGuillaume Turri return (int) $perm; 847f3f0262cSandi } 84848d7b7a6SDominik Eckelmann } 849f3f0262cSandi //get next higher namespace 850f3f0262cSandi $ns = getNS($ns); 851f3f0262cSandi 8523e304b55SMichael Hamann if ($path != '*') { 8533e304b55SMichael Hamann $path = $ns . ':*'; 8543e304b55SMichael Hamann if ($path == ':*') $path = '*'; 855f3f0262cSandi } else { 856f3f0262cSandi //we did this already 857f3f0262cSandi //looks like there is something wrong with the ACL 858f3f0262cSandi //break here 859d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 860d5ce66f6SAndreas Gohr return AUTH_NONE; 861f3f0262cSandi } 862f3f0262cSandi } while (1); //this should never loop endless 863ab5d26daSAndreas Gohr return AUTH_NONE; 864f3f0262cSandi} 865f3f0262cSandi 866f3f0262cSandi/** 8676c2bb100SAndreas Gohr * Encode ASCII special chars 8686c2bb100SAndreas Gohr * 8696c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 8706c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 8716c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 8726c2bb100SAndreas Gohr * urlencoding!). 8736c2bb100SAndreas Gohr * 8746c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 8756c2bb100SAndreas Gohr * 8766c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 8776c2bb100SAndreas Gohr * @see rawurldecode() 87842ea7f44SGerrit Uitslag * 87942ea7f44SGerrit Uitslag * @param string $name 88042ea7f44SGerrit Uitslag * @param bool $skip_group 88142ea7f44SGerrit Uitslag * @return string 8826c2bb100SAndreas Gohr */ 883d868eb89SAndreas Gohrfunction auth_nameencode($name, $skip_group = false) 884d868eb89SAndreas Gohr{ 885a424cd8eSchris global $cache_authname; 886a424cd8eSchris $cache =& $cache_authname; 88731784267SAndreas Gohr $name = (string) $name; 888a424cd8eSchris 88980601d26SAndreas Gohr // never encode wildcard FS#1955 89080601d26SAndreas Gohr if ($name == '%USER%') return $name; 891b78bf706Sromain if ($name == '%GROUP%') return $name; 89280601d26SAndreas Gohr 893a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 8942401f18dSSyntaxseed if ($skip_group && $name[0] == '@') { 89530f6faf0SChristopher Smith $cache[$name][$skip_group] = '@' . preg_replace_callback( 89630f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 897093fe67eSAndreas Gohr auth_nameencode_callback(...), 898dccd6b2bSAndreas Gohr substr($name, 1) 899ab5d26daSAndreas Gohr ); 900e838fc2eSAndreas Gohr } else { 90130f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 90230f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 903093fe67eSAndreas Gohr auth_nameencode_callback(...), 904dccd6b2bSAndreas Gohr $name 905ab5d26daSAndreas Gohr ); 906e838fc2eSAndreas Gohr } 9076c2bb100SAndreas Gohr } 9086c2bb100SAndreas Gohr 909a424cd8eSchris return $cache[$name][$skip_group]; 910a424cd8eSchris} 911a424cd8eSchris 91204d68ae4SGerrit Uitslag/** 91304d68ae4SGerrit Uitslag * callback encodes the matches 91404d68ae4SGerrit Uitslag * 91504d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 91604d68ae4SGerrit Uitslag * @return string 91704d68ae4SGerrit Uitslag */ 918d868eb89SAndreas Gohrfunction auth_nameencode_callback($matches) 919d868eb89SAndreas Gohr{ 92030f6faf0SChristopher Smith return '%' . dechex(ord(substr($matches[1], -1))); 92130f6faf0SChristopher Smith} 92230f6faf0SChristopher Smith 9236c2bb100SAndreas Gohr/** 924f3f0262cSandi * Create a pronouncable password 925f3f0262cSandi * 9268a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 9278a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 9288a285f7fSAndreas Gohr * 9294dc42f7fSGerrit Uitslag * @param string $foruser username for which the password is generated 9304dc42f7fSGerrit Uitslag * @return string pronouncable password 9314dc42f7fSGerrit Uitslag * @throws Exception 9324dc42f7fSGerrit Uitslag * 93315fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 9348a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 93515fae107Sandi * 9364dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 937f3f0262cSandi */ 938d868eb89SAndreas Gohrfunction auth_pwgen($foruser = '') 939d868eb89SAndreas Gohr{ 94024870174SAndreas Gohr $data = [ 941d628dcf3SAndreas Gohr 'password' => '', 942d628dcf3SAndreas Gohr 'foruser' => $foruser 94324870174SAndreas Gohr ]; 9448a285f7fSAndreas Gohr 945e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 9468a285f7fSAndreas Gohr if ($evt->advise_before(true)) { 947f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 948f3f0262cSandi $v = 'aeiou'; //vowels 949f3f0262cSandi $a = $c . $v; //both 950987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 951f3f0262cSandi 952987c8d26SAndreas Gohr //use thre syllables... 953987c8d26SAndreas Gohr for ($i = 0; $i < 3; $i++) { 954483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 955483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 956483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 957f3f0262cSandi } 958987c8d26SAndreas Gohr //... and add a nice number and special 95943f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)] . auth_random(10, 99); 9608a285f7fSAndreas Gohr } 9618a285f7fSAndreas Gohr $evt->advise_after(); 962f3f0262cSandi 9638a285f7fSAndreas Gohr return $data['password']; 964f3f0262cSandi} 965f3f0262cSandi 966f3f0262cSandi/** 967f3f0262cSandi * Sends a password to the given user 968f3f0262cSandi * 96915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 97042ea7f44SGerrit Uitslag * 971ab5d26daSAndreas Gohr * @param string $user Login name of the user 972ab5d26daSAndreas Gohr * @param string $password The new password in clear text 97315fae107Sandi * @return bool true on success 974f3f0262cSandi */ 975d868eb89SAndreas Gohrfunction auth_sendPassword($user, $password) 976d868eb89SAndreas Gohr{ 977f3f0262cSandi global $lang; 978e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 979cd52f92dSchris global $auth; 9806547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 981cd52f92dSchris 982d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 9834dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 984f3f0262cSandi 98587ddda95Sandi if (!$userinfo['mail']) return false; 986f3f0262cSandi 987f3f0262cSandi $text = rawLocale('password'); 98824870174SAndreas Gohr $trep = [ 989d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 990d7169d19SAndreas Gohr 'LOGIN' => $user, 991d7169d19SAndreas Gohr 'PASSWORD' => $password 99224870174SAndreas Gohr ]; 993f3f0262cSandi 994d7169d19SAndreas Gohr $mail = new Mailer(); 995102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']) . ' <' . $userinfo['mail'] . '>'); 996d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 997d7169d19SAndreas Gohr $mail->setBody($text, $trep); 998d7169d19SAndreas Gohr return $mail->send(); 999f3f0262cSandi} 1000f3f0262cSandi 1001f3f0262cSandi/** 100215fae107Sandi * Register a new user 1003f3f0262cSandi * 100415fae107Sandi * This registers a new user - Data is read directly from $_POST 100515fae107Sandi * 100615fae107Sandi * @return bool true on success, false on any error 10074dc42f7fSGerrit Uitslag * @throws Exception 10084dc42f7fSGerrit Uitslag * 10094dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 1010f3f0262cSandi */ 1011d868eb89SAndreas Gohrfunction register() 1012d868eb89SAndreas Gohr{ 1013f3f0262cSandi global $lang; 1014eb5d07e4Sjan global $conf; 10154dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 1016cd52f92dSchris global $auth; 101764273335SAndreas Gohr global $INPUT; 1018f3f0262cSandi 101964273335SAndreas Gohr if (!$INPUT->post->bool('save')) return false; 10203a48618aSAnika Henke if (!actionOK('register')) return false; 1021640145a5Sandi 102264273335SAndreas Gohr // gather input 102364273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 102464273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 102564273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 102664273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 102764273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 1028d752aedeSAndreas Gohr 102964273335SAndreas Gohr if (empty($login) || empty($fullname) || empty($email)) { 1030f3f0262cSandi msg($lang['regmissing'], -1); 1031f3f0262cSandi return false; 1032f3f0262cSandi } 1033f3f0262cSandi 1034cab2716aSmatthias.grimm if ($conf['autopasswd']) { 10358a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 103664273335SAndreas Gohr } elseif (empty($pass) || empty($passchk)) { 1037bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 1038cab2716aSmatthias.grimm return false; 103964273335SAndreas Gohr } elseif ($pass != $passchk) { 1040bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 1041cab2716aSmatthias.grimm return false; 1042cab2716aSmatthias.grimm } 1043cab2716aSmatthias.grimm 1044f3f0262cSandi //check mail 104573dc0a89SAndreas Gohr if (!MailUtils::isValid($email)) { 1046f3f0262cSandi msg($lang['regbadmail'], -1); 1047f3f0262cSandi return false; 1048f3f0262cSandi } 1049f3f0262cSandi 1050f3f0262cSandi //okay try to create the user 105124870174SAndreas Gohr if (!$auth->triggerUserMod('create', [$login, $pass, $fullname, $email])) { 1052db9faf02SPatrick Brown msg($lang['regfail'], -1); 1053f3f0262cSandi return false; 1054f3f0262cSandi } 1055f3f0262cSandi 1056790b7720SAndreas Gohr // send notification about the new user 105775d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 105875d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 105902a498e7Schris 1060790b7720SAndreas Gohr // are we done? 1061cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 1062cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 1063cab2716aSmatthias.grimm return true; 1064cab2716aSmatthias.grimm } 1065cab2716aSmatthias.grimm 1066790b7720SAndreas Gohr // autogenerated password? then send password to user 106764273335SAndreas Gohr if (auth_sendPassword($login, $pass)) { 1068f3f0262cSandi msg($lang['regsuccess'], 1); 1069f3f0262cSandi return true; 1070f3f0262cSandi } else { 1071f3f0262cSandi msg($lang['regmailfail'], -1); 1072f3f0262cSandi return false; 1073f3f0262cSandi } 1074f3f0262cSandi} 1075f3f0262cSandi 107610a76f6fSfrank/** 10778b06d178Schris * Update user profile 10788b06d178Schris * 10794dc42f7fSGerrit Uitslag * @throws Exception 10804dc42f7fSGerrit Uitslag * 10818b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 10828b06d178Schris */ 1083d868eb89SAndreas Gohrfunction updateprofile() 1084d868eb89SAndreas Gohr{ 10858b06d178Schris global $conf; 10868b06d178Schris global $lang; 1087e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1088cd52f92dSchris global $auth; 1089bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1090bcc94b2cSAndreas Gohr global $INPUT; 10918b06d178Schris 1092bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 10931b2a85e8SAndreas Gohr if (!checkSecurityToken()) return false; 10948b06d178Schris 10953a48618aSAnika Henke if (!actionOK('profile')) { 10968b06d178Schris msg($lang['profna'], -1); 10978b06d178Schris return false; 10988b06d178Schris } 10998b06d178Schris 110024870174SAndreas Gohr $changes = []; 1101bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 1102bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 1103bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 1104bcc94b2cSAndreas Gohr 1105bcc94b2cSAndreas Gohr // check misspelled passwords 1106bcc94b2cSAndreas Gohr if ($changes['pass'] != $INPUT->post->str('passchk')) { 1107bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 11088b06d178Schris return false; 11098b06d178Schris } 11108b06d178Schris 11118b06d178Schris // clean fullname and email 1112bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1113bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 11148b06d178Schris 1115bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 11167d34963bSAndreas Gohr if ( 11177d34963bSAndreas Gohr (empty($changes['name']) && $auth->canDo('modName')) || 1118bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1119ab5d26daSAndreas Gohr ) { 11208b06d178Schris msg($lang['profnoempty'], -1); 11218b06d178Schris return false; 11228b06d178Schris } 112373dc0a89SAndreas Gohr if (!MailUtils::isValid($changes['mail']) && $auth->canDo('modMail')) { 11248b06d178Schris msg($lang['regbadmail'], -1); 11258b06d178Schris return false; 11268b06d178Schris } 11278b06d178Schris 1128bcc94b2cSAndreas Gohr $changes = array_filter($changes); 11294c21b7eeSAndreas Gohr 1130bcc94b2cSAndreas Gohr // check for unavailable capabilities 1131bcc94b2cSAndreas Gohr if (!$auth->canDo('modName')) unset($changes['name']); 1132bcc94b2cSAndreas Gohr if (!$auth->canDo('modMail')) unset($changes['mail']); 1133bcc94b2cSAndreas Gohr if (!$auth->canDo('modPass')) unset($changes['pass']); 1134bcc94b2cSAndreas Gohr 1135bcc94b2cSAndreas Gohr // anything to do? 113624870174SAndreas Gohr if ($changes === []) { 11378b06d178Schris msg($lang['profnochange'], -1); 11388b06d178Schris return false; 11398b06d178Schris } 11408b06d178Schris 11418b06d178Schris if ($conf['profileconfirm']) { 1142585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 114371422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 11448b06d178Schris return false; 11458b06d178Schris } 11468b06d178Schris } 11478b06d178Schris 114824870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$INPUT->server->str('REMOTE_USER'), &$changes])) { 1149db9faf02SPatrick Brown msg($lang['proffail'], -1); 1150db9faf02SPatrick Brown return false; 1151db9faf02SPatrick Brown } 1152db9faf02SPatrick Brown 115367efd1edSPieter Hollants if (array_key_exists('pass', $changes) && $changes['pass']) { 1154c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1155a19c9aa0SGerrit Uitslag [/* user */, $sticky, /* pass */] = auth_getCookie(); 115604369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1157585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1158c276e9e8SMarcel Pennewiss } else { 1159c276e9e8SMarcel Pennewiss // make sure the session is writable 1160c276e9e8SMarcel Pennewiss @session_start(); 1161c276e9e8SMarcel Pennewiss // invalidate session cache 1162c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1163c276e9e8SMarcel Pennewiss session_write_close(); 116432ed2b36SAndreas Gohr } 1165c276e9e8SMarcel Pennewiss 116625b2a98cSMichael Klier return true; 1167a0b5b007SChris Smith} 1168ab5d26daSAndreas Gohr 116904d68ae4SGerrit Uitslag/** 117004d68ae4SGerrit Uitslag * Delete the current logged-in user 117104d68ae4SGerrit Uitslag * 117204d68ae4SGerrit Uitslag * @return bool true on success, false on any error 117304d68ae4SGerrit Uitslag */ 1174d868eb89SAndreas Gohrfunction auth_deleteprofile() 1175d868eb89SAndreas Gohr{ 11762a7abf2dSChristopher Smith global $conf; 11772a7abf2dSChristopher Smith global $lang; 11784dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 11792a7abf2dSChristopher Smith global $auth; 11802a7abf2dSChristopher Smith /* @var Input $INPUT */ 11812a7abf2dSChristopher Smith global $INPUT; 11822a7abf2dSChristopher Smith 11832a7abf2dSChristopher Smith if (!$INPUT->post->bool('delete')) return false; 11842a7abf2dSChristopher Smith if (!checkSecurityToken()) return false; 11852a7abf2dSChristopher Smith 11862a7abf2dSChristopher Smith // action prevented or auth module disallows 11872a7abf2dSChristopher Smith if (!actionOK('profile_delete') || !$auth->canDo('delUser')) { 11882a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 11892a7abf2dSChristopher Smith return false; 11902a7abf2dSChristopher Smith } 11912a7abf2dSChristopher Smith 11922a7abf2dSChristopher Smith if (!$INPUT->post->bool('confirm_delete')) { 11932a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 11942a7abf2dSChristopher Smith return false; 11952a7abf2dSChristopher Smith } 11962a7abf2dSChristopher Smith 11972a7abf2dSChristopher Smith if ($conf['profileconfirm']) { 1198585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 11992a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 12002a7abf2dSChristopher Smith return false; 12012a7abf2dSChristopher Smith } 12022a7abf2dSChristopher Smith } 12032a7abf2dSChristopher Smith 120424870174SAndreas Gohr $deleted = []; 1205585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 120624870174SAndreas Gohr if ($auth->triggerUserMod('delete', [$deleted])) { 12072a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 12082a7abf2dSChristopher Smith auth_logoff(); 12092a7abf2dSChristopher Smith return true; 12102a7abf2dSChristopher Smith } 12112a7abf2dSChristopher Smith 12122a7abf2dSChristopher Smith return false; 12132a7abf2dSChristopher Smith} 12142a7abf2dSChristopher Smith 12158b06d178Schris/** 12168b06d178Schris * Send a new password 12178b06d178Schris * 12181d5856cfSAndreas Gohr * This function handles both phases of the password reset: 12191d5856cfSAndreas Gohr * 12201d5856cfSAndreas Gohr * - handling the first request of password reset 12211d5856cfSAndreas Gohr * - validating the password reset auth token 12221d5856cfSAndreas Gohr * 12234dc42f7fSGerrit Uitslag * @return bool true on success, false on any error 12244dc42f7fSGerrit Uitslag * @throws Exception 12254dc42f7fSGerrit Uitslag * 12264dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 12278b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 12288b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 12298b06d178Schris */ 1230d868eb89SAndreas Gohrfunction act_resendpwd() 1231d868eb89SAndreas Gohr{ 12328b06d178Schris global $lang; 12338b06d178Schris global $conf; 1234e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1235cd52f92dSchris global $auth; 1236bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1237bcc94b2cSAndreas Gohr global $INPUT; 12388b06d178Schris 12393a48618aSAnika Henke if (!actionOK('resendpwd')) { 12408b06d178Schris msg($lang['resendna'], -1); 12418b06d178Schris return false; 12428b06d178Schris } 12438b06d178Schris 1244bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 12458b06d178Schris 12461d5856cfSAndreas Gohr if ($token) { 1247cc204bbdSAndreas Gohr // we're in token phase - get user info from token 12481d5856cfSAndreas Gohr 12492401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 125079e79377SAndreas Gohr if (!file_exists($tfile)) { 12511d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1252bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 12531d5856cfSAndreas Gohr return false; 12541d5856cfSAndreas Gohr } 12558a9735e3SAndreas Gohr // token is only valid for 3 days 12568a9735e3SAndreas Gohr if ((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 12578a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1258bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 12591d5856cfSAndreas Gohr @unlink($tfile); 12608a9735e3SAndreas Gohr return false; 12618a9735e3SAndreas Gohr } 12628a9735e3SAndreas Gohr 12638b06d178Schris $user = io_readfile($tfile); 12644dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 12658b06d178Schris if (!$userinfo['mail']) { 12668b06d178Schris msg($lang['resendpwdnouser'], -1); 12678b06d178Schris return false; 12688b06d178Schris } 12698b06d178Schris 1270cc204bbdSAndreas Gohr if (!$conf['autopasswd']) { // we let the user choose a password 1271bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1272bcc94b2cSAndreas Gohr 1273cc204bbdSAndreas Gohr // password given correctly? 1274bcc94b2cSAndreas Gohr if (!$pass) return false; 1275bcc94b2cSAndreas Gohr if ($pass != $INPUT->str('passchk')) { 1276451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1277cc204bbdSAndreas Gohr return false; 1278cc204bbdSAndreas Gohr } 1279cc204bbdSAndreas Gohr 1280bcc94b2cSAndreas Gohr // change it 128124870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1282db9faf02SPatrick Brown msg($lang['proffail'], -1); 1283cc204bbdSAndreas Gohr return false; 1284cc204bbdSAndreas Gohr } 1285cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 12868a285f7fSAndreas Gohr $pass = auth_pwgen($user); 128724870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1288db9faf02SPatrick Brown msg($lang['proffail'], -1); 12898b06d178Schris return false; 12908b06d178Schris } 12918b06d178Schris 12928b06d178Schris if (auth_sendPassword($user, $pass)) { 12938b06d178Schris msg($lang['resendpwdsuccess'], 1); 12948b06d178Schris } else { 12958b06d178Schris msg($lang['regmailfail'], -1); 12968b06d178Schris } 1297cc204bbdSAndreas Gohr } 1298cc204bbdSAndreas Gohr 1299cc204bbdSAndreas Gohr @unlink($tfile); 13008b06d178Schris return true; 13011d5856cfSAndreas Gohr } else { 13021d5856cfSAndreas Gohr // we're in request phase 13031d5856cfSAndreas Gohr 1304bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 13051d5856cfSAndreas Gohr 1306bcc94b2cSAndreas Gohr if (!$INPUT->post->str('login')) { 13071d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 13081d5856cfSAndreas Gohr return false; 13091d5856cfSAndreas Gohr } else { 1310bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 13111d5856cfSAndreas Gohr } 13121d5856cfSAndreas Gohr 13134dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 13141d5856cfSAndreas Gohr if (!$userinfo['mail']) { 13151d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 13161d5856cfSAndreas Gohr return false; 13171d5856cfSAndreas Gohr } 13181d5856cfSAndreas Gohr 13191d5856cfSAndreas Gohr // generate auth token 1320483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 13212401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 132224870174SAndreas Gohr $url = wl('', ['do' => 'resendpwd', 'pwauth' => $token], true, '&'); 13231d5856cfSAndreas Gohr 13241d5856cfSAndreas Gohr io_saveFile($tfile, $user); 13251d5856cfSAndreas Gohr 13261d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 132724870174SAndreas Gohr $trep = ['FULLNAME' => $userinfo['name'], 'LOGIN' => $user, 'CONFIRM' => $url]; 13281d5856cfSAndreas Gohr 1329d7169d19SAndreas Gohr $mail = new Mailer(); 1330d7169d19SAndreas Gohr $mail->to($userinfo['name'] . ' <' . $userinfo['mail'] . '>'); 1331d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1332d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1333d7169d19SAndreas Gohr if ($mail->send()) { 13341d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 13351d5856cfSAndreas Gohr } else { 13361d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 13371d5856cfSAndreas Gohr } 13381d5856cfSAndreas Gohr return true; 13391d5856cfSAndreas Gohr } 1340ab5d26daSAndreas Gohr // never reached 13418b06d178Schris} 13428b06d178Schris 13438b06d178Schris/** 1344b0855b11Sandi * Encrypts a password using the given method and salt 1345b0855b11Sandi * 1346b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1347b0855b11Sandi * is chosen. 1348b0855b11Sandi * 13490ffe9fdaSTobias Bengfort * You can pass null as the password to create an unusable hash. 13500ffe9fdaSTobias Bengfort * 1351b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 135242ea7f44SGerrit Uitslag * 1353ab5d26daSAndreas Gohr * @param string $clear The clear text password 1354ab5d26daSAndreas Gohr * @param string $method The hashing method 1355ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1356b0855b11Sandi * @return string The crypted password 1357b0855b11Sandi */ 1358d868eb89SAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) 1359d868eb89SAndreas Gohr{ 1360b0855b11Sandi global $conf; 1361527ad715STobias Bengfort 1362527ad715STobias Bengfort if ($clear === null) { 1363b21b7935STobias Bengfort return DOKU_UNUSABLE_PASSWORD; 1364527ad715STobias Bengfort } 1365527ad715STobias Bengfort 1366b0855b11Sandi if (empty($method)) $method = $conf['passcrypt']; 136710a76f6fSfrank 13683a0a2d05SAndreas Gohr $pass = new PassHash(); 13693a0a2d05SAndreas Gohr $call = 'hash_' . $method; 1370b0855b11Sandi 13713a0a2d05SAndreas Gohr if (!method_exists($pass, $call)) { 1372b0855b11Sandi msg("Unsupported crypt method $method", -1); 13733a0a2d05SAndreas Gohr return false; 1374b0855b11Sandi } 13753a0a2d05SAndreas Gohr 13763a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1377b0855b11Sandi} 1378b0855b11Sandi 1379b0855b11Sandi/** 1380b0855b11Sandi * Verifies a cleartext password against a crypted hash 1381b0855b11Sandi * 1382ab5d26daSAndreas Gohr * @param string $clear The clear text password 1383ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1384ab5d26daSAndreas Gohr * @return bool true if both match 13854dc42f7fSGerrit Uitslag * @throws Exception 13864dc42f7fSGerrit Uitslag * 13874dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 1388b0855b11Sandi */ 1389d868eb89SAndreas Gohrfunction auth_verifyPassword($clear, $crypt) 1390d868eb89SAndreas Gohr{ 1391b21b7935STobias Bengfort if ($crypt === DOKU_UNUSABLE_PASSWORD) { 1392527ad715STobias Bengfort return false; 1393527ad715STobias Bengfort } 1394527ad715STobias Bengfort 13953a0a2d05SAndreas Gohr $pass = new PassHash(); 13963a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1397b0855b11Sandi} 1398340756e4Sandi 1399a0b5b007SChris Smith/** 1400a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1401a0b5b007SChris Smith * 1402a0b5b007SChris Smith * @param string $user username 1403a0b5b007SChris Smith * @param string $pass encrypted password 1404a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1405ab5d26daSAndreas Gohr * @return bool 1406a0b5b007SChris Smith */ 1407d868eb89SAndreas Gohrfunction auth_setCookie($user, $pass, $sticky) 1408d868eb89SAndreas Gohr{ 1409a0b5b007SChris Smith global $conf; 1410e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1411a0b5b007SChris Smith global $auth; 141279d00841SOliver Geisen global $USERINFO; 1413a0b5b007SChris Smith 14146547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 1415a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1416a0b5b007SChris Smith 1417a0b5b007SChris Smith // set cookie 1418645c0a36SAndreas Gohr $cookie = base64_encode($user) . '|' . ((int) $sticky) . '|' . base64_encode($pass); 141973ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1420c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1421bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, $cookie, [ 1422bf8392ebSAndreas Gohr 'expires' => $time, 1423bf8392ebSAndreas Gohr 'path' => $cookieDir, 14249399c87eSsplitbrain 'secure' => ($conf['securecookie'] && Ip::isSsl()), 1425bf8392ebSAndreas Gohr 'httponly' => true, 1426486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 1427bf8392ebSAndreas Gohr ]); 142855a71a16SGerrit Uitslag 1429a0b5b007SChris Smith // set session 1430a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1431234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1432a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1433a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1434a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1435ab5d26daSAndreas Gohr 1436ab5d26daSAndreas Gohr return true; 1437a0b5b007SChris Smith} 1438a0b5b007SChris Smith 1439645c0a36SAndreas Gohr/** 1440645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1441645c0a36SAndreas Gohr * 1442645c0a36SAndreas Gohr * @returns array 1443645c0a36SAndreas Gohr */ 1444d868eb89SAndreas Gohrfunction auth_getCookie() 1445d868eb89SAndreas Gohr{ 1446c66972f2SAdrian Lang if (!isset($_COOKIE[DOKU_COOKIE])) { 144724870174SAndreas Gohr return [null, null, null]; 1448c66972f2SAdrian Lang } 144924870174SAndreas Gohr [$user, $sticky, $pass] = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, ''); 1450645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1451645c0a36SAndreas Gohr $pass = base64_decode($pass); 1452645c0a36SAndreas Gohr $user = base64_decode($user); 145324870174SAndreas Gohr return [$user, $sticky, $pass]; 1454645c0a36SAndreas Gohr} 1455645c0a36SAndreas Gohr 1456e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1457