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 131cedacf2SAndreas Gohruse dokuwiki\ErrorHandler; 14cf927d07Ssplitbrainuse dokuwiki\JWT; 1524870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1696348f27SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 1796348f27SAndreas Gohruse dokuwiki\Extension\Event; 1896348f27SAndreas Gohruse dokuwiki\Extension\PluginController; 19c3cc6e05SAndreas Gohruse dokuwiki\PassHash; 2075d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 21927933f5SAndreas Gohruse phpseclib3\Crypt\AES; 22927933f5SAndreas Gohruse phpseclib3\Crypt\Common\SymmetricKey; 231cedacf2SAndreas Gohruse phpseclib3\Exception\BadDecryptionException; 24c3cc6e05SAndreas Gohr 2516905344SAndreas Gohr/** 2616905344SAndreas Gohr * Initialize the auth system. 2716905344SAndreas Gohr * 2816905344SAndreas Gohr * This function is automatically called at the end of init.php 2916905344SAndreas Gohr * 3016905344SAndreas Gohr * This used to be the main() of the auth.php 3116905344SAndreas Gohr * 3216905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 3316905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 34ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 35ab5d26daSAndreas Gohr * @return bool 3616905344SAndreas Gohr */ 37d868eb89SAndreas Gohrfunction auth_setup() 38d868eb89SAndreas Gohr{ 39742c66f8Schris global $conf; 40e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4103c4aec3Schris global $auth; 42bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 43bcc94b2cSAndreas Gohr global $INPUT; 449a9714acSDominik Eckelmann global $AUTH_ACL; 459a9714acSDominik Eckelmann global $lang; 463a7140a1SAndreas Gohr /* @var PluginController $plugin_controller */ 479c29eea5SJan Schumann global $plugin_controller; 4824870174SAndreas Gohr $AUTH_ACL = []; 4903c4aec3Schris 5016905344SAndreas Gohr if (!$conf['useacl']) return false; 5116905344SAndreas Gohr 529c29eea5SJan Schumann // try to load auth backend from plugins 539c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 549c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 55f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 569c29eea5SJan Schumann break; 579c29eea5SJan Schumann } 589c29eea5SJan Schumann } 598b06d178Schris 606547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) { 6171c734a9SGerrit Uitslag msg($lang['authtempfail'], -1); 623094e817SAndreas Gohr return false; 633094e817SAndreas Gohr } 648b06d178Schris 656416b708SMichael Hamann if ($auth->success == false) { 660f4f4adfSAndreas Gohr // degrade to unauthenticated user 67ecad51ddSAndreas Gohr $auth = null; 680f4f4adfSAndreas Gohr auth_logoff(); 69cd52f92dSchris msg($lang['authtempfail'], -1); 706416b708SMichael Hamann return false; 71d2dde4ebSMatthias Grimm } 7216905344SAndreas Gohr 7316905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 74bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 75bcc94b2cSAndreas Gohr if (!$conf['rememberme']) $INPUT->set('r', false); 76bbbd6568SAndreas Gohr 7762bf3ac0SDamien Regad // Populate Basic Auth user/password from Authorization header 7862bf3ac0SDamien Regad // Note: with FastCGI, data is in REDIRECT_HTTP_AUTHORIZATION instead of HTTP_AUTHORIZATION 7962bf3ac0SDamien Regad $header = $INPUT->server->str('HTTP_AUTHORIZATION') ?: $INPUT->server->str('REDIRECT_HTTP_AUTHORIZATION'); 8062bf3ac0SDamien Regad if (preg_match('~^Basic ([a-z\d/+]*={0,2})$~i', $header, $matches)) { 8162bf3ac0SDamien Regad $userpass = explode(':', base64_decode($matches[1])); 8224870174SAndreas Gohr [$_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']] = $userpass; 83528ddc7cSAndreas Gohr } 84528ddc7cSAndreas Gohr 851e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 8603062864SAndreas Gohr if (!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($INPUT->server->str('PHP_AUTH_USER'))) { 8703062864SAndreas Gohr $INPUT->set('u', $INPUT->server->str('PHP_AUTH_USER')); 8803062864SAndreas Gohr $INPUT->set('p', $INPUT->server->str('PHP_AUTH_PW')); 89bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 901e8c9c90SAndreas Gohr } 911e8c9c90SAndreas Gohr 92395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 9393a7873eSAndreas Gohr if (true === $auth->success) { 94395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 95395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 96f4476bd9SJan Schumann } 97191bb90aSAndreas Gohr 98455aa67eSAndreas Gohr if (!auth_tokenlogin()) { 9981e99965SPhy $ok = null; 100455aa67eSAndreas Gohr 1016547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin && $auth->canDo('external')) { 10281e99965SPhy $ok = $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 10381e99965SPhy } 10481e99965SPhy 10581e99965SPhy if ($ok === null) { 10681e99965SPhy // external trust mechanism not in place, or returns no result, 10781e99965SPhy // then attempt auth_login 10824870174SAndreas Gohr $evdata = [ 109bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 110bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 111bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 112bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 11324870174SAndreas Gohr ]; 114cbb44eabSAndreas Gohr Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 115f5cb575dSAndreas Gohr } 116455aa67eSAndreas Gohr } 117f5cb575dSAndreas Gohr 11816905344SAndreas Gohr //load ACL into a global array XXX 11975c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 120ab5d26daSAndreas Gohr 121ab5d26daSAndreas Gohr return true; 12275c93b77SAndreas Gohr} 12375c93b77SAndreas Gohr 12475c93b77SAndreas Gohr/** 12575c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12675c93b77SAndreas Gohr * 12775c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12842ea7f44SGerrit Uitslag * 129ab5d26daSAndreas Gohr * @return array 13075c93b77SAndreas Gohr */ 131d868eb89SAndreas Gohrfunction auth_loadACL() 132d868eb89SAndreas Gohr{ 13375c93b77SAndreas Gohr global $config_cascade; 134b78bf706Sromain global $USERINFO; 135585bf44eSChristopher Smith /* @var Input $INPUT */ 136585bf44eSChristopher Smith global $INPUT; 13775c93b77SAndreas Gohr 13824870174SAndreas Gohr if (!is_readable($config_cascade['acl']['default'])) return []; 13975c93b77SAndreas Gohr 14075c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 14175c93b77SAndreas Gohr 14224870174SAndreas Gohr $out = []; 1439ce556d2SAndreas Gohr foreach ($acl as $line) { 1449ce556d2SAndreas Gohr $line = trim($line); 1452401f18dSSyntaxseed if (empty($line) || ($line[0] == '#')) continue; // skip blank lines & comments 14624870174SAndreas Gohr [$id, $rest] = preg_split('/[ \t]+/', $line, 2); 14732e82180SAndreas Gohr 148443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 149ad3d68d7SChristopher Smith if (strstr($line, '%USER%')) { 150ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 151585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 152ad3d68d7SChristopher Smith 153585bf44eSChristopher Smith $id = str_replace('%USER%', cleanID($INPUT->server->str('REMOTE_USER')), $id); 154585bf44eSChristopher Smith $rest = str_replace('%USER%', auth_nameencode($INPUT->server->str('REMOTE_USER')), $rest); 155ad3d68d7SChristopher Smith } 156ad3d68d7SChristopher Smith 157ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1589ce556d2SAndreas Gohr if (strstr($line, '%GROUP%')) { 159ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 16006f34f54SPhy if (isset($USERINFO['grps'])) { 1619ce556d2SAndreas Gohr foreach ((array) $USERINFO['grps'] as $grp) { 162b78bf706Sromain $nid = str_replace('%GROUP%', cleanID($grp), $id); 16332e82180SAndreas Gohr $nrest = str_replace('%GROUP%', '@' . auth_nameencode($grp), $rest); 16432e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 165b78bf706Sromain } 16606f34f54SPhy } 16732e82180SAndreas Gohr } else { 16832e82180SAndreas Gohr $out[] = "$id\t$rest"; 169a8fe108bSGuy Brand } 17011799630Sandi } 1719ce556d2SAndreas Gohr 17232e82180SAndreas Gohr return $out; 173f3f0262cSandi} 174f3f0262cSandi 175ab5d26daSAndreas Gohr/** 176455aa67eSAndreas Gohr * Try a token login 177455aa67eSAndreas Gohr * 178455aa67eSAndreas Gohr * @return bool true if token login succeeded 179455aa67eSAndreas Gohr */ 180cf927d07Ssplitbrainfunction auth_tokenlogin() 181cf927d07Ssplitbrain{ 182455aa67eSAndreas Gohr global $USERINFO; 183455aa67eSAndreas Gohr global $INPUT; 184455aa67eSAndreas Gohr /** @var DokuWiki_Auth_Plugin $auth */ 185455aa67eSAndreas Gohr global $auth; 186455aa67eSAndreas Gohr if (!$auth) return false; 187455aa67eSAndreas Gohr 188*7ffd5bd2SAndreas Gohr // get the headers, either from Apache or from $_SERVER 1896fdb83b6SAndreas Gohr if (function_exists('getallheaders')) { 1906fdb83b6SAndreas Gohr $headers = array_change_key_case(getallheaders()); 191455aa67eSAndreas Gohr } else { 192*7ffd5bd2SAndreas Gohr $headers = []; 193*7ffd5bd2SAndreas Gohr foreach ($_SERVER as $key => $value) { 194*7ffd5bd2SAndreas Gohr if (substr($key, 0, 5) === 'HTTP_') { 195*7ffd5bd2SAndreas Gohr $headers[strtolower(substr($key, 5))] = $value; 196455aa67eSAndreas Gohr } 197*7ffd5bd2SAndreas Gohr } 198*7ffd5bd2SAndreas Gohr } 199*7ffd5bd2SAndreas Gohr 200*7ffd5bd2SAndreas Gohr // check authorization header 201*7ffd5bd2SAndreas Gohr if (isset($headers['authorization'])) { 202*7ffd5bd2SAndreas Gohr [$type, $token] = sexplode(' ', $headers['authorization'], 2); 203*7ffd5bd2SAndreas Gohr if ($type !== 'Bearer') $token = ''; // not the token we want 204*7ffd5bd2SAndreas Gohr } 205*7ffd5bd2SAndreas Gohr 206*7ffd5bd2SAndreas Gohr // check x-dokuwiki-token header 207*7ffd5bd2SAndreas Gohr if (isset($headers['x-dokuwiki-token'])) { 208*7ffd5bd2SAndreas Gohr $token = $headers['x-dokuwiki-token']; 209*7ffd5bd2SAndreas Gohr } 210*7ffd5bd2SAndreas Gohr 211*7ffd5bd2SAndreas Gohr if (empty($token)) return false; 212455aa67eSAndreas Gohr 213455aa67eSAndreas Gohr // check token 214455aa67eSAndreas Gohr try { 215cf927d07Ssplitbrain $authtoken = JWT::validate($token); 216455aa67eSAndreas Gohr } catch (Exception $e) { 217455aa67eSAndreas Gohr msg(hsc($e->getMessage()), -1); 218455aa67eSAndreas Gohr return false; 219455aa67eSAndreas Gohr } 220455aa67eSAndreas Gohr 221455aa67eSAndreas Gohr // fetch user info from backend 222455aa67eSAndreas Gohr $user = $authtoken->getUser(); 223455aa67eSAndreas Gohr $USERINFO = $auth->getUserData($user); 224455aa67eSAndreas Gohr if (!$USERINFO) return false; 225455aa67eSAndreas Gohr 226455aa67eSAndreas Gohr // the code is correct, set up user 227455aa67eSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 228455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 229455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = 'nope'; 230455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 231455aa67eSAndreas Gohr 232455aa67eSAndreas Gohr return true; 233455aa67eSAndreas Gohr} 234455aa67eSAndreas Gohr 235455aa67eSAndreas Gohr/** 236ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 237ab5d26daSAndreas Gohr * 23842ea7f44SGerrit Uitslag * @param array $evdata 239ab5d26daSAndreas Gohr * @return bool 2404dc42f7fSGerrit Uitslag * @throws Exception 241ab5d26daSAndreas Gohr */ 242d868eb89SAndreas Gohrfunction auth_login_wrapper($evdata) 243d868eb89SAndreas Gohr{ 244ab5d26daSAndreas Gohr return auth_login( 245ab5d26daSAndreas Gohr $evdata['user'], 246b5ee21aaSAdrian Lang $evdata['password'], 247b5ee21aaSAdrian Lang $evdata['sticky'], 248ab5d26daSAndreas Gohr $evdata['silent'] 249ab5d26daSAndreas Gohr ); 250b5ee21aaSAdrian Lang} 251b5ee21aaSAdrian Lang 252f3f0262cSandi/** 253f3f0262cSandi * This tries to login the user based on the sent auth credentials 254f3f0262cSandi * 255f3f0262cSandi * The authentication works like this: if a username was given 25615fae107Sandi * a new login is assumed and user/password are checked. If they 25715fae107Sandi * are correct the password is encrypted with blowfish and stored 25815fae107Sandi * together with the username in a cookie - the same info is stored 25915fae107Sandi * in the session, too. Additonally a browserID is stored in the 26015fae107Sandi * session. 26115fae107Sandi * 26215fae107Sandi * If no username was given the cookie is checked: if the username, 26315fae107Sandi * crypted password and browserID match between session and cookie 26415fae107Sandi * no further testing is done and the user is accepted 26515fae107Sandi * 26615fae107Sandi * If a cookie was found but no session info was availabe the 267136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 26815fae107Sandi * together with username rechecked by calling this function again. 269f3f0262cSandi * 270f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 271f3f0262cSandi * are set. 27215fae107Sandi * 27315fae107Sandi * @param string $user Username 27415fae107Sandi * @param string $pass Cleartext Password 27515fae107Sandi * @param bool $sticky Cookie should not expire 276f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 27715fae107Sandi * @return bool true on successful auth 2784dc42f7fSGerrit Uitslag * @throws Exception 2794dc42f7fSGerrit Uitslag * 2804dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 281f3f0262cSandi */ 282d868eb89SAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) 283d868eb89SAndreas Gohr{ 284f3f0262cSandi global $USERINFO; 285f3f0262cSandi global $conf; 286f3f0262cSandi global $lang; 287e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 288cd52f92dSchris global $auth; 289585bf44eSChristopher Smith /* @var Input $INPUT */ 290585bf44eSChristopher Smith global $INPUT; 291ab5d26daSAndreas Gohr 2926547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 293beca106aSAdrian Lang 294bbbd6568SAndreas Gohr if (!empty($user)) { 295132bdbfeSandi //usual login 2965e9e1054SAndreas Gohr if (!empty($pass) && $auth->checkPass($user, $pass)) { 297132bdbfeSandi // make logininfo globally available 298585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 29930d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 30004369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 301132bdbfeSandi return true; 302f3f0262cSandi } else { 303f3f0262cSandi //invalid credentials - log off 304f8b1e4e7SAndreas Gohr if (!$silent) { 305f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 306f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 307f8b1e4e7SAndreas Gohr } 308f3f0262cSandi auth_logoff(); 309132bdbfeSandi return false; 310f3f0262cSandi } 311f3f0262cSandi } else { 312132bdbfeSandi // read cookie information 31324870174SAndreas Gohr [$user, $sticky, $pass] = auth_getCookie(); 314132bdbfeSandi if ($user && $pass) { 315132bdbfeSandi // we got a cookie - see if we can trust it 316fa7c70ffSAdrian Lang 317fa7c70ffSAdrian Lang // get session info 3180058ae75SDamien Regad if (isset($_SESSION[DOKU_COOKIE])) { 319fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 3207d34963bSAndreas Gohr if ( 3217d34963bSAndreas Gohr isset($session) && 3227172dbc0SAndreas Gohr $auth->useSessionCache($user) && 3234c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 324132bdbfeSandi ($session['user'] == $user) && 325234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 326ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 327ab5d26daSAndreas Gohr ) { 328132bdbfeSandi // he has session, cookie and browser right - let him in 329585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 330132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 331132bdbfeSandi return true; 332132bdbfeSandi } 3330058ae75SDamien Regad } 334f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 33530d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 33604369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 337f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 338132bdbfeSandi } 339132bdbfeSandi } 340f3f0262cSandi //just to be sure 341883179a4SAndreas Gohr auth_logoff(true); 342132bdbfeSandi return false; 343f3f0262cSandi} 344132bdbfeSandi 345132bdbfeSandi/** 346136ce040Sandi * Builds a pseudo UID from browser and IP data 347132bdbfeSandi * 348132bdbfeSandi * This is neither unique nor unfakable - still it adds some 349136ce040Sandi * security. Using the first part of the IP makes sure 35080b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 35115fae107Sandi * 35215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 35315fae107Sandi * 354b13c0e1aSAdaKaleh * @return string a SHA256 sum of various browser headers 355132bdbfeSandi */ 356d868eb89SAndreas Gohrfunction auth_browseruid() 357d868eb89SAndreas Gohr{ 358585bf44eSChristopher Smith /* @var Input $INPUT */ 359585bf44eSChristopher Smith global $INPUT; 360585bf44eSChristopher Smith 3612f9daf16SAndreas Gohr $ip = clientIP(true); 362b13c0e1aSAdaKaleh // convert IP string to packed binary representation 363b13c0e1aSAdaKaleh $pip = inet_pton($ip); 364b7c67f83SAndreas Gohr 365b7c67f83SAndreas Gohr $uid = implode("\n", [ 366b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_USER_AGENT'), 367b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_ACCEPT_LANGUAGE'), 368b7c67f83SAndreas Gohr substr($pip, 0, strlen($pip) / 2), // use half of the IP address (works for both IPv4 and IPv6) 369b7c67f83SAndreas Gohr ]); 370b13c0e1aSAdaKaleh return hash('sha256', $uid); 371132bdbfeSandi} 372132bdbfeSandi 373132bdbfeSandi/** 374132bdbfeSandi * Creates a random key to encrypt the password in cookies 37515fae107Sandi * 37615fae107Sandi * This function tries to read the password for encrypting 37798407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 37815fae107Sandi * if no such file is found a random key is created and 37915fae107Sandi * and stored in this file. 38015fae107Sandi * 38132ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 38230d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 38315fae107Sandi * @return string 3844dc42f7fSGerrit Uitslag * @throws Exception 3854dc42f7fSGerrit Uitslag * 3864dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 387132bdbfeSandi */ 388d868eb89SAndreas Gohrfunction auth_cookiesalt($addsession = false, $secure = false) 389d868eb89SAndreas Gohr{ 390a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 391fe745becSMichael Große return 'test'; 392a1fe3c9cSMichael Große } 393132bdbfeSandi global $conf; 39498407a7aSandi $file = $conf['metadir'] . '/_htcookiesalt'; 39530d544a4SMichael Hamann if ($secure || !file_exists($file)) { 39630d544a4SMichael Hamann $file = $conf['metadir'] . '/_htcookiesalt2'; 39730d544a4SMichael Hamann } 398132bdbfeSandi $salt = io_readFile($file); 399132bdbfeSandi if (empty($salt)) { 40030d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 401132bdbfeSandi io_saveFile($file, $salt); 402132bdbfeSandi } 40332ed2b36SAndreas Gohr if ($addsession) { 40432ed2b36SAndreas Gohr $salt .= session_id(); 40532ed2b36SAndreas Gohr } 406132bdbfeSandi return $salt; 407f3f0262cSandi} 408f3f0262cSandi 409f3f0262cSandi/** 4107a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 411483b6238SMichael Hamann * 4127a33d2f8SNiklas Keller * @param int $length number of bytes 4137a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 4144dc42f7fSGerrit Uitslag * @throws Exception 4154dc42f7fSGerrit Uitslag * 4164dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 417483b6238SMichael Hamann */ 418d868eb89SAndreas Gohrfunction auth_randombytes($length) 419d868eb89SAndreas Gohr{ 4207a33d2f8SNiklas Keller return random_bytes($length); 421483b6238SMichael Hamann} 422483b6238SMichael Hamann 423483b6238SMichael Hamann/** 4247a33d2f8SNiklas Keller * Cryptographically secure random number generator. 425483b6238SMichael Hamann * 426483b6238SMichael Hamann * @param int $min 427483b6238SMichael Hamann * @param int $max 428483b6238SMichael Hamann * @return int 4294dc42f7fSGerrit Uitslag * @throws Exception 4304dc42f7fSGerrit Uitslag * 4314dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 432483b6238SMichael Hamann */ 433d868eb89SAndreas Gohrfunction auth_random($min, $max) 434d868eb89SAndreas Gohr{ 4357a33d2f8SNiklas Keller return random_int($min, $max); 436483b6238SMichael Hamann} 437483b6238SMichael Hamann 438483b6238SMichael Hamann/** 43904369c3eSMichael Hamann * Encrypt data using the given secret using AES 44004369c3eSMichael Hamann * 44104369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 44204369c3eSMichael Hamann * using pbkdf2. 44304369c3eSMichael Hamann * 44404369c3eSMichael Hamann * @param string $data The data that shall be encrypted 44504369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 44604369c3eSMichael Hamann * @return string The ciphertext 4474dc42f7fSGerrit Uitslag * @throws Exception 44804369c3eSMichael Hamann */ 449d868eb89SAndreas Gohrfunction auth_encrypt($data, $secret) 450d868eb89SAndreas Gohr{ 45104369c3eSMichael Hamann $iv = auth_randombytes(16); 452927933f5SAndreas Gohr $cipher = new AES('cbc'); 45347e9ed0eSAndreas Gohr $cipher->setPassword($secret, 'pbkdf2', 'sha1', 'phpseclib'); 454927933f5SAndreas Gohr $cipher->setIV($iv); 45504369c3eSMichael Hamann 4567b650cefSMichael Hamann /* 4577b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 4587b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 4597b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 4607b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 4617b650cefSMichael Hamann */ 46204369c3eSMichael Hamann return $cipher->encrypt($iv . $data); 46304369c3eSMichael Hamann} 46404369c3eSMichael Hamann 46504369c3eSMichael Hamann/** 46604369c3eSMichael Hamann * Decrypt the given AES ciphertext 46704369c3eSMichael Hamann * 46804369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 46904369c3eSMichael Hamann * 47004369c3eSMichael Hamann * @param string $ciphertext The encrypted data 47104369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 4721cedacf2SAndreas Gohr * @return string|null The decrypted data 47304369c3eSMichael Hamann */ 474d868eb89SAndreas Gohrfunction auth_decrypt($ciphertext, $secret) 475d868eb89SAndreas Gohr{ 4767b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 477927933f5SAndreas Gohr $cipher = new AES('cbc'); 47847e9ed0eSAndreas Gohr $cipher->setPassword($secret, 'pbkdf2', 'sha1', 'phpseclib'); 4797b650cefSMichael Hamann $cipher->setIV($iv); 48004369c3eSMichael Hamann 4811cedacf2SAndreas Gohr try { 4827b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 4831cedacf2SAndreas Gohr } catch (BadDecryptionException $e) { 4841cedacf2SAndreas Gohr ErrorHandler::logException($e); 4851cedacf2SAndreas Gohr return null; 4861cedacf2SAndreas Gohr } 48704369c3eSMichael Hamann} 48804369c3eSMichael Hamann 48904369c3eSMichael Hamann/** 490883179a4SAndreas Gohr * Log out the current user 491883179a4SAndreas Gohr * 492f3f0262cSandi * This clears all authentication data and thus log the user 493883179a4SAndreas Gohr * off. It also clears session data. 49415fae107Sandi * 49515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 49642ea7f44SGerrit Uitslag * 497883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 498f3f0262cSandi */ 499d868eb89SAndreas Gohrfunction auth_logoff($keepbc = false) 500d868eb89SAndreas Gohr{ 501f3f0262cSandi global $conf; 502f3f0262cSandi global $USERINFO; 503e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 5045298a619SAndreas Gohr global $auth; 505585bf44eSChristopher Smith /* @var Input $INPUT */ 506585bf44eSChristopher Smith global $INPUT; 50737065e65Sandi 508d4869846SAndreas Gohr // make sure the session is writable (it usually is) 509e9621d07SAndreas Gohr @session_start(); 510e9621d07SAndreas Gohr 511e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 512e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 513e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 514e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 515e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 516e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 517883179a4SAndreas Gohr if (!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 518e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 519585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 520132bdbfeSandi $USERINFO = null; //FIXME 521f5c6743cSAndreas Gohr 52273ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 523bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, '', [ 524bf8392ebSAndreas Gohr 'expires' => time() - 600000, 525bf8392ebSAndreas Gohr 'path' => $cookieDir, 526bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 527bf8392ebSAndreas Gohr 'httponly' => true, 528486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 529bf8392ebSAndreas Gohr ]); 5305298a619SAndreas Gohr 5316547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin) { 5326547cfc7SGerrit Uitslag $auth->logOff(); 5336547cfc7SGerrit Uitslag } 534f3f0262cSandi} 535f3f0262cSandi 536f3f0262cSandi/** 537f8cc712eSAndreas Gohr * Check if a user is a manager 538f8cc712eSAndreas Gohr * 539f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 540f8cc712eSAndreas Gohr * user. 541f8cc712eSAndreas Gohr * 542f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 543f8cc712eSAndreas Gohr * 544ab5d26daSAndreas Gohr * @param string $user Username 545ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 546ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 54710396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 548ab5d26daSAndreas Gohr * @return bool 54996348f27SAndreas Gohr * @see auth_isadmin 55096348f27SAndreas Gohr * 55196348f27SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 552f8cc712eSAndreas Gohr */ 553d868eb89SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache = false) 554d868eb89SAndreas Gohr{ 555f8cc712eSAndreas Gohr global $conf; 556f8cc712eSAndreas Gohr global $USERINFO; 557e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 558d752aedeSAndreas Gohr global $auth; 559585bf44eSChristopher Smith /* @var Input $INPUT */ 560585bf44eSChristopher Smith global $INPUT; 561585bf44eSChristopher Smith 562f8cc712eSAndreas Gohr 5636547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 564c66972f2SAdrian Lang if (is_null($user)) { 565585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) { 566c66972f2SAdrian Lang return false; 567c66972f2SAdrian Lang } else { 568585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 569c66972f2SAdrian Lang } 570c66972f2SAdrian Lang } 571d6dc956fSAndreas Gohr if (is_null($groups)) { 5721525c228SAnna Dabrowska // checking the logged in user, or another one? 5731525c228SAnna Dabrowska if ($USERINFO && $user === $INPUT->server->str('REMOTE_USER')) { 5741525c228SAnna Dabrowska $groups = (array) $USERINFO['grps']; 57566b108d6SAnna Dabrowska } else { 5766cf7b139SAndreas Gohr $groups = $auth->getUserData($user); 5776cf7b139SAndreas Gohr $groups = $groups ? $groups['grps'] : []; 57866b108d6SAnna Dabrowska } 579e259aa79SAndreas Gohr } 580e259aa79SAndreas Gohr 58196348f27SAndreas Gohr // prefer cached result 58296348f27SAndreas Gohr static $cache = []; 58310396f77SAndreas Gohr $cachekey = serialize([$user, $adminonly, $groups]); 58496348f27SAndreas Gohr if (!isset($cache[$cachekey]) || $recache) { 585d6dc956fSAndreas Gohr // check superuser match 58696348f27SAndreas Gohr $ok = auth_isMember($conf['superuser'], $user, $groups); 58700ce12daSChris Smith 58896348f27SAndreas Gohr // check managers 58996348f27SAndreas Gohr if (!$ok && !$adminonly) { 59096348f27SAndreas Gohr $ok = auth_isMember($conf['manager'], $user, $groups); 59196348f27SAndreas Gohr } 59296348f27SAndreas Gohr 59396348f27SAndreas Gohr $cache[$cachekey] = $ok; 59496348f27SAndreas Gohr } 59596348f27SAndreas Gohr 59696348f27SAndreas Gohr return $cache[$cachekey]; 597f8cc712eSAndreas Gohr} 598f8cc712eSAndreas Gohr 599f8cc712eSAndreas Gohr/** 600f8cc712eSAndreas Gohr * Check if a user is admin 601f8cc712eSAndreas Gohr * 602f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 603f8cc712eSAndreas Gohr * 604f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 605f8cc712eSAndreas Gohr * 60696348f27SAndreas Gohr * @param string $user Username 60796348f27SAndreas Gohr * @param array $groups List of groups the user is in 60810396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 60996348f27SAndreas Gohr * @return bool 610f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 611ab5d26daSAndreas Gohr * @see auth_ismanager() 61242ea7f44SGerrit Uitslag * 613f8cc712eSAndreas Gohr */ 614d868eb89SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache = false) 615d868eb89SAndreas Gohr{ 61696348f27SAndreas Gohr return auth_ismanager($user, $groups, true, $recache); 617f8cc712eSAndreas Gohr} 618f8cc712eSAndreas Gohr 619d6dc956fSAndreas Gohr/** 620d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 621d6dc956fSAndreas Gohr * users and groups to determine membership status 622d6dc956fSAndreas Gohr * 623d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 624d6dc956fSAndreas Gohr * 62542ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 62642ea7f44SGerrit Uitslag * @param string $user user to match against 62742ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 6285446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 629d6dc956fSAndreas Gohr */ 630d868eb89SAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) 631d868eb89SAndreas Gohr{ 632e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 633d6dc956fSAndreas Gohr global $auth; 6346547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 635d6dc956fSAndreas Gohr 636d6dc956fSAndreas Gohr // clean user and groups 6374f56ecbfSAdrian Lang if (!$auth->isCaseSensitive()) { 63824870174SAndreas Gohr $user = PhpString::strtolower($user); 63924870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 640d6dc956fSAndreas Gohr } 641d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 64224870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 643d6dc956fSAndreas Gohr 644d6dc956fSAndreas Gohr // extract the memberlist 645d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 646d6dc956fSAndreas Gohr $members = array_map('trim', $members); 647d6dc956fSAndreas Gohr $members = array_unique($members); 648d6dc956fSAndreas Gohr $members = array_filter($members); 649d6dc956fSAndreas Gohr 650d6dc956fSAndreas Gohr // compare cleaned values 651d6dc956fSAndreas Gohr foreach ($members as $member) { 652e5204a12SJurgen Hart if ($member == '@ALL') return true; 65324870174SAndreas Gohr if (!$auth->isCaseSensitive()) $member = PhpString::strtolower($member); 654d6dc956fSAndreas Gohr if ($member[0] == '@') { 655d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 656d6dc956fSAndreas Gohr if (in_array($member, $groups)) return true; 657d6dc956fSAndreas Gohr } else { 658d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 659d6dc956fSAndreas Gohr if ($member == $user) return true; 660d6dc956fSAndreas Gohr } 661d6dc956fSAndreas Gohr } 662d6dc956fSAndreas Gohr 663d6dc956fSAndreas Gohr // still here? not a member! 664d6dc956fSAndreas Gohr return false; 665d6dc956fSAndreas Gohr} 666d6dc956fSAndreas Gohr 667f8cc712eSAndreas Gohr/** 66815fae107Sandi * Convinience function for auth_aclcheck() 66915fae107Sandi * 67015fae107Sandi * This checks the permissions for the current user 67115fae107Sandi * 67215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 67315fae107Sandi * 6741698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 67515fae107Sandi * @return int permission level 676f3f0262cSandi */ 677d868eb89SAndreas Gohrfunction auth_quickaclcheck($id) 678d868eb89SAndreas Gohr{ 679f3f0262cSandi global $conf; 680f3f0262cSandi global $USERINFO; 681585bf44eSChristopher Smith /* @var Input $INPUT */ 682585bf44eSChristopher Smith global $INPUT; 683f3f0262cSandi # if no ACL is used always return upload rights 684f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 68524870174SAndreas Gohr return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), is_array($USERINFO) ? $USERINFO['grps'] : []); 686f3f0262cSandi} 687f3f0262cSandi 688f3f0262cSandi/** 689c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 69015fae107Sandi * 69115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 69242ea7f44SGerrit Uitslag * 693c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6941698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 69515fae107Sandi * @param string $user Username 6963272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 69715fae107Sandi * @return int permission level 698f3f0262cSandi */ 699d868eb89SAndreas Gohrfunction auth_aclcheck($id, $user, $groups) 700d868eb89SAndreas Gohr{ 70124870174SAndreas Gohr $data = [ 702bf8f8509SAndreas Gohr 'id' => $id ?? '', 703c17acc9fSAndreas Gohr 'user' => $user, 704c17acc9fSAndreas Gohr 'groups' => $groups 70524870174SAndreas Gohr ]; 706c17acc9fSAndreas Gohr 707cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 708c17acc9fSAndreas Gohr} 709c17acc9fSAndreas Gohr 710c17acc9fSAndreas Gohr/** 711c17acc9fSAndreas Gohr * default ACL check method 712c17acc9fSAndreas Gohr * 713c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 714c17acc9fSAndreas Gohr * 715c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 71642ea7f44SGerrit Uitslag * 717c17acc9fSAndreas Gohr * @param array $data event data 718c17acc9fSAndreas Gohr * @return int permission level 719c17acc9fSAndreas Gohr */ 720d868eb89SAndreas Gohrfunction auth_aclcheck_cb($data) 721d868eb89SAndreas Gohr{ 722c17acc9fSAndreas Gohr $id =& $data['id']; 723c17acc9fSAndreas Gohr $user =& $data['user']; 724c17acc9fSAndreas Gohr $groups =& $data['groups']; 725c17acc9fSAndreas Gohr 726f3f0262cSandi global $conf; 727f3f0262cSandi global $AUTH_ACL; 728e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 729d752aedeSAndreas Gohr global $auth; 730f3f0262cSandi 73185d03f68SAndreas Gohr // if no ACL is used always return upload rights 732f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 7336547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return AUTH_NONE; 734bf8f8509SAndreas Gohr if (!is_array($AUTH_ACL)) return AUTH_NONE; 735f3f0262cSandi 736074cf26bSandi //make sure groups is an array 73724870174SAndreas Gohr if (!is_array($groups)) $groups = []; 738074cf26bSandi 73985d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 740ab5d26daSAndreas Gohr if (auth_isadmin($user, $groups)) { 741ab5d26daSAndreas Gohr return AUTH_ADMIN; 742ab5d26daSAndreas Gohr } 74385d03f68SAndreas Gohr 744eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive()) { 74524870174SAndreas Gohr $user = PhpString::strtolower($user); 74624870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 747eb3ce0d5SKazutaka Miyasaka } 74837ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 74924870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 75085d03f68SAndreas Gohr 7516c2bb100SAndreas Gohr //prepend groups with @ and nameencode 75237ff2261SSascha Klopp foreach ($groups as &$group) { 75337ff2261SSascha Klopp $group = '@' . auth_nameencode($group); 75410a76f6fSfrank } 75510a76f6fSfrank 756f3f0262cSandi $ns = getNS($id); 757f3f0262cSandi $perm = -1; 758f3f0262cSandi 759f3f0262cSandi //add ALL group 760f3f0262cSandi $groups[] = '@ALL'; 76137ff2261SSascha Klopp 762f3f0262cSandi //add User 76334aeb4afSAndreas Gohr if ($user) $groups[] = $user; 764f3f0262cSandi 765f3f0262cSandi //check exact match first 76621c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($id, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 767f3f0262cSandi if (count($matches)) { 768f3f0262cSandi foreach ($matches as $match) { 769f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 77021c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 771eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 77224870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 773eb3ce0d5SKazutaka Miyasaka } 77448d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 77548d7b7a6SDominik Eckelmann continue; 77648d7b7a6SDominik Eckelmann } 7778ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 778f3f0262cSandi if ($acl[2] > $perm) { 779f3f0262cSandi $perm = $acl[2]; 780f3f0262cSandi } 781f3f0262cSandi } 782f3f0262cSandi if ($perm > -1) { 783f3f0262cSandi //we had a match - return it 784def492a2SGuillaume Turri return (int) $perm; 785f3f0262cSandi } 786f3f0262cSandi } 787f3f0262cSandi 788f3f0262cSandi //still here? do the namespace checks 789f3f0262cSandi if ($ns) { 7903e304b55SMichael Hamann $path = $ns . ':*'; 791f3f0262cSandi } else { 7923e304b55SMichael Hamann $path = '*'; //root document 793f3f0262cSandi } 794f3f0262cSandi 795f3f0262cSandi do { 79621c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($path, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 797f3f0262cSandi if (count($matches)) { 798f3f0262cSandi foreach ($matches as $match) { 799f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 80021c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 801eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 80224870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 803eb3ce0d5SKazutaka Miyasaka } 80448d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 80548d7b7a6SDominik Eckelmann continue; 80648d7b7a6SDominik Eckelmann } 8078ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 808f3f0262cSandi if ($acl[2] > $perm) { 809f3f0262cSandi $perm = $acl[2]; 810f3f0262cSandi } 811f3f0262cSandi } 812f3f0262cSandi //we had a match - return it 81348d7b7a6SDominik Eckelmann if ($perm != -1) { 814def492a2SGuillaume Turri return (int) $perm; 815f3f0262cSandi } 81648d7b7a6SDominik Eckelmann } 817f3f0262cSandi //get next higher namespace 818f3f0262cSandi $ns = getNS($ns); 819f3f0262cSandi 8203e304b55SMichael Hamann if ($path != '*') { 8213e304b55SMichael Hamann $path = $ns . ':*'; 8223e304b55SMichael Hamann if ($path == ':*') $path = '*'; 823f3f0262cSandi } else { 824f3f0262cSandi //we did this already 825f3f0262cSandi //looks like there is something wrong with the ACL 826f3f0262cSandi //break here 827d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 828d5ce66f6SAndreas Gohr return AUTH_NONE; 829f3f0262cSandi } 830f3f0262cSandi } while (1); //this should never loop endless 831ab5d26daSAndreas Gohr return AUTH_NONE; 832f3f0262cSandi} 833f3f0262cSandi 834f3f0262cSandi/** 8356c2bb100SAndreas Gohr * Encode ASCII special chars 8366c2bb100SAndreas Gohr * 8376c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 8386c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 8396c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 8406c2bb100SAndreas Gohr * urlencoding!). 8416c2bb100SAndreas Gohr * 8426c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 8436c2bb100SAndreas Gohr * 8446c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 8456c2bb100SAndreas Gohr * @see rawurldecode() 84642ea7f44SGerrit Uitslag * 84742ea7f44SGerrit Uitslag * @param string $name 84842ea7f44SGerrit Uitslag * @param bool $skip_group 84942ea7f44SGerrit Uitslag * @return string 8506c2bb100SAndreas Gohr */ 851d868eb89SAndreas Gohrfunction auth_nameencode($name, $skip_group = false) 852d868eb89SAndreas Gohr{ 853a424cd8eSchris global $cache_authname; 854a424cd8eSchris $cache =& $cache_authname; 85531784267SAndreas Gohr $name = (string) $name; 856a424cd8eSchris 85780601d26SAndreas Gohr // never encode wildcard FS#1955 85880601d26SAndreas Gohr if ($name == '%USER%') return $name; 859b78bf706Sromain if ($name == '%GROUP%') return $name; 86080601d26SAndreas Gohr 861a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 8622401f18dSSyntaxseed if ($skip_group && $name[0] == '@') { 86330f6faf0SChristopher Smith $cache[$name][$skip_group] = '@' . preg_replace_callback( 86430f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 865dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 866dccd6b2bSAndreas Gohr substr($name, 1) 867ab5d26daSAndreas Gohr ); 868e838fc2eSAndreas Gohr } else { 86930f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 87030f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 871dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 872dccd6b2bSAndreas Gohr $name 873ab5d26daSAndreas Gohr ); 874e838fc2eSAndreas Gohr } 8756c2bb100SAndreas Gohr } 8766c2bb100SAndreas Gohr 877a424cd8eSchris return $cache[$name][$skip_group]; 878a424cd8eSchris} 879a424cd8eSchris 88004d68ae4SGerrit Uitslag/** 88104d68ae4SGerrit Uitslag * callback encodes the matches 88204d68ae4SGerrit Uitslag * 88304d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 88404d68ae4SGerrit Uitslag * @return string 88504d68ae4SGerrit Uitslag */ 886d868eb89SAndreas Gohrfunction auth_nameencode_callback($matches) 887d868eb89SAndreas Gohr{ 88830f6faf0SChristopher Smith return '%' . dechex(ord(substr($matches[1], -1))); 88930f6faf0SChristopher Smith} 89030f6faf0SChristopher Smith 8916c2bb100SAndreas Gohr/** 892f3f0262cSandi * Create a pronouncable password 893f3f0262cSandi * 8948a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8958a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8968a285f7fSAndreas Gohr * 8974dc42f7fSGerrit Uitslag * @param string $foruser username for which the password is generated 8984dc42f7fSGerrit Uitslag * @return string pronouncable password 8994dc42f7fSGerrit Uitslag * @throws Exception 9004dc42f7fSGerrit Uitslag * 90115fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 9028a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 90315fae107Sandi * 9044dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 905f3f0262cSandi */ 906d868eb89SAndreas Gohrfunction auth_pwgen($foruser = '') 907d868eb89SAndreas Gohr{ 90824870174SAndreas Gohr $data = [ 909d628dcf3SAndreas Gohr 'password' => '', 910d628dcf3SAndreas Gohr 'foruser' => $foruser 91124870174SAndreas Gohr ]; 9128a285f7fSAndreas Gohr 913e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 9148a285f7fSAndreas Gohr if ($evt->advise_before(true)) { 915f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 916f3f0262cSandi $v = 'aeiou'; //vowels 917f3f0262cSandi $a = $c . $v; //both 918987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 919f3f0262cSandi 920987c8d26SAndreas Gohr //use thre syllables... 921987c8d26SAndreas Gohr for ($i = 0; $i < 3; $i++) { 922483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 923483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 924483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 925f3f0262cSandi } 926987c8d26SAndreas Gohr //... and add a nice number and special 92743f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)] . auth_random(10, 99); 9288a285f7fSAndreas Gohr } 9298a285f7fSAndreas Gohr $evt->advise_after(); 930f3f0262cSandi 9318a285f7fSAndreas Gohr return $data['password']; 932f3f0262cSandi} 933f3f0262cSandi 934f3f0262cSandi/** 935f3f0262cSandi * Sends a password to the given user 936f3f0262cSandi * 93715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 93842ea7f44SGerrit Uitslag * 939ab5d26daSAndreas Gohr * @param string $user Login name of the user 940ab5d26daSAndreas Gohr * @param string $password The new password in clear text 94115fae107Sandi * @return bool true on success 942f3f0262cSandi */ 943d868eb89SAndreas Gohrfunction auth_sendPassword($user, $password) 944d868eb89SAndreas Gohr{ 945f3f0262cSandi global $lang; 946e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 947cd52f92dSchris global $auth; 9486547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 949cd52f92dSchris 950d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 9514dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 952f3f0262cSandi 95387ddda95Sandi if (!$userinfo['mail']) return false; 954f3f0262cSandi 955f3f0262cSandi $text = rawLocale('password'); 95624870174SAndreas Gohr $trep = [ 957d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 958d7169d19SAndreas Gohr 'LOGIN' => $user, 959d7169d19SAndreas Gohr 'PASSWORD' => $password 96024870174SAndreas Gohr ]; 961f3f0262cSandi 962d7169d19SAndreas Gohr $mail = new Mailer(); 963102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']) . ' <' . $userinfo['mail'] . '>'); 964d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 965d7169d19SAndreas Gohr $mail->setBody($text, $trep); 966d7169d19SAndreas Gohr return $mail->send(); 967f3f0262cSandi} 968f3f0262cSandi 969f3f0262cSandi/** 97015fae107Sandi * Register a new user 971f3f0262cSandi * 97215fae107Sandi * This registers a new user - Data is read directly from $_POST 97315fae107Sandi * 97415fae107Sandi * @return bool true on success, false on any error 9754dc42f7fSGerrit Uitslag * @throws Exception 9764dc42f7fSGerrit Uitslag * 9774dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 978f3f0262cSandi */ 979d868eb89SAndreas Gohrfunction register() 980d868eb89SAndreas Gohr{ 981f3f0262cSandi global $lang; 982eb5d07e4Sjan global $conf; 9834dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 984cd52f92dSchris global $auth; 98564273335SAndreas Gohr global $INPUT; 986f3f0262cSandi 98764273335SAndreas Gohr if (!$INPUT->post->bool('save')) return false; 9883a48618aSAnika Henke if (!actionOK('register')) return false; 989640145a5Sandi 99064273335SAndreas Gohr // gather input 99164273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 99264273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 99364273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 99464273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 99564273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 996d752aedeSAndreas Gohr 99764273335SAndreas Gohr if (empty($login) || empty($fullname) || empty($email)) { 998f3f0262cSandi msg($lang['regmissing'], -1); 999f3f0262cSandi return false; 1000f3f0262cSandi } 1001f3f0262cSandi 1002cab2716aSmatthias.grimm if ($conf['autopasswd']) { 10038a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 100464273335SAndreas Gohr } elseif (empty($pass) || empty($passchk)) { 1005bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 1006cab2716aSmatthias.grimm return false; 100764273335SAndreas Gohr } elseif ($pass != $passchk) { 1008bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 1009cab2716aSmatthias.grimm return false; 1010cab2716aSmatthias.grimm } 1011cab2716aSmatthias.grimm 1012f3f0262cSandi //check mail 101364273335SAndreas Gohr if (!mail_isvalid($email)) { 1014f3f0262cSandi msg($lang['regbadmail'], -1); 1015f3f0262cSandi return false; 1016f3f0262cSandi } 1017f3f0262cSandi 1018f3f0262cSandi //okay try to create the user 101924870174SAndreas Gohr if (!$auth->triggerUserMod('create', [$login, $pass, $fullname, $email])) { 1020db9faf02SPatrick Brown msg($lang['regfail'], -1); 1021f3f0262cSandi return false; 1022f3f0262cSandi } 1023f3f0262cSandi 1024790b7720SAndreas Gohr // send notification about the new user 102575d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 102675d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 102702a498e7Schris 1028790b7720SAndreas Gohr // are we done? 1029cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 1030cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 1031cab2716aSmatthias.grimm return true; 1032cab2716aSmatthias.grimm } 1033cab2716aSmatthias.grimm 1034790b7720SAndreas Gohr // autogenerated password? then send password to user 103564273335SAndreas Gohr if (auth_sendPassword($login, $pass)) { 1036f3f0262cSandi msg($lang['regsuccess'], 1); 1037f3f0262cSandi return true; 1038f3f0262cSandi } else { 1039f3f0262cSandi msg($lang['regmailfail'], -1); 1040f3f0262cSandi return false; 1041f3f0262cSandi } 1042f3f0262cSandi} 1043f3f0262cSandi 104410a76f6fSfrank/** 10458b06d178Schris * Update user profile 10468b06d178Schris * 10474dc42f7fSGerrit Uitslag * @throws Exception 10484dc42f7fSGerrit Uitslag * 10498b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 10508b06d178Schris */ 1051d868eb89SAndreas Gohrfunction updateprofile() 1052d868eb89SAndreas Gohr{ 10538b06d178Schris global $conf; 10548b06d178Schris global $lang; 1055e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1056cd52f92dSchris global $auth; 1057bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1058bcc94b2cSAndreas Gohr global $INPUT; 10598b06d178Schris 1060bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 10611b2a85e8SAndreas Gohr if (!checkSecurityToken()) return false; 10628b06d178Schris 10633a48618aSAnika Henke if (!actionOK('profile')) { 10648b06d178Schris msg($lang['profna'], -1); 10658b06d178Schris return false; 10668b06d178Schris } 10678b06d178Schris 106824870174SAndreas Gohr $changes = []; 1069bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 1070bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 1071bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 1072bcc94b2cSAndreas Gohr 1073bcc94b2cSAndreas Gohr // check misspelled passwords 1074bcc94b2cSAndreas Gohr if ($changes['pass'] != $INPUT->post->str('passchk')) { 1075bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10768b06d178Schris return false; 10778b06d178Schris } 10788b06d178Schris 10798b06d178Schris // clean fullname and email 1080bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1081bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10828b06d178Schris 1083bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 10847d34963bSAndreas Gohr if ( 10857d34963bSAndreas Gohr (empty($changes['name']) && $auth->canDo('modName')) || 1086bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1087ab5d26daSAndreas Gohr ) { 10888b06d178Schris msg($lang['profnoempty'], -1); 10898b06d178Schris return false; 10908b06d178Schris } 1091bcc94b2cSAndreas Gohr if (!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10928b06d178Schris msg($lang['regbadmail'], -1); 10938b06d178Schris return false; 10948b06d178Schris } 10958b06d178Schris 1096bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10974c21b7eeSAndreas Gohr 1098bcc94b2cSAndreas Gohr // check for unavailable capabilities 1099bcc94b2cSAndreas Gohr if (!$auth->canDo('modName')) unset($changes['name']); 1100bcc94b2cSAndreas Gohr if (!$auth->canDo('modMail')) unset($changes['mail']); 1101bcc94b2cSAndreas Gohr if (!$auth->canDo('modPass')) unset($changes['pass']); 1102bcc94b2cSAndreas Gohr 1103bcc94b2cSAndreas Gohr // anything to do? 110424870174SAndreas Gohr if ($changes === []) { 11058b06d178Schris msg($lang['profnochange'], -1); 11068b06d178Schris return false; 11078b06d178Schris } 11088b06d178Schris 11098b06d178Schris if ($conf['profileconfirm']) { 1110585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 111171422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 11128b06d178Schris return false; 11138b06d178Schris } 11148b06d178Schris } 11158b06d178Schris 111624870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$INPUT->server->str('REMOTE_USER'), &$changes])) { 1117db9faf02SPatrick Brown msg($lang['proffail'], -1); 1118db9faf02SPatrick Brown return false; 1119db9faf02SPatrick Brown } 1120db9faf02SPatrick Brown 112167efd1edSPieter Hollants if (array_key_exists('pass', $changes) && $changes['pass']) { 1122c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1123a19c9aa0SGerrit Uitslag [/* user */, $sticky, /* pass */] = auth_getCookie(); 112404369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1125585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1126c276e9e8SMarcel Pennewiss } else { 1127c276e9e8SMarcel Pennewiss // make sure the session is writable 1128c276e9e8SMarcel Pennewiss @session_start(); 1129c276e9e8SMarcel Pennewiss // invalidate session cache 1130c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1131c276e9e8SMarcel Pennewiss session_write_close(); 113232ed2b36SAndreas Gohr } 1133c276e9e8SMarcel Pennewiss 113425b2a98cSMichael Klier return true; 1135a0b5b007SChris Smith} 1136ab5d26daSAndreas Gohr 113704d68ae4SGerrit Uitslag/** 113804d68ae4SGerrit Uitslag * Delete the current logged-in user 113904d68ae4SGerrit Uitslag * 114004d68ae4SGerrit Uitslag * @return bool true on success, false on any error 114104d68ae4SGerrit Uitslag */ 1142d868eb89SAndreas Gohrfunction auth_deleteprofile() 1143d868eb89SAndreas Gohr{ 11442a7abf2dSChristopher Smith global $conf; 11452a7abf2dSChristopher Smith global $lang; 11464dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 11472a7abf2dSChristopher Smith global $auth; 11482a7abf2dSChristopher Smith /* @var Input $INPUT */ 11492a7abf2dSChristopher Smith global $INPUT; 11502a7abf2dSChristopher Smith 11512a7abf2dSChristopher Smith if (!$INPUT->post->bool('delete')) return false; 11522a7abf2dSChristopher Smith if (!checkSecurityToken()) return false; 11532a7abf2dSChristopher Smith 11542a7abf2dSChristopher Smith // action prevented or auth module disallows 11552a7abf2dSChristopher Smith if (!actionOK('profile_delete') || !$auth->canDo('delUser')) { 11562a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 11572a7abf2dSChristopher Smith return false; 11582a7abf2dSChristopher Smith } 11592a7abf2dSChristopher Smith 11602a7abf2dSChristopher Smith if (!$INPUT->post->bool('confirm_delete')) { 11612a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 11622a7abf2dSChristopher Smith return false; 11632a7abf2dSChristopher Smith } 11642a7abf2dSChristopher Smith 11652a7abf2dSChristopher Smith if ($conf['profileconfirm']) { 1166585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 11672a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 11682a7abf2dSChristopher Smith return false; 11692a7abf2dSChristopher Smith } 11702a7abf2dSChristopher Smith } 11712a7abf2dSChristopher Smith 117224870174SAndreas Gohr $deleted = []; 1173585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 117424870174SAndreas Gohr if ($auth->triggerUserMod('delete', [$deleted])) { 11752a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11762a7abf2dSChristopher Smith auth_logoff(); 11772a7abf2dSChristopher Smith return true; 11782a7abf2dSChristopher Smith } 11792a7abf2dSChristopher Smith 11802a7abf2dSChristopher Smith return false; 11812a7abf2dSChristopher Smith} 11822a7abf2dSChristopher Smith 11838b06d178Schris/** 11848b06d178Schris * Send a new password 11858b06d178Schris * 11861d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11871d5856cfSAndreas Gohr * 11881d5856cfSAndreas Gohr * - handling the first request of password reset 11891d5856cfSAndreas Gohr * - validating the password reset auth token 11901d5856cfSAndreas Gohr * 11914dc42f7fSGerrit Uitslag * @return bool true on success, false on any error 11924dc42f7fSGerrit Uitslag * @throws Exception 11934dc42f7fSGerrit Uitslag * 11944dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 11958b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11968b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11978b06d178Schris */ 1198d868eb89SAndreas Gohrfunction act_resendpwd() 1199d868eb89SAndreas Gohr{ 12008b06d178Schris global $lang; 12018b06d178Schris global $conf; 1202e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1203cd52f92dSchris global $auth; 1204bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1205bcc94b2cSAndreas Gohr global $INPUT; 12068b06d178Schris 12073a48618aSAnika Henke if (!actionOK('resendpwd')) { 12088b06d178Schris msg($lang['resendna'], -1); 12098b06d178Schris return false; 12108b06d178Schris } 12118b06d178Schris 1212bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 12138b06d178Schris 12141d5856cfSAndreas Gohr if ($token) { 1215cc204bbdSAndreas Gohr // we're in token phase - get user info from token 12161d5856cfSAndreas Gohr 12172401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 121879e79377SAndreas Gohr if (!file_exists($tfile)) { 12191d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1220bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 12211d5856cfSAndreas Gohr return false; 12221d5856cfSAndreas Gohr } 12238a9735e3SAndreas Gohr // token is only valid for 3 days 12248a9735e3SAndreas Gohr if ((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 12258a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1226bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 12271d5856cfSAndreas Gohr @unlink($tfile); 12288a9735e3SAndreas Gohr return false; 12298a9735e3SAndreas Gohr } 12308a9735e3SAndreas Gohr 12318b06d178Schris $user = io_readfile($tfile); 12324dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 12338b06d178Schris if (!$userinfo['mail']) { 12348b06d178Schris msg($lang['resendpwdnouser'], -1); 12358b06d178Schris return false; 12368b06d178Schris } 12378b06d178Schris 1238cc204bbdSAndreas Gohr if (!$conf['autopasswd']) { // we let the user choose a password 1239bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1240bcc94b2cSAndreas Gohr 1241cc204bbdSAndreas Gohr // password given correctly? 1242bcc94b2cSAndreas Gohr if (!$pass) return false; 1243bcc94b2cSAndreas Gohr if ($pass != $INPUT->str('passchk')) { 1244451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1245cc204bbdSAndreas Gohr return false; 1246cc204bbdSAndreas Gohr } 1247cc204bbdSAndreas Gohr 1248bcc94b2cSAndreas Gohr // change it 124924870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1250db9faf02SPatrick Brown msg($lang['proffail'], -1); 1251cc204bbdSAndreas Gohr return false; 1252cc204bbdSAndreas Gohr } 1253cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 12548a285f7fSAndreas Gohr $pass = auth_pwgen($user); 125524870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1256db9faf02SPatrick Brown msg($lang['proffail'], -1); 12578b06d178Schris return false; 12588b06d178Schris } 12598b06d178Schris 12608b06d178Schris if (auth_sendPassword($user, $pass)) { 12618b06d178Schris msg($lang['resendpwdsuccess'], 1); 12628b06d178Schris } else { 12638b06d178Schris msg($lang['regmailfail'], -1); 12648b06d178Schris } 1265cc204bbdSAndreas Gohr } 1266cc204bbdSAndreas Gohr 1267cc204bbdSAndreas Gohr @unlink($tfile); 12688b06d178Schris return true; 12691d5856cfSAndreas Gohr } else { 12701d5856cfSAndreas Gohr // we're in request phase 12711d5856cfSAndreas Gohr 1272bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 12731d5856cfSAndreas Gohr 1274bcc94b2cSAndreas Gohr if (!$INPUT->post->str('login')) { 12751d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12761d5856cfSAndreas Gohr return false; 12771d5856cfSAndreas Gohr } else { 1278bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12791d5856cfSAndreas Gohr } 12801d5856cfSAndreas Gohr 12814dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 12821d5856cfSAndreas Gohr if (!$userinfo['mail']) { 12831d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12841d5856cfSAndreas Gohr return false; 12851d5856cfSAndreas Gohr } 12861d5856cfSAndreas Gohr 12871d5856cfSAndreas Gohr // generate auth token 1288483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12892401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 129024870174SAndreas Gohr $url = wl('', ['do' => 'resendpwd', 'pwauth' => $token], true, '&'); 12911d5856cfSAndreas Gohr 12921d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12931d5856cfSAndreas Gohr 12941d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 129524870174SAndreas Gohr $trep = ['FULLNAME' => $userinfo['name'], 'LOGIN' => $user, 'CONFIRM' => $url]; 12961d5856cfSAndreas Gohr 1297d7169d19SAndreas Gohr $mail = new Mailer(); 1298d7169d19SAndreas Gohr $mail->to($userinfo['name'] . ' <' . $userinfo['mail'] . '>'); 1299d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1300d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1301d7169d19SAndreas Gohr if ($mail->send()) { 13021d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 13031d5856cfSAndreas Gohr } else { 13041d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 13051d5856cfSAndreas Gohr } 13061d5856cfSAndreas Gohr return true; 13071d5856cfSAndreas Gohr } 1308ab5d26daSAndreas Gohr // never reached 13098b06d178Schris} 13108b06d178Schris 13118b06d178Schris/** 1312b0855b11Sandi * Encrypts a password using the given method and salt 1313b0855b11Sandi * 1314b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1315b0855b11Sandi * is chosen. 1316b0855b11Sandi * 1317b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 131842ea7f44SGerrit Uitslag * 1319ab5d26daSAndreas Gohr * @param string $clear The clear text password 1320ab5d26daSAndreas Gohr * @param string $method The hashing method 1321ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1322b0855b11Sandi * @return string The crypted password 1323b0855b11Sandi */ 1324d868eb89SAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) 1325d868eb89SAndreas Gohr{ 1326b0855b11Sandi global $conf; 1327b0855b11Sandi if (empty($method)) $method = $conf['passcrypt']; 132810a76f6fSfrank 13293a0a2d05SAndreas Gohr $pass = new PassHash(); 13303a0a2d05SAndreas Gohr $call = 'hash_' . $method; 1331b0855b11Sandi 13323a0a2d05SAndreas Gohr if (!method_exists($pass, $call)) { 1333b0855b11Sandi msg("Unsupported crypt method $method", -1); 13343a0a2d05SAndreas Gohr return false; 1335b0855b11Sandi } 13363a0a2d05SAndreas Gohr 13373a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1338b0855b11Sandi} 1339b0855b11Sandi 1340b0855b11Sandi/** 1341b0855b11Sandi * Verifies a cleartext password against a crypted hash 1342b0855b11Sandi * 1343ab5d26daSAndreas Gohr * @param string $clear The clear text password 1344ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1345ab5d26daSAndreas Gohr * @return bool true if both match 13464dc42f7fSGerrit Uitslag * @throws Exception 13474dc42f7fSGerrit Uitslag * 13484dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 1349b0855b11Sandi */ 1350d868eb89SAndreas Gohrfunction auth_verifyPassword($clear, $crypt) 1351d868eb89SAndreas Gohr{ 13523a0a2d05SAndreas Gohr $pass = new PassHash(); 13533a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1354b0855b11Sandi} 1355340756e4Sandi 1356a0b5b007SChris Smith/** 1357a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1358a0b5b007SChris Smith * 1359a0b5b007SChris Smith * @param string $user username 1360a0b5b007SChris Smith * @param string $pass encrypted password 1361a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1362ab5d26daSAndreas Gohr * @return bool 1363a0b5b007SChris Smith */ 1364d868eb89SAndreas Gohrfunction auth_setCookie($user, $pass, $sticky) 1365d868eb89SAndreas Gohr{ 1366a0b5b007SChris Smith global $conf; 1367e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1368a0b5b007SChris Smith global $auth; 136979d00841SOliver Geisen global $USERINFO; 1370a0b5b007SChris Smith 13716547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 1372a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1373a0b5b007SChris Smith 1374a0b5b007SChris Smith // set cookie 1375645c0a36SAndreas Gohr $cookie = base64_encode($user) . '|' . ((int) $sticky) . '|' . base64_encode($pass); 137673ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1377c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1378bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, $cookie, [ 1379bf8392ebSAndreas Gohr 'expires' => $time, 1380bf8392ebSAndreas Gohr 'path' => $cookieDir, 1381bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 1382bf8392ebSAndreas Gohr 'httponly' => true, 1383486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 1384bf8392ebSAndreas Gohr ]); 138555a71a16SGerrit Uitslag 1386a0b5b007SChris Smith // set session 1387a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1388234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1389a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1390a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1391a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1392ab5d26daSAndreas Gohr 1393ab5d26daSAndreas Gohr return true; 1394a0b5b007SChris Smith} 1395a0b5b007SChris Smith 1396645c0a36SAndreas Gohr/** 1397645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1398645c0a36SAndreas Gohr * 1399645c0a36SAndreas Gohr * @returns array 1400645c0a36SAndreas Gohr */ 1401d868eb89SAndreas Gohrfunction auth_getCookie() 1402d868eb89SAndreas Gohr{ 1403c66972f2SAdrian Lang if (!isset($_COOKIE[DOKU_COOKIE])) { 140424870174SAndreas Gohr return [null, null, null]; 1405c66972f2SAdrian Lang } 140624870174SAndreas Gohr [$user, $sticky, $pass] = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, ''); 1407645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1408645c0a36SAndreas Gohr $pass = base64_decode($pass); 1409645c0a36SAndreas Gohr $user = base64_decode($user); 141024870174SAndreas Gohr return [$user, $sticky, $pass]; 1411645c0a36SAndreas Gohr} 1412645c0a36SAndreas Gohr 1413e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1414