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 13cf927d07Ssplitbrainuse dokuwiki\JWT; 1424870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1596348f27SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 1696348f27SAndreas Gohruse dokuwiki\Extension\Event; 1796348f27SAndreas Gohruse dokuwiki\Extension\PluginController; 18c3cc6e05SAndreas Gohruse dokuwiki\PassHash; 1975d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 20*927933f5SAndreas Gohruse phpseclib3\Crypt\AES; 21*927933f5SAndreas Gohruse phpseclib3\Crypt\Common\SymmetricKey; 22c3cc6e05SAndreas Gohr 2316905344SAndreas Gohr/** 2416905344SAndreas Gohr * Initialize the auth system. 2516905344SAndreas Gohr * 2616905344SAndreas Gohr * This function is automatically called at the end of init.php 2716905344SAndreas Gohr * 2816905344SAndreas Gohr * This used to be the main() of the auth.php 2916905344SAndreas Gohr * 3016905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 3116905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 32ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 33ab5d26daSAndreas Gohr * @return bool 3416905344SAndreas Gohr */ 35d868eb89SAndreas Gohrfunction auth_setup() 36d868eb89SAndreas Gohr{ 37742c66f8Schris global $conf; 38e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 3903c4aec3Schris global $auth; 40bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 41bcc94b2cSAndreas Gohr global $INPUT; 429a9714acSDominik Eckelmann global $AUTH_ACL; 439a9714acSDominik Eckelmann global $lang; 443a7140a1SAndreas Gohr /* @var PluginController $plugin_controller */ 459c29eea5SJan Schumann global $plugin_controller; 4624870174SAndreas Gohr $AUTH_ACL = []; 4703c4aec3Schris 4816905344SAndreas Gohr if (!$conf['useacl']) return false; 4916905344SAndreas Gohr 509c29eea5SJan Schumann // try to load auth backend from plugins 519c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 529c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 53f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 549c29eea5SJan Schumann break; 559c29eea5SJan Schumann } 569c29eea5SJan Schumann } 578b06d178Schris 586547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) { 5971c734a9SGerrit Uitslag msg($lang['authtempfail'], -1); 603094e817SAndreas Gohr return false; 613094e817SAndreas Gohr } 628b06d178Schris 636416b708SMichael Hamann if ($auth->success == false) { 640f4f4adfSAndreas Gohr // degrade to unauthenticated user 65ecad51ddSAndreas Gohr $auth = null; 660f4f4adfSAndreas Gohr auth_logoff(); 67cd52f92dSchris msg($lang['authtempfail'], -1); 686416b708SMichael Hamann return false; 69d2dde4ebSMatthias Grimm } 7016905344SAndreas Gohr 7116905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 72bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 73bcc94b2cSAndreas Gohr if (!$conf['rememberme']) $INPUT->set('r', false); 74bbbd6568SAndreas Gohr 7562bf3ac0SDamien Regad // Populate Basic Auth user/password from Authorization header 7662bf3ac0SDamien Regad // Note: with FastCGI, data is in REDIRECT_HTTP_AUTHORIZATION instead of HTTP_AUTHORIZATION 7762bf3ac0SDamien Regad $header = $INPUT->server->str('HTTP_AUTHORIZATION') ?: $INPUT->server->str('REDIRECT_HTTP_AUTHORIZATION'); 7862bf3ac0SDamien Regad if (preg_match('~^Basic ([a-z\d/+]*={0,2})$~i', $header, $matches)) { 7962bf3ac0SDamien Regad $userpass = explode(':', base64_decode($matches[1])); 8024870174SAndreas Gohr [$_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']] = $userpass; 81528ddc7cSAndreas Gohr } 82528ddc7cSAndreas Gohr 831e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 8403062864SAndreas Gohr if (!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($INPUT->server->str('PHP_AUTH_USER'))) { 8503062864SAndreas Gohr $INPUT->set('u', $INPUT->server->str('PHP_AUTH_USER')); 8603062864SAndreas Gohr $INPUT->set('p', $INPUT->server->str('PHP_AUTH_PW')); 87bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 881e8c9c90SAndreas Gohr } 891e8c9c90SAndreas Gohr 90395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 9193a7873eSAndreas Gohr if (true === $auth->success) { 92395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 93395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 94f4476bd9SJan Schumann } 95191bb90aSAndreas Gohr 96455aa67eSAndreas Gohr if (!auth_tokenlogin()) { 9781e99965SPhy $ok = null; 98455aa67eSAndreas Gohr 996547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin && $auth->canDo('external')) { 10081e99965SPhy $ok = $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 10181e99965SPhy } 10281e99965SPhy 10381e99965SPhy if ($ok === null) { 10481e99965SPhy // external trust mechanism not in place, or returns no result, 10581e99965SPhy // then attempt auth_login 10624870174SAndreas Gohr $evdata = [ 107bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 108bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 109bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 110bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 11124870174SAndreas Gohr ]; 112cbb44eabSAndreas Gohr Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 113f5cb575dSAndreas Gohr } 114455aa67eSAndreas Gohr } 115f5cb575dSAndreas Gohr 11616905344SAndreas Gohr //load ACL into a global array XXX 11775c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 118ab5d26daSAndreas Gohr 119ab5d26daSAndreas Gohr return true; 12075c93b77SAndreas Gohr} 12175c93b77SAndreas Gohr 12275c93b77SAndreas Gohr/** 12375c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12475c93b77SAndreas Gohr * 12575c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12642ea7f44SGerrit Uitslag * 127ab5d26daSAndreas Gohr * @return array 12875c93b77SAndreas Gohr */ 129d868eb89SAndreas Gohrfunction auth_loadACL() 130d868eb89SAndreas Gohr{ 13175c93b77SAndreas Gohr global $config_cascade; 132b78bf706Sromain global $USERINFO; 133585bf44eSChristopher Smith /* @var Input $INPUT */ 134585bf44eSChristopher Smith global $INPUT; 13575c93b77SAndreas Gohr 13624870174SAndreas Gohr if (!is_readable($config_cascade['acl']['default'])) return []; 13775c93b77SAndreas Gohr 13875c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13975c93b77SAndreas Gohr 14024870174SAndreas Gohr $out = []; 1419ce556d2SAndreas Gohr foreach ($acl as $line) { 1429ce556d2SAndreas Gohr $line = trim($line); 1432401f18dSSyntaxseed if (empty($line) || ($line[0] == '#')) continue; // skip blank lines & comments 14424870174SAndreas Gohr [$id, $rest] = preg_split('/[ \t]+/', $line, 2); 14532e82180SAndreas Gohr 146443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 147ad3d68d7SChristopher Smith if (strstr($line, '%USER%')) { 148ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 149585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 150ad3d68d7SChristopher Smith 151585bf44eSChristopher Smith $id = str_replace('%USER%', cleanID($INPUT->server->str('REMOTE_USER')), $id); 152585bf44eSChristopher Smith $rest = str_replace('%USER%', auth_nameencode($INPUT->server->str('REMOTE_USER')), $rest); 153ad3d68d7SChristopher Smith } 154ad3d68d7SChristopher Smith 155ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1569ce556d2SAndreas Gohr if (strstr($line, '%GROUP%')) { 157ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 15806f34f54SPhy if (isset($USERINFO['grps'])) { 1599ce556d2SAndreas Gohr foreach ((array) $USERINFO['grps'] as $grp) { 160b78bf706Sromain $nid = str_replace('%GROUP%', cleanID($grp), $id); 16132e82180SAndreas Gohr $nrest = str_replace('%GROUP%', '@' . auth_nameencode($grp), $rest); 16232e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 163b78bf706Sromain } 16406f34f54SPhy } 16532e82180SAndreas Gohr } else { 16632e82180SAndreas Gohr $out[] = "$id\t$rest"; 167a8fe108bSGuy Brand } 16811799630Sandi } 1699ce556d2SAndreas Gohr 17032e82180SAndreas Gohr return $out; 171f3f0262cSandi} 172f3f0262cSandi 173ab5d26daSAndreas Gohr/** 174455aa67eSAndreas Gohr * Try a token login 175455aa67eSAndreas Gohr * 176455aa67eSAndreas Gohr * @return bool true if token login succeeded 177455aa67eSAndreas Gohr */ 178cf927d07Ssplitbrainfunction auth_tokenlogin() 179cf927d07Ssplitbrain{ 180455aa67eSAndreas Gohr global $USERINFO; 181455aa67eSAndreas Gohr global $INPUT; 182455aa67eSAndreas Gohr /** @var DokuWiki_Auth_Plugin $auth */ 183455aa67eSAndreas Gohr global $auth; 184455aa67eSAndreas Gohr if (!$auth) return false; 185455aa67eSAndreas Gohr 186455aa67eSAndreas Gohr // see if header has token 187455aa67eSAndreas Gohr $header = ''; 1886fdb83b6SAndreas Gohr if (function_exists('getallheaders')) { 189455aa67eSAndreas Gohr // Authorization headers are not in $_SERVER for mod_php 1906fdb83b6SAndreas Gohr $headers = array_change_key_case(getallheaders()); 191d26e5a24SAndreas Gohr if (isset($headers['authorization'])) $header = $headers['authorization']; 192455aa67eSAndreas Gohr } else { 193455aa67eSAndreas Gohr $header = $INPUT->server->str('HTTP_AUTHORIZATION'); 194455aa67eSAndreas Gohr } 195455aa67eSAndreas Gohr if (!$header) return false; 196cf927d07Ssplitbrain [$type, $token] = sexplode(' ', $header, 2); 197455aa67eSAndreas Gohr if ($type !== 'Bearer') return false; 198455aa67eSAndreas Gohr 199455aa67eSAndreas Gohr // check token 200455aa67eSAndreas Gohr try { 201cf927d07Ssplitbrain $authtoken = JWT::validate($token); 202455aa67eSAndreas Gohr } catch (Exception $e) { 203455aa67eSAndreas Gohr msg(hsc($e->getMessage()), -1); 204455aa67eSAndreas Gohr return false; 205455aa67eSAndreas Gohr } 206455aa67eSAndreas Gohr 207455aa67eSAndreas Gohr // fetch user info from backend 208455aa67eSAndreas Gohr $user = $authtoken->getUser(); 209455aa67eSAndreas Gohr $USERINFO = $auth->getUserData($user); 210455aa67eSAndreas Gohr if (!$USERINFO) return false; 211455aa67eSAndreas Gohr 212455aa67eSAndreas Gohr // the code is correct, set up user 213455aa67eSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 214455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 215455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = 'nope'; 216455aa67eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 217455aa67eSAndreas Gohr 218455aa67eSAndreas Gohr return true; 219455aa67eSAndreas Gohr} 220455aa67eSAndreas Gohr 221455aa67eSAndreas Gohr/** 222ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 223ab5d26daSAndreas Gohr * 22442ea7f44SGerrit Uitslag * @param array $evdata 225ab5d26daSAndreas Gohr * @return bool 2264dc42f7fSGerrit Uitslag * @throws Exception 227ab5d26daSAndreas Gohr */ 228d868eb89SAndreas Gohrfunction auth_login_wrapper($evdata) 229d868eb89SAndreas Gohr{ 230ab5d26daSAndreas Gohr return auth_login( 231ab5d26daSAndreas Gohr $evdata['user'], 232b5ee21aaSAdrian Lang $evdata['password'], 233b5ee21aaSAdrian Lang $evdata['sticky'], 234ab5d26daSAndreas Gohr $evdata['silent'] 235ab5d26daSAndreas Gohr ); 236b5ee21aaSAdrian Lang} 237b5ee21aaSAdrian Lang 238f3f0262cSandi/** 239f3f0262cSandi * This tries to login the user based on the sent auth credentials 240f3f0262cSandi * 241f3f0262cSandi * The authentication works like this: if a username was given 24215fae107Sandi * a new login is assumed and user/password are checked. If they 24315fae107Sandi * are correct the password is encrypted with blowfish and stored 24415fae107Sandi * together with the username in a cookie - the same info is stored 24515fae107Sandi * in the session, too. Additonally a browserID is stored in the 24615fae107Sandi * session. 24715fae107Sandi * 24815fae107Sandi * If no username was given the cookie is checked: if the username, 24915fae107Sandi * crypted password and browserID match between session and cookie 25015fae107Sandi * no further testing is done and the user is accepted 25115fae107Sandi * 25215fae107Sandi * If a cookie was found but no session info was availabe the 253136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 25415fae107Sandi * together with username rechecked by calling this function again. 255f3f0262cSandi * 256f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 257f3f0262cSandi * are set. 25815fae107Sandi * 25915fae107Sandi * @param string $user Username 26015fae107Sandi * @param string $pass Cleartext Password 26115fae107Sandi * @param bool $sticky Cookie should not expire 262f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 26315fae107Sandi * @return bool true on successful auth 2644dc42f7fSGerrit Uitslag * @throws Exception 2654dc42f7fSGerrit Uitslag * 2664dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 267f3f0262cSandi */ 268d868eb89SAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) 269d868eb89SAndreas Gohr{ 270f3f0262cSandi global $USERINFO; 271f3f0262cSandi global $conf; 272f3f0262cSandi global $lang; 273e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 274cd52f92dSchris global $auth; 275585bf44eSChristopher Smith /* @var Input $INPUT */ 276585bf44eSChristopher Smith global $INPUT; 277ab5d26daSAndreas Gohr 2786547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 279beca106aSAdrian Lang 280bbbd6568SAndreas Gohr if (!empty($user)) { 281132bdbfeSandi //usual login 2825e9e1054SAndreas Gohr if (!empty($pass) && $auth->checkPass($user, $pass)) { 283132bdbfeSandi // make logininfo globally available 284585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 28530d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 28604369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 287132bdbfeSandi return true; 288f3f0262cSandi } else { 289f3f0262cSandi //invalid credentials - log off 290f8b1e4e7SAndreas Gohr if (!$silent) { 291f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 292f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 293f8b1e4e7SAndreas Gohr } 294f3f0262cSandi auth_logoff(); 295132bdbfeSandi return false; 296f3f0262cSandi } 297f3f0262cSandi } else { 298132bdbfeSandi // read cookie information 29924870174SAndreas Gohr [$user, $sticky, $pass] = auth_getCookie(); 300132bdbfeSandi if ($user && $pass) { 301132bdbfeSandi // we got a cookie - see if we can trust it 302fa7c70ffSAdrian Lang 303fa7c70ffSAdrian Lang // get session info 3040058ae75SDamien Regad if (isset($_SESSION[DOKU_COOKIE])) { 305fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 3067d34963bSAndreas Gohr if ( 3077d34963bSAndreas Gohr isset($session) && 3087172dbc0SAndreas Gohr $auth->useSessionCache($user) && 3094c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 310132bdbfeSandi ($session['user'] == $user) && 311234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 312ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 313ab5d26daSAndreas Gohr ) { 314132bdbfeSandi // he has session, cookie and browser right - let him in 315585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 316132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 317132bdbfeSandi return true; 318132bdbfeSandi } 3190058ae75SDamien Regad } 320f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 32130d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 32204369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 323f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 324132bdbfeSandi } 325132bdbfeSandi } 326f3f0262cSandi //just to be sure 327883179a4SAndreas Gohr auth_logoff(true); 328132bdbfeSandi return false; 329f3f0262cSandi} 330132bdbfeSandi 331132bdbfeSandi/** 332136ce040Sandi * Builds a pseudo UID from browser and IP data 333132bdbfeSandi * 334132bdbfeSandi * This is neither unique nor unfakable - still it adds some 335136ce040Sandi * security. Using the first part of the IP makes sure 33680b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 33715fae107Sandi * 33815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 33915fae107Sandi * 340b13c0e1aSAdaKaleh * @return string a SHA256 sum of various browser headers 341132bdbfeSandi */ 342d868eb89SAndreas Gohrfunction auth_browseruid() 343d868eb89SAndreas Gohr{ 344585bf44eSChristopher Smith /* @var Input $INPUT */ 345585bf44eSChristopher Smith global $INPUT; 346585bf44eSChristopher Smith 3472f9daf16SAndreas Gohr $ip = clientIP(true); 348b13c0e1aSAdaKaleh // convert IP string to packed binary representation 349b13c0e1aSAdaKaleh $pip = inet_pton($ip); 350b7c67f83SAndreas Gohr 351b7c67f83SAndreas Gohr $uid = implode("\n", [ 352b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_USER_AGENT'), 353b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_ACCEPT_LANGUAGE'), 354b7c67f83SAndreas Gohr substr($pip, 0, strlen($pip) / 2), // use half of the IP address (works for both IPv4 and IPv6) 355b7c67f83SAndreas Gohr ]); 356b13c0e1aSAdaKaleh return hash('sha256', $uid); 357132bdbfeSandi} 358132bdbfeSandi 359132bdbfeSandi/** 360132bdbfeSandi * Creates a random key to encrypt the password in cookies 36115fae107Sandi * 36215fae107Sandi * This function tries to read the password for encrypting 36398407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 36415fae107Sandi * if no such file is found a random key is created and 36515fae107Sandi * and stored in this file. 36615fae107Sandi * 36732ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 36830d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 36915fae107Sandi * @return string 3704dc42f7fSGerrit Uitslag * @throws Exception 3714dc42f7fSGerrit Uitslag * 3724dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 373132bdbfeSandi */ 374d868eb89SAndreas Gohrfunction auth_cookiesalt($addsession = false, $secure = false) 375d868eb89SAndreas Gohr{ 376a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 377fe745becSMichael Große return 'test'; 378a1fe3c9cSMichael Große } 379132bdbfeSandi global $conf; 38098407a7aSandi $file = $conf['metadir'] . '/_htcookiesalt'; 38130d544a4SMichael Hamann if ($secure || !file_exists($file)) { 38230d544a4SMichael Hamann $file = $conf['metadir'] . '/_htcookiesalt2'; 38330d544a4SMichael Hamann } 384132bdbfeSandi $salt = io_readFile($file); 385132bdbfeSandi if (empty($salt)) { 38630d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 387132bdbfeSandi io_saveFile($file, $salt); 388132bdbfeSandi } 38932ed2b36SAndreas Gohr if ($addsession) { 39032ed2b36SAndreas Gohr $salt .= session_id(); 39132ed2b36SAndreas Gohr } 392132bdbfeSandi return $salt; 393f3f0262cSandi} 394f3f0262cSandi 395f3f0262cSandi/** 3967a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 397483b6238SMichael Hamann * 3987a33d2f8SNiklas Keller * @param int $length number of bytes 3997a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 4004dc42f7fSGerrit Uitslag * @throws Exception 4014dc42f7fSGerrit Uitslag * 4024dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 403483b6238SMichael Hamann */ 404d868eb89SAndreas Gohrfunction auth_randombytes($length) 405d868eb89SAndreas Gohr{ 4067a33d2f8SNiklas Keller return random_bytes($length); 407483b6238SMichael Hamann} 408483b6238SMichael Hamann 409483b6238SMichael Hamann/** 4107a33d2f8SNiklas Keller * Cryptographically secure random number generator. 411483b6238SMichael Hamann * 412483b6238SMichael Hamann * @param int $min 413483b6238SMichael Hamann * @param int $max 414483b6238SMichael Hamann * @return int 4154dc42f7fSGerrit Uitslag * @throws Exception 4164dc42f7fSGerrit Uitslag * 4174dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 418483b6238SMichael Hamann */ 419d868eb89SAndreas Gohrfunction auth_random($min, $max) 420d868eb89SAndreas Gohr{ 4217a33d2f8SNiklas Keller return random_int($min, $max); 422483b6238SMichael Hamann} 423483b6238SMichael Hamann 424483b6238SMichael Hamann/** 42504369c3eSMichael Hamann * Encrypt data using the given secret using AES 42604369c3eSMichael Hamann * 42704369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 42804369c3eSMichael Hamann * using pbkdf2. 42904369c3eSMichael Hamann * 43004369c3eSMichael Hamann * @param string $data The data that shall be encrypted 43104369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 43204369c3eSMichael Hamann * @return string The ciphertext 4334dc42f7fSGerrit Uitslag * @throws Exception 43404369c3eSMichael Hamann */ 435d868eb89SAndreas Gohrfunction auth_encrypt($data, $secret) 436d868eb89SAndreas Gohr{ 43704369c3eSMichael Hamann $iv = auth_randombytes(16); 438*927933f5SAndreas Gohr $cipher = new AES('cbc'); 43904369c3eSMichael Hamann $cipher->setPassword($secret); 440*927933f5SAndreas Gohr $cipher->setIV($iv); 44104369c3eSMichael Hamann 4427b650cefSMichael Hamann /* 4437b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 4447b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 4457b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 4467b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 4477b650cefSMichael Hamann */ 44804369c3eSMichael Hamann return $cipher->encrypt($iv . $data); 44904369c3eSMichael Hamann} 45004369c3eSMichael Hamann 45104369c3eSMichael Hamann/** 45204369c3eSMichael Hamann * Decrypt the given AES ciphertext 45304369c3eSMichael Hamann * 45404369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 45504369c3eSMichael Hamann * 45604369c3eSMichael Hamann * @param string $ciphertext The encrypted data 45704369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 45804369c3eSMichael Hamann * @return string The decrypted data 45904369c3eSMichael Hamann */ 460d868eb89SAndreas Gohrfunction auth_decrypt($ciphertext, $secret) 461d868eb89SAndreas Gohr{ 4627b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 463*927933f5SAndreas Gohr $cipher = new AES('cbc'); 46404369c3eSMichael Hamann $cipher->setPassword($secret); 4657b650cefSMichael Hamann $cipher->setIV($iv); 46604369c3eSMichael Hamann 4677b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 46804369c3eSMichael Hamann} 46904369c3eSMichael Hamann 47004369c3eSMichael Hamann/** 471883179a4SAndreas Gohr * Log out the current user 472883179a4SAndreas Gohr * 473f3f0262cSandi * This clears all authentication data and thus log the user 474883179a4SAndreas Gohr * off. It also clears session data. 47515fae107Sandi * 47615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 47742ea7f44SGerrit Uitslag * 478883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 479f3f0262cSandi */ 480d868eb89SAndreas Gohrfunction auth_logoff($keepbc = false) 481d868eb89SAndreas Gohr{ 482f3f0262cSandi global $conf; 483f3f0262cSandi global $USERINFO; 484e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4855298a619SAndreas Gohr global $auth; 486585bf44eSChristopher Smith /* @var Input $INPUT */ 487585bf44eSChristopher Smith global $INPUT; 48837065e65Sandi 489d4869846SAndreas Gohr // make sure the session is writable (it usually is) 490e9621d07SAndreas Gohr @session_start(); 491e9621d07SAndreas Gohr 492e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 493e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 494e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 495e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 496e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 497e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 498883179a4SAndreas Gohr if (!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 499e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 500585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 501132bdbfeSandi $USERINFO = null; //FIXME 502f5c6743cSAndreas Gohr 50373ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 504bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, '', [ 505bf8392ebSAndreas Gohr 'expires' => time() - 600000, 506bf8392ebSAndreas Gohr 'path' => $cookieDir, 507bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 508bf8392ebSAndreas Gohr 'httponly' => true, 509486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 510bf8392ebSAndreas Gohr ]); 5115298a619SAndreas Gohr 5126547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin) { 5136547cfc7SGerrit Uitslag $auth->logOff(); 5146547cfc7SGerrit Uitslag } 515f3f0262cSandi} 516f3f0262cSandi 517f3f0262cSandi/** 518f8cc712eSAndreas Gohr * Check if a user is a manager 519f8cc712eSAndreas Gohr * 520f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 521f8cc712eSAndreas Gohr * user. 522f8cc712eSAndreas Gohr * 523f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 524f8cc712eSAndreas Gohr * 525ab5d26daSAndreas Gohr * @param string $user Username 526ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 527ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 52810396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 529ab5d26daSAndreas Gohr * @return bool 53096348f27SAndreas Gohr * @see auth_isadmin 53196348f27SAndreas Gohr * 53296348f27SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 533f8cc712eSAndreas Gohr */ 534d868eb89SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache = false) 535d868eb89SAndreas Gohr{ 536f8cc712eSAndreas Gohr global $conf; 537f8cc712eSAndreas Gohr global $USERINFO; 538e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 539d752aedeSAndreas Gohr global $auth; 540585bf44eSChristopher Smith /* @var Input $INPUT */ 541585bf44eSChristopher Smith global $INPUT; 542585bf44eSChristopher Smith 543f8cc712eSAndreas Gohr 5446547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 545c66972f2SAdrian Lang if (is_null($user)) { 546585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) { 547c66972f2SAdrian Lang return false; 548c66972f2SAdrian Lang } else { 549585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 550c66972f2SAdrian Lang } 551c66972f2SAdrian Lang } 552d6dc956fSAndreas Gohr if (is_null($groups)) { 5531525c228SAnna Dabrowska // checking the logged in user, or another one? 5541525c228SAnna Dabrowska if ($USERINFO && $user === $INPUT->server->str('REMOTE_USER')) { 5551525c228SAnna Dabrowska $groups = (array) $USERINFO['grps']; 55666b108d6SAnna Dabrowska } else { 5576cf7b139SAndreas Gohr $groups = $auth->getUserData($user); 5586cf7b139SAndreas Gohr $groups = $groups ? $groups['grps'] : []; 55966b108d6SAnna Dabrowska } 560e259aa79SAndreas Gohr } 561e259aa79SAndreas Gohr 56296348f27SAndreas Gohr // prefer cached result 56396348f27SAndreas Gohr static $cache = []; 56410396f77SAndreas Gohr $cachekey = serialize([$user, $adminonly, $groups]); 56596348f27SAndreas Gohr if (!isset($cache[$cachekey]) || $recache) { 566d6dc956fSAndreas Gohr // check superuser match 56796348f27SAndreas Gohr $ok = auth_isMember($conf['superuser'], $user, $groups); 56800ce12daSChris Smith 56996348f27SAndreas Gohr // check managers 57096348f27SAndreas Gohr if (!$ok && !$adminonly) { 57196348f27SAndreas Gohr $ok = auth_isMember($conf['manager'], $user, $groups); 57296348f27SAndreas Gohr } 57396348f27SAndreas Gohr 57496348f27SAndreas Gohr $cache[$cachekey] = $ok; 57596348f27SAndreas Gohr } 57696348f27SAndreas Gohr 57796348f27SAndreas Gohr return $cache[$cachekey]; 578f8cc712eSAndreas Gohr} 579f8cc712eSAndreas Gohr 580f8cc712eSAndreas Gohr/** 581f8cc712eSAndreas Gohr * Check if a user is admin 582f8cc712eSAndreas Gohr * 583f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 584f8cc712eSAndreas Gohr * 585f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 586f8cc712eSAndreas Gohr * 58796348f27SAndreas Gohr * @param string $user Username 58896348f27SAndreas Gohr * @param array $groups List of groups the user is in 58910396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 59096348f27SAndreas Gohr * @return bool 591f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 592ab5d26daSAndreas Gohr * @see auth_ismanager() 59342ea7f44SGerrit Uitslag * 594f8cc712eSAndreas Gohr */ 595d868eb89SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache = false) 596d868eb89SAndreas Gohr{ 59796348f27SAndreas Gohr return auth_ismanager($user, $groups, true, $recache); 598f8cc712eSAndreas Gohr} 599f8cc712eSAndreas Gohr 600d6dc956fSAndreas Gohr/** 601d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 602d6dc956fSAndreas Gohr * users and groups to determine membership status 603d6dc956fSAndreas Gohr * 604d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 605d6dc956fSAndreas Gohr * 60642ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 60742ea7f44SGerrit Uitslag * @param string $user user to match against 60842ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 6095446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 610d6dc956fSAndreas Gohr */ 611d868eb89SAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) 612d868eb89SAndreas Gohr{ 613e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 614d6dc956fSAndreas Gohr global $auth; 6156547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 616d6dc956fSAndreas Gohr 617d6dc956fSAndreas Gohr // clean user and groups 6184f56ecbfSAdrian Lang if (!$auth->isCaseSensitive()) { 61924870174SAndreas Gohr $user = PhpString::strtolower($user); 62024870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 621d6dc956fSAndreas Gohr } 622d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 62324870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 624d6dc956fSAndreas Gohr 625d6dc956fSAndreas Gohr // extract the memberlist 626d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 627d6dc956fSAndreas Gohr $members = array_map('trim', $members); 628d6dc956fSAndreas Gohr $members = array_unique($members); 629d6dc956fSAndreas Gohr $members = array_filter($members); 630d6dc956fSAndreas Gohr 631d6dc956fSAndreas Gohr // compare cleaned values 632d6dc956fSAndreas Gohr foreach ($members as $member) { 633e5204a12SJurgen Hart if ($member == '@ALL') return true; 63424870174SAndreas Gohr if (!$auth->isCaseSensitive()) $member = PhpString::strtolower($member); 635d6dc956fSAndreas Gohr if ($member[0] == '@') { 636d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 637d6dc956fSAndreas Gohr if (in_array($member, $groups)) return true; 638d6dc956fSAndreas Gohr } else { 639d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 640d6dc956fSAndreas Gohr if ($member == $user) return true; 641d6dc956fSAndreas Gohr } 642d6dc956fSAndreas Gohr } 643d6dc956fSAndreas Gohr 644d6dc956fSAndreas Gohr // still here? not a member! 645d6dc956fSAndreas Gohr return false; 646d6dc956fSAndreas Gohr} 647d6dc956fSAndreas Gohr 648f8cc712eSAndreas Gohr/** 64915fae107Sandi * Convinience function for auth_aclcheck() 65015fae107Sandi * 65115fae107Sandi * This checks the permissions for the current user 65215fae107Sandi * 65315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 65415fae107Sandi * 6551698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 65615fae107Sandi * @return int permission level 657f3f0262cSandi */ 658d868eb89SAndreas Gohrfunction auth_quickaclcheck($id) 659d868eb89SAndreas Gohr{ 660f3f0262cSandi global $conf; 661f3f0262cSandi global $USERINFO; 662585bf44eSChristopher Smith /* @var Input $INPUT */ 663585bf44eSChristopher Smith global $INPUT; 664f3f0262cSandi # if no ACL is used always return upload rights 665f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 66624870174SAndreas Gohr return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), is_array($USERINFO) ? $USERINFO['grps'] : []); 667f3f0262cSandi} 668f3f0262cSandi 669f3f0262cSandi/** 670c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 67115fae107Sandi * 67215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 67342ea7f44SGerrit Uitslag * 674c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6751698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 67615fae107Sandi * @param string $user Username 6773272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 67815fae107Sandi * @return int permission level 679f3f0262cSandi */ 680d868eb89SAndreas Gohrfunction auth_aclcheck($id, $user, $groups) 681d868eb89SAndreas Gohr{ 68224870174SAndreas Gohr $data = [ 683bf8f8509SAndreas Gohr 'id' => $id ?? '', 684c17acc9fSAndreas Gohr 'user' => $user, 685c17acc9fSAndreas Gohr 'groups' => $groups 68624870174SAndreas Gohr ]; 687c17acc9fSAndreas Gohr 688cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 689c17acc9fSAndreas Gohr} 690c17acc9fSAndreas Gohr 691c17acc9fSAndreas Gohr/** 692c17acc9fSAndreas Gohr * default ACL check method 693c17acc9fSAndreas Gohr * 694c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 695c17acc9fSAndreas Gohr * 696c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 69742ea7f44SGerrit Uitslag * 698c17acc9fSAndreas Gohr * @param array $data event data 699c17acc9fSAndreas Gohr * @return int permission level 700c17acc9fSAndreas Gohr */ 701d868eb89SAndreas Gohrfunction auth_aclcheck_cb($data) 702d868eb89SAndreas Gohr{ 703c17acc9fSAndreas Gohr $id =& $data['id']; 704c17acc9fSAndreas Gohr $user =& $data['user']; 705c17acc9fSAndreas Gohr $groups =& $data['groups']; 706c17acc9fSAndreas Gohr 707f3f0262cSandi global $conf; 708f3f0262cSandi global $AUTH_ACL; 709e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 710d752aedeSAndreas Gohr global $auth; 711f3f0262cSandi 71285d03f68SAndreas Gohr // if no ACL is used always return upload rights 713f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 7146547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return AUTH_NONE; 715bf8f8509SAndreas Gohr if (!is_array($AUTH_ACL)) return AUTH_NONE; 716f3f0262cSandi 717074cf26bSandi //make sure groups is an array 71824870174SAndreas Gohr if (!is_array($groups)) $groups = []; 719074cf26bSandi 72085d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 721ab5d26daSAndreas Gohr if (auth_isadmin($user, $groups)) { 722ab5d26daSAndreas Gohr return AUTH_ADMIN; 723ab5d26daSAndreas Gohr } 72485d03f68SAndreas Gohr 725eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive()) { 72624870174SAndreas Gohr $user = PhpString::strtolower($user); 72724870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 728eb3ce0d5SKazutaka Miyasaka } 72937ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 73024870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 73185d03f68SAndreas Gohr 7326c2bb100SAndreas Gohr //prepend groups with @ and nameencode 73337ff2261SSascha Klopp foreach ($groups as &$group) { 73437ff2261SSascha Klopp $group = '@' . auth_nameencode($group); 73510a76f6fSfrank } 73610a76f6fSfrank 737f3f0262cSandi $ns = getNS($id); 738f3f0262cSandi $perm = -1; 739f3f0262cSandi 740f3f0262cSandi //add ALL group 741f3f0262cSandi $groups[] = '@ALL'; 74237ff2261SSascha Klopp 743f3f0262cSandi //add User 74434aeb4afSAndreas Gohr if ($user) $groups[] = $user; 745f3f0262cSandi 746f3f0262cSandi //check exact match first 74721c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($id, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 748f3f0262cSandi if (count($matches)) { 749f3f0262cSandi foreach ($matches as $match) { 750f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 75121c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 752eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 75324870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 754eb3ce0d5SKazutaka Miyasaka } 75548d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 75648d7b7a6SDominik Eckelmann continue; 75748d7b7a6SDominik Eckelmann } 7588ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 759f3f0262cSandi if ($acl[2] > $perm) { 760f3f0262cSandi $perm = $acl[2]; 761f3f0262cSandi } 762f3f0262cSandi } 763f3f0262cSandi if ($perm > -1) { 764f3f0262cSandi //we had a match - return it 765def492a2SGuillaume Turri return (int) $perm; 766f3f0262cSandi } 767f3f0262cSandi } 768f3f0262cSandi 769f3f0262cSandi //still here? do the namespace checks 770f3f0262cSandi if ($ns) { 7713e304b55SMichael Hamann $path = $ns . ':*'; 772f3f0262cSandi } else { 7733e304b55SMichael Hamann $path = '*'; //root document 774f3f0262cSandi } 775f3f0262cSandi 776f3f0262cSandi do { 77721c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($path, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 778f3f0262cSandi if (count($matches)) { 779f3f0262cSandi foreach ($matches as $match) { 780f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 78121c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 782eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 78324870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 784eb3ce0d5SKazutaka Miyasaka } 78548d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 78648d7b7a6SDominik Eckelmann continue; 78748d7b7a6SDominik Eckelmann } 7888ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 789f3f0262cSandi if ($acl[2] > $perm) { 790f3f0262cSandi $perm = $acl[2]; 791f3f0262cSandi } 792f3f0262cSandi } 793f3f0262cSandi //we had a match - return it 79448d7b7a6SDominik Eckelmann if ($perm != -1) { 795def492a2SGuillaume Turri return (int) $perm; 796f3f0262cSandi } 79748d7b7a6SDominik Eckelmann } 798f3f0262cSandi //get next higher namespace 799f3f0262cSandi $ns = getNS($ns); 800f3f0262cSandi 8013e304b55SMichael Hamann if ($path != '*') { 8023e304b55SMichael Hamann $path = $ns . ':*'; 8033e304b55SMichael Hamann if ($path == ':*') $path = '*'; 804f3f0262cSandi } else { 805f3f0262cSandi //we did this already 806f3f0262cSandi //looks like there is something wrong with the ACL 807f3f0262cSandi //break here 808d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 809d5ce66f6SAndreas Gohr return AUTH_NONE; 810f3f0262cSandi } 811f3f0262cSandi } while (1); //this should never loop endless 812ab5d26daSAndreas Gohr return AUTH_NONE; 813f3f0262cSandi} 814f3f0262cSandi 815f3f0262cSandi/** 8166c2bb100SAndreas Gohr * Encode ASCII special chars 8176c2bb100SAndreas Gohr * 8186c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 8196c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 8206c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 8216c2bb100SAndreas Gohr * urlencoding!). 8226c2bb100SAndreas Gohr * 8236c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 8246c2bb100SAndreas Gohr * 8256c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 8266c2bb100SAndreas Gohr * @see rawurldecode() 82742ea7f44SGerrit Uitslag * 82842ea7f44SGerrit Uitslag * @param string $name 82942ea7f44SGerrit Uitslag * @param bool $skip_group 83042ea7f44SGerrit Uitslag * @return string 8316c2bb100SAndreas Gohr */ 832d868eb89SAndreas Gohrfunction auth_nameencode($name, $skip_group = false) 833d868eb89SAndreas Gohr{ 834a424cd8eSchris global $cache_authname; 835a424cd8eSchris $cache =& $cache_authname; 83631784267SAndreas Gohr $name = (string) $name; 837a424cd8eSchris 83880601d26SAndreas Gohr // never encode wildcard FS#1955 83980601d26SAndreas Gohr if ($name == '%USER%') return $name; 840b78bf706Sromain if ($name == '%GROUP%') return $name; 84180601d26SAndreas Gohr 842a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 8432401f18dSSyntaxseed if ($skip_group && $name[0] == '@') { 84430f6faf0SChristopher Smith $cache[$name][$skip_group] = '@' . preg_replace_callback( 84530f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 846dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 847dccd6b2bSAndreas Gohr substr($name, 1) 848ab5d26daSAndreas Gohr ); 849e838fc2eSAndreas Gohr } else { 85030f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 85130f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 852dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 853dccd6b2bSAndreas Gohr $name 854ab5d26daSAndreas Gohr ); 855e838fc2eSAndreas Gohr } 8566c2bb100SAndreas Gohr } 8576c2bb100SAndreas Gohr 858a424cd8eSchris return $cache[$name][$skip_group]; 859a424cd8eSchris} 860a424cd8eSchris 86104d68ae4SGerrit Uitslag/** 86204d68ae4SGerrit Uitslag * callback encodes the matches 86304d68ae4SGerrit Uitslag * 86404d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 86504d68ae4SGerrit Uitslag * @return string 86604d68ae4SGerrit Uitslag */ 867d868eb89SAndreas Gohrfunction auth_nameencode_callback($matches) 868d868eb89SAndreas Gohr{ 86930f6faf0SChristopher Smith return '%' . dechex(ord(substr($matches[1], -1))); 87030f6faf0SChristopher Smith} 87130f6faf0SChristopher Smith 8726c2bb100SAndreas Gohr/** 873f3f0262cSandi * Create a pronouncable password 874f3f0262cSandi * 8758a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8768a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8778a285f7fSAndreas Gohr * 8784dc42f7fSGerrit Uitslag * @param string $foruser username for which the password is generated 8794dc42f7fSGerrit Uitslag * @return string pronouncable password 8804dc42f7fSGerrit Uitslag * @throws Exception 8814dc42f7fSGerrit Uitslag * 88215fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8838a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 88415fae107Sandi * 8854dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 886f3f0262cSandi */ 887d868eb89SAndreas Gohrfunction auth_pwgen($foruser = '') 888d868eb89SAndreas Gohr{ 88924870174SAndreas Gohr $data = [ 890d628dcf3SAndreas Gohr 'password' => '', 891d628dcf3SAndreas Gohr 'foruser' => $foruser 89224870174SAndreas Gohr ]; 8938a285f7fSAndreas Gohr 894e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 8958a285f7fSAndreas Gohr if ($evt->advise_before(true)) { 896f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 897f3f0262cSandi $v = 'aeiou'; //vowels 898f3f0262cSandi $a = $c . $v; //both 899987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 900f3f0262cSandi 901987c8d26SAndreas Gohr //use thre syllables... 902987c8d26SAndreas Gohr for ($i = 0; $i < 3; $i++) { 903483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 904483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 905483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 906f3f0262cSandi } 907987c8d26SAndreas Gohr //... and add a nice number and special 90843f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)] . auth_random(10, 99); 9098a285f7fSAndreas Gohr } 9108a285f7fSAndreas Gohr $evt->advise_after(); 911f3f0262cSandi 9128a285f7fSAndreas Gohr return $data['password']; 913f3f0262cSandi} 914f3f0262cSandi 915f3f0262cSandi/** 916f3f0262cSandi * Sends a password to the given user 917f3f0262cSandi * 91815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 91942ea7f44SGerrit Uitslag * 920ab5d26daSAndreas Gohr * @param string $user Login name of the user 921ab5d26daSAndreas Gohr * @param string $password The new password in clear text 92215fae107Sandi * @return bool true on success 923f3f0262cSandi */ 924d868eb89SAndreas Gohrfunction auth_sendPassword($user, $password) 925d868eb89SAndreas Gohr{ 926f3f0262cSandi global $lang; 927e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 928cd52f92dSchris global $auth; 9296547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 930cd52f92dSchris 931d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 9324dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 933f3f0262cSandi 93487ddda95Sandi if (!$userinfo['mail']) return false; 935f3f0262cSandi 936f3f0262cSandi $text = rawLocale('password'); 93724870174SAndreas Gohr $trep = [ 938d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 939d7169d19SAndreas Gohr 'LOGIN' => $user, 940d7169d19SAndreas Gohr 'PASSWORD' => $password 94124870174SAndreas Gohr ]; 942f3f0262cSandi 943d7169d19SAndreas Gohr $mail = new Mailer(); 944102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']) . ' <' . $userinfo['mail'] . '>'); 945d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 946d7169d19SAndreas Gohr $mail->setBody($text, $trep); 947d7169d19SAndreas Gohr return $mail->send(); 948f3f0262cSandi} 949f3f0262cSandi 950f3f0262cSandi/** 95115fae107Sandi * Register a new user 952f3f0262cSandi * 95315fae107Sandi * This registers a new user - Data is read directly from $_POST 95415fae107Sandi * 95515fae107Sandi * @return bool true on success, false on any error 9564dc42f7fSGerrit Uitslag * @throws Exception 9574dc42f7fSGerrit Uitslag * 9584dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 959f3f0262cSandi */ 960d868eb89SAndreas Gohrfunction register() 961d868eb89SAndreas Gohr{ 962f3f0262cSandi global $lang; 963eb5d07e4Sjan global $conf; 9644dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 965cd52f92dSchris global $auth; 96664273335SAndreas Gohr global $INPUT; 967f3f0262cSandi 96864273335SAndreas Gohr if (!$INPUT->post->bool('save')) return false; 9693a48618aSAnika Henke if (!actionOK('register')) return false; 970640145a5Sandi 97164273335SAndreas Gohr // gather input 97264273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 97364273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 97464273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 97564273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 97664273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 977d752aedeSAndreas Gohr 97864273335SAndreas Gohr if (empty($login) || empty($fullname) || empty($email)) { 979f3f0262cSandi msg($lang['regmissing'], -1); 980f3f0262cSandi return false; 981f3f0262cSandi } 982f3f0262cSandi 983cab2716aSmatthias.grimm if ($conf['autopasswd']) { 9848a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 98564273335SAndreas Gohr } elseif (empty($pass) || empty($passchk)) { 986bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 987cab2716aSmatthias.grimm return false; 98864273335SAndreas Gohr } elseif ($pass != $passchk) { 989bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 990cab2716aSmatthias.grimm return false; 991cab2716aSmatthias.grimm } 992cab2716aSmatthias.grimm 993f3f0262cSandi //check mail 99464273335SAndreas Gohr if (!mail_isvalid($email)) { 995f3f0262cSandi msg($lang['regbadmail'], -1); 996f3f0262cSandi return false; 997f3f0262cSandi } 998f3f0262cSandi 999f3f0262cSandi //okay try to create the user 100024870174SAndreas Gohr if (!$auth->triggerUserMod('create', [$login, $pass, $fullname, $email])) { 1001db9faf02SPatrick Brown msg($lang['regfail'], -1); 1002f3f0262cSandi return false; 1003f3f0262cSandi } 1004f3f0262cSandi 1005790b7720SAndreas Gohr // send notification about the new user 100675d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 100775d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 100802a498e7Schris 1009790b7720SAndreas Gohr // are we done? 1010cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 1011cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 1012cab2716aSmatthias.grimm return true; 1013cab2716aSmatthias.grimm } 1014cab2716aSmatthias.grimm 1015790b7720SAndreas Gohr // autogenerated password? then send password to user 101664273335SAndreas Gohr if (auth_sendPassword($login, $pass)) { 1017f3f0262cSandi msg($lang['regsuccess'], 1); 1018f3f0262cSandi return true; 1019f3f0262cSandi } else { 1020f3f0262cSandi msg($lang['regmailfail'], -1); 1021f3f0262cSandi return false; 1022f3f0262cSandi } 1023f3f0262cSandi} 1024f3f0262cSandi 102510a76f6fSfrank/** 10268b06d178Schris * Update user profile 10278b06d178Schris * 10284dc42f7fSGerrit Uitslag * @throws Exception 10294dc42f7fSGerrit Uitslag * 10308b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 10318b06d178Schris */ 1032d868eb89SAndreas Gohrfunction updateprofile() 1033d868eb89SAndreas Gohr{ 10348b06d178Schris global $conf; 10358b06d178Schris global $lang; 1036e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1037cd52f92dSchris global $auth; 1038bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1039bcc94b2cSAndreas Gohr global $INPUT; 10408b06d178Schris 1041bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 10421b2a85e8SAndreas Gohr if (!checkSecurityToken()) return false; 10438b06d178Schris 10443a48618aSAnika Henke if (!actionOK('profile')) { 10458b06d178Schris msg($lang['profna'], -1); 10468b06d178Schris return false; 10478b06d178Schris } 10488b06d178Schris 104924870174SAndreas Gohr $changes = []; 1050bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 1051bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 1052bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 1053bcc94b2cSAndreas Gohr 1054bcc94b2cSAndreas Gohr // check misspelled passwords 1055bcc94b2cSAndreas Gohr if ($changes['pass'] != $INPUT->post->str('passchk')) { 1056bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10578b06d178Schris return false; 10588b06d178Schris } 10598b06d178Schris 10608b06d178Schris // clean fullname and email 1061bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1062bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10638b06d178Schris 1064bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 10657d34963bSAndreas Gohr if ( 10667d34963bSAndreas Gohr (empty($changes['name']) && $auth->canDo('modName')) || 1067bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1068ab5d26daSAndreas Gohr ) { 10698b06d178Schris msg($lang['profnoempty'], -1); 10708b06d178Schris return false; 10718b06d178Schris } 1072bcc94b2cSAndreas Gohr if (!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10738b06d178Schris msg($lang['regbadmail'], -1); 10748b06d178Schris return false; 10758b06d178Schris } 10768b06d178Schris 1077bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10784c21b7eeSAndreas Gohr 1079bcc94b2cSAndreas Gohr // check for unavailable capabilities 1080bcc94b2cSAndreas Gohr if (!$auth->canDo('modName')) unset($changes['name']); 1081bcc94b2cSAndreas Gohr if (!$auth->canDo('modMail')) unset($changes['mail']); 1082bcc94b2cSAndreas Gohr if (!$auth->canDo('modPass')) unset($changes['pass']); 1083bcc94b2cSAndreas Gohr 1084bcc94b2cSAndreas Gohr // anything to do? 108524870174SAndreas Gohr if ($changes === []) { 10868b06d178Schris msg($lang['profnochange'], -1); 10878b06d178Schris return false; 10888b06d178Schris } 10898b06d178Schris 10908b06d178Schris if ($conf['profileconfirm']) { 1091585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 109271422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10938b06d178Schris return false; 10948b06d178Schris } 10958b06d178Schris } 10968b06d178Schris 109724870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$INPUT->server->str('REMOTE_USER'), &$changes])) { 1098db9faf02SPatrick Brown msg($lang['proffail'], -1); 1099db9faf02SPatrick Brown return false; 1100db9faf02SPatrick Brown } 1101db9faf02SPatrick Brown 110267efd1edSPieter Hollants if (array_key_exists('pass', $changes) && $changes['pass']) { 1103c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1104a19c9aa0SGerrit Uitslag [/* user */, $sticky, /* pass */] = auth_getCookie(); 110504369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1106585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1107c276e9e8SMarcel Pennewiss } else { 1108c276e9e8SMarcel Pennewiss // make sure the session is writable 1109c276e9e8SMarcel Pennewiss @session_start(); 1110c276e9e8SMarcel Pennewiss // invalidate session cache 1111c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1112c276e9e8SMarcel Pennewiss session_write_close(); 111332ed2b36SAndreas Gohr } 1114c276e9e8SMarcel Pennewiss 111525b2a98cSMichael Klier return true; 1116a0b5b007SChris Smith} 1117ab5d26daSAndreas Gohr 111804d68ae4SGerrit Uitslag/** 111904d68ae4SGerrit Uitslag * Delete the current logged-in user 112004d68ae4SGerrit Uitslag * 112104d68ae4SGerrit Uitslag * @return bool true on success, false on any error 112204d68ae4SGerrit Uitslag */ 1123d868eb89SAndreas Gohrfunction auth_deleteprofile() 1124d868eb89SAndreas Gohr{ 11252a7abf2dSChristopher Smith global $conf; 11262a7abf2dSChristopher Smith global $lang; 11274dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 11282a7abf2dSChristopher Smith global $auth; 11292a7abf2dSChristopher Smith /* @var Input $INPUT */ 11302a7abf2dSChristopher Smith global $INPUT; 11312a7abf2dSChristopher Smith 11322a7abf2dSChristopher Smith if (!$INPUT->post->bool('delete')) return false; 11332a7abf2dSChristopher Smith if (!checkSecurityToken()) return false; 11342a7abf2dSChristopher Smith 11352a7abf2dSChristopher Smith // action prevented or auth module disallows 11362a7abf2dSChristopher Smith if (!actionOK('profile_delete') || !$auth->canDo('delUser')) { 11372a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 11382a7abf2dSChristopher Smith return false; 11392a7abf2dSChristopher Smith } 11402a7abf2dSChristopher Smith 11412a7abf2dSChristopher Smith if (!$INPUT->post->bool('confirm_delete')) { 11422a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 11432a7abf2dSChristopher Smith return false; 11442a7abf2dSChristopher Smith } 11452a7abf2dSChristopher Smith 11462a7abf2dSChristopher Smith if ($conf['profileconfirm']) { 1147585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 11482a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 11492a7abf2dSChristopher Smith return false; 11502a7abf2dSChristopher Smith } 11512a7abf2dSChristopher Smith } 11522a7abf2dSChristopher Smith 115324870174SAndreas Gohr $deleted = []; 1154585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 115524870174SAndreas Gohr if ($auth->triggerUserMod('delete', [$deleted])) { 11562a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11572a7abf2dSChristopher Smith auth_logoff(); 11582a7abf2dSChristopher Smith return true; 11592a7abf2dSChristopher Smith } 11602a7abf2dSChristopher Smith 11612a7abf2dSChristopher Smith return false; 11622a7abf2dSChristopher Smith} 11632a7abf2dSChristopher Smith 11648b06d178Schris/** 11658b06d178Schris * Send a new password 11668b06d178Schris * 11671d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11681d5856cfSAndreas Gohr * 11691d5856cfSAndreas Gohr * - handling the first request of password reset 11701d5856cfSAndreas Gohr * - validating the password reset auth token 11711d5856cfSAndreas Gohr * 11724dc42f7fSGerrit Uitslag * @return bool true on success, false on any error 11734dc42f7fSGerrit Uitslag * @throws Exception 11744dc42f7fSGerrit Uitslag * 11754dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 11768b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11778b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11788b06d178Schris */ 1179d868eb89SAndreas Gohrfunction act_resendpwd() 1180d868eb89SAndreas Gohr{ 11818b06d178Schris global $lang; 11828b06d178Schris global $conf; 1183e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1184cd52f92dSchris global $auth; 1185bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1186bcc94b2cSAndreas Gohr global $INPUT; 11878b06d178Schris 11883a48618aSAnika Henke if (!actionOK('resendpwd')) { 11898b06d178Schris msg($lang['resendna'], -1); 11908b06d178Schris return false; 11918b06d178Schris } 11928b06d178Schris 1193bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11948b06d178Schris 11951d5856cfSAndreas Gohr if ($token) { 1196cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11971d5856cfSAndreas Gohr 11982401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 119979e79377SAndreas Gohr if (!file_exists($tfile)) { 12001d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1201bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 12021d5856cfSAndreas Gohr return false; 12031d5856cfSAndreas Gohr } 12048a9735e3SAndreas Gohr // token is only valid for 3 days 12058a9735e3SAndreas Gohr if ((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 12068a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1207bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 12081d5856cfSAndreas Gohr @unlink($tfile); 12098a9735e3SAndreas Gohr return false; 12108a9735e3SAndreas Gohr } 12118a9735e3SAndreas Gohr 12128b06d178Schris $user = io_readfile($tfile); 12134dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 12148b06d178Schris if (!$userinfo['mail']) { 12158b06d178Schris msg($lang['resendpwdnouser'], -1); 12168b06d178Schris return false; 12178b06d178Schris } 12188b06d178Schris 1219cc204bbdSAndreas Gohr if (!$conf['autopasswd']) { // we let the user choose a password 1220bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1221bcc94b2cSAndreas Gohr 1222cc204bbdSAndreas Gohr // password given correctly? 1223bcc94b2cSAndreas Gohr if (!$pass) return false; 1224bcc94b2cSAndreas Gohr if ($pass != $INPUT->str('passchk')) { 1225451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1226cc204bbdSAndreas Gohr return false; 1227cc204bbdSAndreas Gohr } 1228cc204bbdSAndreas Gohr 1229bcc94b2cSAndreas Gohr // change it 123024870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1231db9faf02SPatrick Brown msg($lang['proffail'], -1); 1232cc204bbdSAndreas Gohr return false; 1233cc204bbdSAndreas Gohr } 1234cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 12358a285f7fSAndreas Gohr $pass = auth_pwgen($user); 123624870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1237db9faf02SPatrick Brown msg($lang['proffail'], -1); 12388b06d178Schris return false; 12398b06d178Schris } 12408b06d178Schris 12418b06d178Schris if (auth_sendPassword($user, $pass)) { 12428b06d178Schris msg($lang['resendpwdsuccess'], 1); 12438b06d178Schris } else { 12448b06d178Schris msg($lang['regmailfail'], -1); 12458b06d178Schris } 1246cc204bbdSAndreas Gohr } 1247cc204bbdSAndreas Gohr 1248cc204bbdSAndreas Gohr @unlink($tfile); 12498b06d178Schris return true; 12501d5856cfSAndreas Gohr } else { 12511d5856cfSAndreas Gohr // we're in request phase 12521d5856cfSAndreas Gohr 1253bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 12541d5856cfSAndreas Gohr 1255bcc94b2cSAndreas Gohr if (!$INPUT->post->str('login')) { 12561d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12571d5856cfSAndreas Gohr return false; 12581d5856cfSAndreas Gohr } else { 1259bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12601d5856cfSAndreas Gohr } 12611d5856cfSAndreas Gohr 12624dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 12631d5856cfSAndreas Gohr if (!$userinfo['mail']) { 12641d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12651d5856cfSAndreas Gohr return false; 12661d5856cfSAndreas Gohr } 12671d5856cfSAndreas Gohr 12681d5856cfSAndreas Gohr // generate auth token 1269483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12702401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 127124870174SAndreas Gohr $url = wl('', ['do' => 'resendpwd', 'pwauth' => $token], true, '&'); 12721d5856cfSAndreas Gohr 12731d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12741d5856cfSAndreas Gohr 12751d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 127624870174SAndreas Gohr $trep = ['FULLNAME' => $userinfo['name'], 'LOGIN' => $user, 'CONFIRM' => $url]; 12771d5856cfSAndreas Gohr 1278d7169d19SAndreas Gohr $mail = new Mailer(); 1279d7169d19SAndreas Gohr $mail->to($userinfo['name'] . ' <' . $userinfo['mail'] . '>'); 1280d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1281d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1282d7169d19SAndreas Gohr if ($mail->send()) { 12831d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12841d5856cfSAndreas Gohr } else { 12851d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12861d5856cfSAndreas Gohr } 12871d5856cfSAndreas Gohr return true; 12881d5856cfSAndreas Gohr } 1289ab5d26daSAndreas Gohr // never reached 12908b06d178Schris} 12918b06d178Schris 12928b06d178Schris/** 1293b0855b11Sandi * Encrypts a password using the given method and salt 1294b0855b11Sandi * 1295b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1296b0855b11Sandi * is chosen. 1297b0855b11Sandi * 1298b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 129942ea7f44SGerrit Uitslag * 1300ab5d26daSAndreas Gohr * @param string $clear The clear text password 1301ab5d26daSAndreas Gohr * @param string $method The hashing method 1302ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1303b0855b11Sandi * @return string The crypted password 1304b0855b11Sandi */ 1305d868eb89SAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) 1306d868eb89SAndreas Gohr{ 1307b0855b11Sandi global $conf; 1308b0855b11Sandi if (empty($method)) $method = $conf['passcrypt']; 130910a76f6fSfrank 13103a0a2d05SAndreas Gohr $pass = new PassHash(); 13113a0a2d05SAndreas Gohr $call = 'hash_' . $method; 1312b0855b11Sandi 13133a0a2d05SAndreas Gohr if (!method_exists($pass, $call)) { 1314b0855b11Sandi msg("Unsupported crypt method $method", -1); 13153a0a2d05SAndreas Gohr return false; 1316b0855b11Sandi } 13173a0a2d05SAndreas Gohr 13183a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1319b0855b11Sandi} 1320b0855b11Sandi 1321b0855b11Sandi/** 1322b0855b11Sandi * Verifies a cleartext password against a crypted hash 1323b0855b11Sandi * 1324ab5d26daSAndreas Gohr * @param string $clear The clear text password 1325ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1326ab5d26daSAndreas Gohr * @return bool true if both match 13274dc42f7fSGerrit Uitslag * @throws Exception 13284dc42f7fSGerrit Uitslag * 13294dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 1330b0855b11Sandi */ 1331d868eb89SAndreas Gohrfunction auth_verifyPassword($clear, $crypt) 1332d868eb89SAndreas Gohr{ 13333a0a2d05SAndreas Gohr $pass = new PassHash(); 13343a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1335b0855b11Sandi} 1336340756e4Sandi 1337a0b5b007SChris Smith/** 1338a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1339a0b5b007SChris Smith * 1340a0b5b007SChris Smith * @param string $user username 1341a0b5b007SChris Smith * @param string $pass encrypted password 1342a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1343ab5d26daSAndreas Gohr * @return bool 1344a0b5b007SChris Smith */ 1345d868eb89SAndreas Gohrfunction auth_setCookie($user, $pass, $sticky) 1346d868eb89SAndreas Gohr{ 1347a0b5b007SChris Smith global $conf; 1348e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1349a0b5b007SChris Smith global $auth; 135079d00841SOliver Geisen global $USERINFO; 1351a0b5b007SChris Smith 13526547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 1353a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1354a0b5b007SChris Smith 1355a0b5b007SChris Smith // set cookie 1356645c0a36SAndreas Gohr $cookie = base64_encode($user) . '|' . ((int) $sticky) . '|' . base64_encode($pass); 135773ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1358c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1359bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, $cookie, [ 1360bf8392ebSAndreas Gohr 'expires' => $time, 1361bf8392ebSAndreas Gohr 'path' => $cookieDir, 1362bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 1363bf8392ebSAndreas Gohr 'httponly' => true, 1364486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 1365bf8392ebSAndreas Gohr ]); 136655a71a16SGerrit Uitslag 1367a0b5b007SChris Smith // set session 1368a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1369234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1370a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1371a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1372a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1373ab5d26daSAndreas Gohr 1374ab5d26daSAndreas Gohr return true; 1375a0b5b007SChris Smith} 1376a0b5b007SChris Smith 1377645c0a36SAndreas Gohr/** 1378645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1379645c0a36SAndreas Gohr * 1380645c0a36SAndreas Gohr * @returns array 1381645c0a36SAndreas Gohr */ 1382d868eb89SAndreas Gohrfunction auth_getCookie() 1383d868eb89SAndreas Gohr{ 1384c66972f2SAdrian Lang if (!isset($_COOKIE[DOKU_COOKIE])) { 138524870174SAndreas Gohr return [null, null, null]; 1386c66972f2SAdrian Lang } 138724870174SAndreas Gohr [$user, $sticky, $pass] = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, ''); 1388645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1389645c0a36SAndreas Gohr $pass = base64_decode($pass); 1390645c0a36SAndreas Gohr $user = base64_decode($user); 139124870174SAndreas Gohr return [$user, $sticky, $pass]; 1392645c0a36SAndreas Gohr} 1393645c0a36SAndreas Gohr 1394e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1395