1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * Authentication library 415fae107Sandi * 515fae107Sandi * Including this file will automatically try to login 615fae107Sandi * a user by calling auth_login() 715fae107Sandi * 815fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1015fae107Sandi */ 1124870174SAndreas Gohruse phpseclib\Crypt\AES; 1224870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1396348f27SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 1496348f27SAndreas Gohruse dokuwiki\Extension\Event; 1596348f27SAndreas Gohruse dokuwiki\Extension\PluginController; 16c3cc6e05SAndreas Gohruse dokuwiki\PassHash; 1775d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 18c3cc6e05SAndreas Gohr 1916905344SAndreas Gohr/** 2016905344SAndreas Gohr * Initialize the auth system. 2116905344SAndreas Gohr * 2216905344SAndreas Gohr * This function is automatically called at the end of init.php 2316905344SAndreas Gohr * 2416905344SAndreas Gohr * This used to be the main() of the auth.php 2516905344SAndreas Gohr * 2616905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 2716905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 28ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 29ab5d26daSAndreas Gohr * @return bool 3016905344SAndreas Gohr */ 31*d868eb89SAndreas Gohrfunction auth_setup() 32*d868eb89SAndreas Gohr{ 33742c66f8Schris global $conf; 34e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 3503c4aec3Schris global $auth; 36bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 37bcc94b2cSAndreas Gohr global $INPUT; 389a9714acSDominik Eckelmann global $AUTH_ACL; 399a9714acSDominik Eckelmann global $lang; 403a7140a1SAndreas Gohr /* @var PluginController $plugin_controller */ 419c29eea5SJan Schumann global $plugin_controller; 4224870174SAndreas Gohr $AUTH_ACL = []; 4303c4aec3Schris 4416905344SAndreas Gohr if(!$conf['useacl']) return false; 4516905344SAndreas Gohr 469c29eea5SJan Schumann // try to load auth backend from plugins 479c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 489c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 49f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 509c29eea5SJan Schumann break; 519c29eea5SJan Schumann } 529c29eea5SJan Schumann } 538b06d178Schris 546416b708SMichael Hamann if(!isset($auth) || !$auth){ 553094e817SAndreas Gohr msg($lang['authtempfail'], -1); 563094e817SAndreas Gohr return false; 573094e817SAndreas Gohr } 588b06d178Schris 596416b708SMichael Hamann if ($auth->success == false) { 600f4f4adfSAndreas Gohr // degrade to unauthenticated user 61ecad51ddSAndreas Gohr $auth = null; 620f4f4adfSAndreas Gohr auth_logoff(); 63cd52f92dSchris msg($lang['authtempfail'], -1); 646416b708SMichael Hamann return false; 65d2dde4ebSMatthias Grimm } 6616905344SAndreas Gohr 6716905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 68bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 69bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 70bbbd6568SAndreas Gohr 7162bf3ac0SDamien Regad // Populate Basic Auth user/password from Authorization header 7262bf3ac0SDamien Regad // Note: with FastCGI, data is in REDIRECT_HTTP_AUTHORIZATION instead of HTTP_AUTHORIZATION 7362bf3ac0SDamien Regad $header = $INPUT->server->str('HTTP_AUTHORIZATION') ?: $INPUT->server->str('REDIRECT_HTTP_AUTHORIZATION'); 7462bf3ac0SDamien Regad if(preg_match( '~^Basic ([a-z\d/+]*={0,2})$~i', $header, $matches )) { 7562bf3ac0SDamien Regad $userpass = explode(':', base64_decode($matches[1])); 7624870174SAndreas Gohr [$_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']] = $userpass; 77528ddc7cSAndreas Gohr } 78528ddc7cSAndreas Gohr 791e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 8003062864SAndreas Gohr if (!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($INPUT->server->str('PHP_AUTH_USER'))) { 8103062864SAndreas Gohr $INPUT->set('u', $INPUT->server->str('PHP_AUTH_USER')); 8203062864SAndreas Gohr $INPUT->set('p', $INPUT->server->str('PHP_AUTH_PW')); 83bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 841e8c9c90SAndreas Gohr } 851e8c9c90SAndreas Gohr 86395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 8793a7873eSAndreas Gohr if (true === $auth->success) { 88395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 89395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 90f4476bd9SJan Schumann } 91191bb90aSAndreas Gohr 9281e99965SPhy $ok = null; 938eca974cSAndreas Gohr if (!is_null($auth) && $auth->canDo('external')) { 9481e99965SPhy $ok = $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 9581e99965SPhy } 9681e99965SPhy 9781e99965SPhy if ($ok === null) { 9881e99965SPhy // external trust mechanism not in place, or returns no result, 9981e99965SPhy // then attempt auth_login 10024870174SAndreas Gohr $evdata = [ 101bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 102bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 103bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 104bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 10524870174SAndreas Gohr ]; 106cbb44eabSAndreas Gohr Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 107f5cb575dSAndreas Gohr } 108f5cb575dSAndreas Gohr 10916905344SAndreas Gohr //load ACL into a global array XXX 11075c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 111ab5d26daSAndreas Gohr 112ab5d26daSAndreas Gohr return true; 11375c93b77SAndreas Gohr} 11475c93b77SAndreas Gohr 11575c93b77SAndreas Gohr/** 11675c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 11775c93b77SAndreas Gohr * 11875c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11942ea7f44SGerrit Uitslag * 120ab5d26daSAndreas Gohr * @return array 12175c93b77SAndreas Gohr */ 122*d868eb89SAndreas Gohrfunction auth_loadACL() 123*d868eb89SAndreas Gohr{ 12475c93b77SAndreas Gohr global $config_cascade; 125b78bf706Sromain global $USERINFO; 126585bf44eSChristopher Smith /* @var Input $INPUT */ 127585bf44eSChristopher Smith global $INPUT; 12875c93b77SAndreas Gohr 12924870174SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return []; 13075c93b77SAndreas Gohr 13175c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13275c93b77SAndreas Gohr 13324870174SAndreas Gohr $out = []; 1349ce556d2SAndreas Gohr foreach($acl as $line) { 1359ce556d2SAndreas Gohr $line = trim($line); 1362401f18dSSyntaxseed if(empty($line) || ($line[0] == '#')) continue; // skip blank lines & comments 13724870174SAndreas Gohr [$id, $rest] = preg_split('/[ \t]+/', $line, 2); 13832e82180SAndreas Gohr 139443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 140ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 141ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 142585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 143ad3d68d7SChristopher Smith 144585bf44eSChristopher Smith $id = str_replace('%USER%', cleanID($INPUT->server->str('REMOTE_USER')), $id); 145585bf44eSChristopher Smith $rest = str_replace('%USER%', auth_nameencode($INPUT->server->str('REMOTE_USER')), $rest); 146ad3d68d7SChristopher Smith } 147ad3d68d7SChristopher Smith 148ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1499ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 150ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 15106f34f54SPhy if(isset($USERINFO['grps'])){ 1529ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 153b78bf706Sromain $nid = str_replace('%GROUP%', cleanID($grp), $id); 15432e82180SAndreas Gohr $nrest = str_replace('%GROUP%', '@'.auth_nameencode($grp), $rest); 15532e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 156b78bf706Sromain } 15706f34f54SPhy } 15832e82180SAndreas Gohr } else { 15932e82180SAndreas Gohr $out[] = "$id\t$rest"; 160a8fe108bSGuy Brand } 16111799630Sandi } 1629ce556d2SAndreas Gohr 16332e82180SAndreas Gohr return $out; 164f3f0262cSandi} 165f3f0262cSandi 166ab5d26daSAndreas Gohr/** 167ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 168ab5d26daSAndreas Gohr * 16942ea7f44SGerrit Uitslag * @param array $evdata 170ab5d26daSAndreas Gohr * @return bool 171ab5d26daSAndreas Gohr */ 172*d868eb89SAndreas Gohrfunction auth_login_wrapper($evdata) 173*d868eb89SAndreas Gohr{ 174ab5d26daSAndreas Gohr return auth_login( 175ab5d26daSAndreas Gohr $evdata['user'], 176b5ee21aaSAdrian Lang $evdata['password'], 177b5ee21aaSAdrian Lang $evdata['sticky'], 178ab5d26daSAndreas Gohr $evdata['silent'] 179ab5d26daSAndreas Gohr ); 180b5ee21aaSAdrian Lang} 181b5ee21aaSAdrian Lang 182f3f0262cSandi/** 183f3f0262cSandi * This tries to login the user based on the sent auth credentials 184f3f0262cSandi * 185f3f0262cSandi * The authentication works like this: if a username was given 18615fae107Sandi * a new login is assumed and user/password are checked. If they 18715fae107Sandi * are correct the password is encrypted with blowfish and stored 18815fae107Sandi * together with the username in a cookie - the same info is stored 18915fae107Sandi * in the session, too. Additonally a browserID is stored in the 19015fae107Sandi * session. 19115fae107Sandi * 19215fae107Sandi * If no username was given the cookie is checked: if the username, 19315fae107Sandi * crypted password and browserID match between session and cookie 19415fae107Sandi * no further testing is done and the user is accepted 19515fae107Sandi * 19615fae107Sandi * If a cookie was found but no session info was availabe the 197136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 19815fae107Sandi * together with username rechecked by calling this function again. 199f3f0262cSandi * 200f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 201f3f0262cSandi * are set. 20215fae107Sandi * 20315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20415fae107Sandi * 20515fae107Sandi * @param string $user Username 20615fae107Sandi * @param string $pass Cleartext Password 20715fae107Sandi * @param bool $sticky Cookie should not expire 208f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 20915fae107Sandi * @return bool true on successful auth 210f3f0262cSandi */ 211*d868eb89SAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) 212*d868eb89SAndreas Gohr{ 213f3f0262cSandi global $USERINFO; 214f3f0262cSandi global $conf; 215f3f0262cSandi global $lang; 216e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 217cd52f92dSchris global $auth; 218585bf44eSChristopher Smith /* @var Input $INPUT */ 219585bf44eSChristopher Smith global $INPUT; 220ab5d26daSAndreas Gohr 221beca106aSAdrian Lang if(!$auth) return false; 222beca106aSAdrian Lang 223bbbd6568SAndreas Gohr if(!empty($user)) { 224132bdbfeSandi //usual login 2255e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 226132bdbfeSandi // make logininfo globally available 227585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 22830d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 22904369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 230132bdbfeSandi return true; 231f3f0262cSandi } else { 232f3f0262cSandi //invalid credentials - log off 233f8b1e4e7SAndreas Gohr if(!$silent) { 234f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 235f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 236f8b1e4e7SAndreas Gohr } 237f3f0262cSandi auth_logoff(); 238132bdbfeSandi return false; 239f3f0262cSandi } 240f3f0262cSandi } else { 241132bdbfeSandi // read cookie information 24224870174SAndreas Gohr [$user, $sticky, $pass] = auth_getCookie(); 243132bdbfeSandi if($user && $pass) { 244132bdbfeSandi // we got a cookie - see if we can trust it 245fa7c70ffSAdrian Lang 246fa7c70ffSAdrian Lang // get session info 2470058ae75SDamien Regad if (isset($_SESSION[DOKU_COOKIE])) { 248fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 249132bdbfeSandi if (isset($session) && 2507172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2514c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 252132bdbfeSandi ($session['user'] == $user) && 253234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 254ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 255ab5d26daSAndreas Gohr ) { 256234ce57eSAndreas Gohr 257132bdbfeSandi // he has session, cookie and browser right - let him in 258585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 259132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 260132bdbfeSandi return true; 261132bdbfeSandi } 2620058ae75SDamien Regad } 263f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 26430d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26504369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 266f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 267132bdbfeSandi } 268132bdbfeSandi } 269f3f0262cSandi //just to be sure 270883179a4SAndreas Gohr auth_logoff(true); 271132bdbfeSandi return false; 272f3f0262cSandi} 273132bdbfeSandi 274132bdbfeSandi/** 275136ce040Sandi * Builds a pseudo UID from browser and IP data 276132bdbfeSandi * 277132bdbfeSandi * This is neither unique nor unfakable - still it adds some 278136ce040Sandi * security. Using the first part of the IP makes sure 27980b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 28015fae107Sandi * 28115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 28215fae107Sandi * 283b13c0e1aSAdaKaleh * @return string a SHA256 sum of various browser headers 284132bdbfeSandi */ 285*d868eb89SAndreas Gohrfunction auth_browseruid() 286*d868eb89SAndreas Gohr{ 287585bf44eSChristopher Smith /* @var Input $INPUT */ 288585bf44eSChristopher Smith global $INPUT; 289585bf44eSChristopher Smith 2902f9daf16SAndreas Gohr $ip = clientIP(true); 291b13c0e1aSAdaKaleh // convert IP string to packed binary representation 292b13c0e1aSAdaKaleh $pip = inet_pton($ip); 293b7c67f83SAndreas Gohr 294b7c67f83SAndreas Gohr $uid = implode("\n", [ 295b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_USER_AGENT'), 296b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_ACCEPT_LANGUAGE'), 297b7c67f83SAndreas Gohr substr($pip, 0, strlen($pip) / 2), // use half of the IP address (works for both IPv4 and IPv6) 298b7c67f83SAndreas Gohr ]); 299b13c0e1aSAdaKaleh return hash('sha256', $uid); 300132bdbfeSandi} 301132bdbfeSandi 302132bdbfeSandi/** 303132bdbfeSandi * Creates a random key to encrypt the password in cookies 30415fae107Sandi * 30515fae107Sandi * This function tries to read the password for encrypting 30698407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 30715fae107Sandi * if no such file is found a random key is created and 30815fae107Sandi * and stored in this file. 30915fae107Sandi * 31015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 31142ea7f44SGerrit Uitslag * 31232ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 31330d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 31415fae107Sandi * @return string 315132bdbfeSandi */ 316*d868eb89SAndreas Gohrfunction auth_cookiesalt($addsession = false, $secure = false) 317*d868eb89SAndreas Gohr{ 318a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 319fe745becSMichael Große return 'test'; 320a1fe3c9cSMichael Große } 321132bdbfeSandi global $conf; 32298407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 32330d544a4SMichael Hamann if ($secure || !file_exists($file)) { 32430d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 32530d544a4SMichael Hamann } 326132bdbfeSandi $salt = io_readFile($file); 327132bdbfeSandi if(empty($salt)) { 32830d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 329132bdbfeSandi io_saveFile($file, $salt); 330132bdbfeSandi } 33132ed2b36SAndreas Gohr if($addsession) { 33232ed2b36SAndreas Gohr $salt .= session_id(); 33332ed2b36SAndreas Gohr } 334132bdbfeSandi return $salt; 335f3f0262cSandi} 336f3f0262cSandi 337f3f0262cSandi/** 3387a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 339483b6238SMichael Hamann * 3407a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 34142ea7f44SGerrit Uitslag * 3427a33d2f8SNiklas Keller * @param int $length number of bytes 3437a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 344483b6238SMichael Hamann */ 345*d868eb89SAndreas Gohrfunction auth_randombytes($length) 346*d868eb89SAndreas Gohr{ 3477a33d2f8SNiklas Keller return random_bytes($length); 348483b6238SMichael Hamann} 349483b6238SMichael Hamann 350483b6238SMichael Hamann/** 3517a33d2f8SNiklas Keller * Cryptographically secure random number generator. 352483b6238SMichael Hamann * 3537a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 35442ea7f44SGerrit Uitslag * 355483b6238SMichael Hamann * @param int $min 356483b6238SMichael Hamann * @param int $max 357483b6238SMichael Hamann * @return int 358483b6238SMichael Hamann */ 359*d868eb89SAndreas Gohrfunction auth_random($min, $max) 360*d868eb89SAndreas Gohr{ 3617a33d2f8SNiklas Keller return random_int($min, $max); 362483b6238SMichael Hamann} 363483b6238SMichael Hamann 364483b6238SMichael Hamann/** 36504369c3eSMichael Hamann * Encrypt data using the given secret using AES 36604369c3eSMichael Hamann * 36704369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 36804369c3eSMichael Hamann * using pbkdf2. 36904369c3eSMichael Hamann * 37004369c3eSMichael Hamann * @param string $data The data that shall be encrypted 37104369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 37204369c3eSMichael Hamann * @return string The ciphertext 37304369c3eSMichael Hamann */ 374*d868eb89SAndreas Gohrfunction auth_encrypt($data, $secret) 375*d868eb89SAndreas Gohr{ 37604369c3eSMichael Hamann $iv = auth_randombytes(16); 37724870174SAndreas Gohr $cipher = new AES(); 37804369c3eSMichael Hamann $cipher->setPassword($secret); 37904369c3eSMichael Hamann 3807b650cefSMichael Hamann /* 3817b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 3827b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 3837b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 3847b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 3857b650cefSMichael Hamann */ 38604369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 38704369c3eSMichael Hamann} 38804369c3eSMichael Hamann 38904369c3eSMichael Hamann/** 39004369c3eSMichael Hamann * Decrypt the given AES ciphertext 39104369c3eSMichael Hamann * 39204369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 39304369c3eSMichael Hamann * 39404369c3eSMichael Hamann * @param string $ciphertext The encrypted data 39504369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 39604369c3eSMichael Hamann * @return string The decrypted data 39704369c3eSMichael Hamann */ 398*d868eb89SAndreas Gohrfunction auth_decrypt($ciphertext, $secret) 399*d868eb89SAndreas Gohr{ 4007b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 40124870174SAndreas Gohr $cipher = new AES(); 40204369c3eSMichael Hamann $cipher->setPassword($secret); 4037b650cefSMichael Hamann $cipher->setIV($iv); 40404369c3eSMichael Hamann 4057b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 40604369c3eSMichael Hamann} 40704369c3eSMichael Hamann 40804369c3eSMichael Hamann/** 409883179a4SAndreas Gohr * Log out the current user 410883179a4SAndreas Gohr * 411f3f0262cSandi * This clears all authentication data and thus log the user 412883179a4SAndreas Gohr * off. It also clears session data. 41315fae107Sandi * 41415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 41542ea7f44SGerrit Uitslag * 416883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 417f3f0262cSandi */ 418*d868eb89SAndreas Gohrfunction auth_logoff($keepbc = false) 419*d868eb89SAndreas Gohr{ 420f3f0262cSandi global $conf; 421f3f0262cSandi global $USERINFO; 422e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4235298a619SAndreas Gohr global $auth; 424585bf44eSChristopher Smith /* @var Input $INPUT */ 425585bf44eSChristopher Smith global $INPUT; 42637065e65Sandi 427d4869846SAndreas Gohr // make sure the session is writable (it usually is) 428e9621d07SAndreas Gohr @session_start(); 429e9621d07SAndreas Gohr 430e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 431e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 432e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 433e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 434e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 435e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 436883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 437e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 438585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 439132bdbfeSandi $USERINFO = null; //FIXME 440f5c6743cSAndreas Gohr 44173ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 442bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, '', [ 443bf8392ebSAndreas Gohr 'expires' => time() - 600000, 444bf8392ebSAndreas Gohr 'path' => $cookieDir, 445bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 446bf8392ebSAndreas Gohr 'httponly' => true, 447486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 448bf8392ebSAndreas Gohr ]); 4495298a619SAndreas Gohr 450880f62faSAndreas Gohr if($auth) $auth->logOff(); 451f3f0262cSandi} 452f3f0262cSandi 453f3f0262cSandi/** 454f8cc712eSAndreas Gohr * Check if a user is a manager 455f8cc712eSAndreas Gohr * 456f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 457f8cc712eSAndreas Gohr * user. 458f8cc712eSAndreas Gohr * 459f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 460f8cc712eSAndreas Gohr * 461ab5d26daSAndreas Gohr * @param string $user Username 462ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 463ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 46410396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 465ab5d26daSAndreas Gohr * @return bool 46696348f27SAndreas Gohr * @see auth_isadmin 46796348f27SAndreas Gohr * 46896348f27SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 469f8cc712eSAndreas Gohr */ 470*d868eb89SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache = false) 471*d868eb89SAndreas Gohr{ 472f8cc712eSAndreas Gohr global $conf; 473f8cc712eSAndreas Gohr global $USERINFO; 474e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 475d752aedeSAndreas Gohr global $auth; 476585bf44eSChristopher Smith /* @var Input $INPUT */ 477585bf44eSChristopher Smith global $INPUT; 478585bf44eSChristopher Smith 479f8cc712eSAndreas Gohr 480beca106aSAdrian Lang if(!$auth) return false; 481c66972f2SAdrian Lang if(is_null($user)) { 482585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 483c66972f2SAdrian Lang return false; 484c66972f2SAdrian Lang } else { 485585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 486c66972f2SAdrian Lang } 487c66972f2SAdrian Lang } 488d6dc956fSAndreas Gohr if (is_null($groups)) { 4891525c228SAnna Dabrowska // checking the logged in user, or another one? 4901525c228SAnna Dabrowska if ($USERINFO && $user === $INPUT->server->str('REMOTE_USER')) { 4911525c228SAnna Dabrowska $groups = (array) $USERINFO['grps']; 49266b108d6SAnna Dabrowska } else { 4936cf7b139SAndreas Gohr $groups = $auth->getUserData($user); 4946cf7b139SAndreas Gohr $groups = $groups ? $groups['grps'] : []; 49566b108d6SAnna Dabrowska } 496e259aa79SAndreas Gohr } 497e259aa79SAndreas Gohr 49896348f27SAndreas Gohr // prefer cached result 49996348f27SAndreas Gohr static $cache = []; 50010396f77SAndreas Gohr $cachekey = serialize([$user, $adminonly, $groups]); 50196348f27SAndreas Gohr if (!isset($cache[$cachekey]) || $recache) { 502d6dc956fSAndreas Gohr // check superuser match 50396348f27SAndreas Gohr $ok = auth_isMember($conf['superuser'], $user, $groups); 50400ce12daSChris Smith 50596348f27SAndreas Gohr // check managers 50696348f27SAndreas Gohr if (!$ok && !$adminonly) { 50796348f27SAndreas Gohr $ok = auth_isMember($conf['manager'], $user, $groups); 50896348f27SAndreas Gohr } 50996348f27SAndreas Gohr 51096348f27SAndreas Gohr $cache[$cachekey] = $ok; 51196348f27SAndreas Gohr } 51296348f27SAndreas Gohr 51396348f27SAndreas Gohr return $cache[$cachekey]; 514f8cc712eSAndreas Gohr} 515f8cc712eSAndreas Gohr 516f8cc712eSAndreas Gohr/** 517f8cc712eSAndreas Gohr * Check if a user is admin 518f8cc712eSAndreas Gohr * 519f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 520f8cc712eSAndreas Gohr * 521f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 522f8cc712eSAndreas Gohr * 52396348f27SAndreas Gohr * @param string $user Username 52496348f27SAndreas Gohr * @param array $groups List of groups the user is in 52510396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 52696348f27SAndreas Gohr * @return bool 527f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 528ab5d26daSAndreas Gohr * @see auth_ismanager() 52942ea7f44SGerrit Uitslag * 530f8cc712eSAndreas Gohr */ 531*d868eb89SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache = false) 532*d868eb89SAndreas Gohr{ 53396348f27SAndreas Gohr return auth_ismanager($user, $groups, true, $recache); 534f8cc712eSAndreas Gohr} 535f8cc712eSAndreas Gohr 536d6dc956fSAndreas Gohr/** 537d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 538d6dc956fSAndreas Gohr * users and groups to determine membership status 539d6dc956fSAndreas Gohr * 540d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 541d6dc956fSAndreas Gohr * 54242ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 54342ea7f44SGerrit Uitslag * @param string $user user to match against 54442ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5455446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 546d6dc956fSAndreas Gohr */ 547*d868eb89SAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) 548*d868eb89SAndreas Gohr{ 549e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 550d6dc956fSAndreas Gohr global $auth; 551d6dc956fSAndreas Gohr if(!$auth) return false; 552d6dc956fSAndreas Gohr 553d6dc956fSAndreas Gohr // clean user and groups 5544f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 55524870174SAndreas Gohr $user = PhpString::strtolower($user); 55624870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 557d6dc956fSAndreas Gohr } 558d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 55924870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 560d6dc956fSAndreas Gohr 561d6dc956fSAndreas Gohr // extract the memberlist 562d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 563d6dc956fSAndreas Gohr $members = array_map('trim', $members); 564d6dc956fSAndreas Gohr $members = array_unique($members); 565d6dc956fSAndreas Gohr $members = array_filter($members); 566d6dc956fSAndreas Gohr 567d6dc956fSAndreas Gohr // compare cleaned values 568d6dc956fSAndreas Gohr foreach($members as $member) { 569e5204a12SJurgen Hart if($member == '@ALL' ) return true; 57024870174SAndreas Gohr if(!$auth->isCaseSensitive()) $member = PhpString::strtolower($member); 571d6dc956fSAndreas Gohr if($member[0] == '@') { 572d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 573d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 574d6dc956fSAndreas Gohr } else { 575d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 576d6dc956fSAndreas Gohr if($member == $user) return true; 577d6dc956fSAndreas Gohr } 578d6dc956fSAndreas Gohr } 579d6dc956fSAndreas Gohr 580d6dc956fSAndreas Gohr // still here? not a member! 581d6dc956fSAndreas Gohr return false; 582d6dc956fSAndreas Gohr} 583d6dc956fSAndreas Gohr 584f8cc712eSAndreas Gohr/** 58515fae107Sandi * Convinience function for auth_aclcheck() 58615fae107Sandi * 58715fae107Sandi * This checks the permissions for the current user 58815fae107Sandi * 58915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 59015fae107Sandi * 5911698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 59215fae107Sandi * @return int permission level 593f3f0262cSandi */ 594*d868eb89SAndreas Gohrfunction auth_quickaclcheck($id) 595*d868eb89SAndreas Gohr{ 596f3f0262cSandi global $conf; 597f3f0262cSandi global $USERINFO; 598585bf44eSChristopher Smith /* @var Input $INPUT */ 599585bf44eSChristopher Smith global $INPUT; 600f3f0262cSandi # if no ACL is used always return upload rights 601f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 60224870174SAndreas Gohr return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), is_array($USERINFO) ? $USERINFO['grps'] : []); 603f3f0262cSandi} 604f3f0262cSandi 605f3f0262cSandi/** 606c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 60715fae107Sandi * 60815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 60942ea7f44SGerrit Uitslag * 610c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6111698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 61215fae107Sandi * @param string $user Username 6133272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 61415fae107Sandi * @return int permission level 615f3f0262cSandi */ 616*d868eb89SAndreas Gohrfunction auth_aclcheck($id, $user, $groups) 617*d868eb89SAndreas Gohr{ 61824870174SAndreas Gohr $data = [ 619bf8f8509SAndreas Gohr 'id' => $id ?? '', 620c17acc9fSAndreas Gohr 'user' => $user, 621c17acc9fSAndreas Gohr 'groups' => $groups 62224870174SAndreas Gohr ]; 623c17acc9fSAndreas Gohr 624cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 625c17acc9fSAndreas Gohr} 626c17acc9fSAndreas Gohr 627c17acc9fSAndreas Gohr/** 628c17acc9fSAndreas Gohr * default ACL check method 629c17acc9fSAndreas Gohr * 630c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 631c17acc9fSAndreas Gohr * 632c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 63342ea7f44SGerrit Uitslag * 634c17acc9fSAndreas Gohr * @param array $data event data 635c17acc9fSAndreas Gohr * @return int permission level 636c17acc9fSAndreas Gohr */ 637*d868eb89SAndreas Gohrfunction auth_aclcheck_cb($data) 638*d868eb89SAndreas Gohr{ 639c17acc9fSAndreas Gohr $id =& $data['id']; 640c17acc9fSAndreas Gohr $user =& $data['user']; 641c17acc9fSAndreas Gohr $groups =& $data['groups']; 642c17acc9fSAndreas Gohr 643f3f0262cSandi global $conf; 644f3f0262cSandi global $AUTH_ACL; 645e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 646d752aedeSAndreas Gohr global $auth; 647f3f0262cSandi 64885d03f68SAndreas Gohr // if no ACL is used always return upload rights 649f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 650beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 651bf8f8509SAndreas Gohr if(!is_array($AUTH_ACL)) return AUTH_NONE; 652f3f0262cSandi 653074cf26bSandi //make sure groups is an array 65424870174SAndreas Gohr if(!is_array($groups)) $groups = []; 655074cf26bSandi 65685d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 657ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 658ab5d26daSAndreas Gohr return AUTH_ADMIN; 659ab5d26daSAndreas Gohr } 66085d03f68SAndreas Gohr 661eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 66224870174SAndreas Gohr $user = PhpString::strtolower($user); 66324870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 664eb3ce0d5SKazutaka Miyasaka } 66537ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 66624870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 66785d03f68SAndreas Gohr 6686c2bb100SAndreas Gohr //prepend groups with @ and nameencode 66937ff2261SSascha Klopp foreach($groups as &$group) { 67037ff2261SSascha Klopp $group = '@'.auth_nameencode($group); 67110a76f6fSfrank } 67210a76f6fSfrank 673f3f0262cSandi $ns = getNS($id); 674f3f0262cSandi $perm = -1; 675f3f0262cSandi 676f3f0262cSandi //add ALL group 677f3f0262cSandi $groups[] = '@ALL'; 67837ff2261SSascha Klopp 679f3f0262cSandi //add User 68034aeb4afSAndreas Gohr if($user) $groups[] = $user; 681f3f0262cSandi 682f3f0262cSandi //check exact match first 68321c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 684f3f0262cSandi if(count($matches)) { 685f3f0262cSandi foreach($matches as $match) { 686f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 68721c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 688eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 68924870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 690eb3ce0d5SKazutaka Miyasaka } 69148d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 69248d7b7a6SDominik Eckelmann continue; 69348d7b7a6SDominik Eckelmann } 6948ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 695f3f0262cSandi if($acl[2] > $perm) { 696f3f0262cSandi $perm = $acl[2]; 697f3f0262cSandi } 698f3f0262cSandi } 699f3f0262cSandi if($perm > -1) { 700f3f0262cSandi //we had a match - return it 701def492a2SGuillaume Turri return (int) $perm; 702f3f0262cSandi } 703f3f0262cSandi } 704f3f0262cSandi 705f3f0262cSandi //still here? do the namespace checks 706f3f0262cSandi if($ns) { 7073e304b55SMichael Hamann $path = $ns.':*'; 708f3f0262cSandi } else { 7093e304b55SMichael Hamann $path = '*'; //root document 710f3f0262cSandi } 711f3f0262cSandi 712f3f0262cSandi do { 71321c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 714f3f0262cSandi if(count($matches)) { 715f3f0262cSandi foreach($matches as $match) { 716f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 71721c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 718eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 71924870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 720eb3ce0d5SKazutaka Miyasaka } 72148d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 72248d7b7a6SDominik Eckelmann continue; 72348d7b7a6SDominik Eckelmann } 7248ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 725f3f0262cSandi if($acl[2] > $perm) { 726f3f0262cSandi $perm = $acl[2]; 727f3f0262cSandi } 728f3f0262cSandi } 729f3f0262cSandi //we had a match - return it 73048d7b7a6SDominik Eckelmann if($perm != -1) { 731def492a2SGuillaume Turri return (int) $perm; 732f3f0262cSandi } 73348d7b7a6SDominik Eckelmann } 734f3f0262cSandi //get next higher namespace 735f3f0262cSandi $ns = getNS($ns); 736f3f0262cSandi 7373e304b55SMichael Hamann if($path != '*') { 7383e304b55SMichael Hamann $path = $ns.':*'; 7393e304b55SMichael Hamann if($path == ':*') $path = '*'; 740f3f0262cSandi } else { 741f3f0262cSandi //we did this already 742f3f0262cSandi //looks like there is something wrong with the ACL 743f3f0262cSandi //break here 744d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 745d5ce66f6SAndreas Gohr return AUTH_NONE; 746f3f0262cSandi } 747f3f0262cSandi } while(1); //this should never loop endless 748ab5d26daSAndreas Gohr return AUTH_NONE; 749f3f0262cSandi} 750f3f0262cSandi 751f3f0262cSandi/** 7526c2bb100SAndreas Gohr * Encode ASCII special chars 7536c2bb100SAndreas Gohr * 7546c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7556c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7566c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7576c2bb100SAndreas Gohr * urlencoding!). 7586c2bb100SAndreas Gohr * 7596c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7606c2bb100SAndreas Gohr * 7616c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7626c2bb100SAndreas Gohr * @see rawurldecode() 76342ea7f44SGerrit Uitslag * 76442ea7f44SGerrit Uitslag * @param string $name 76542ea7f44SGerrit Uitslag * @param bool $skip_group 76642ea7f44SGerrit Uitslag * @return string 7676c2bb100SAndreas Gohr */ 768*d868eb89SAndreas Gohrfunction auth_nameencode($name, $skip_group = false) 769*d868eb89SAndreas Gohr{ 770a424cd8eSchris global $cache_authname; 771a424cd8eSchris $cache =& $cache_authname; 77231784267SAndreas Gohr $name = (string) $name; 773a424cd8eSchris 77480601d26SAndreas Gohr // never encode wildcard FS#1955 77580601d26SAndreas Gohr if($name == '%USER%') return $name; 776b78bf706Sromain if($name == '%GROUP%') return $name; 77780601d26SAndreas Gohr 778a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 7792401f18dSSyntaxseed if($skip_group && $name[0] == '@') { 78030f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 78130f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 78230f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 783ab5d26daSAndreas Gohr ); 784e838fc2eSAndreas Gohr } else { 78530f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 78630f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 78730f6faf0SChristopher Smith 'auth_nameencode_callback', $name 788ab5d26daSAndreas Gohr ); 789e838fc2eSAndreas Gohr } 7906c2bb100SAndreas Gohr } 7916c2bb100SAndreas Gohr 792a424cd8eSchris return $cache[$name][$skip_group]; 793a424cd8eSchris} 794a424cd8eSchris 79504d68ae4SGerrit Uitslag/** 79604d68ae4SGerrit Uitslag * callback encodes the matches 79704d68ae4SGerrit Uitslag * 79804d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 79904d68ae4SGerrit Uitslag * @return string 80004d68ae4SGerrit Uitslag */ 801*d868eb89SAndreas Gohrfunction auth_nameencode_callback($matches) 802*d868eb89SAndreas Gohr{ 80330f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1], -1))); 80430f6faf0SChristopher Smith} 80530f6faf0SChristopher Smith 8066c2bb100SAndreas Gohr/** 807f3f0262cSandi * Create a pronouncable password 808f3f0262cSandi * 8098a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8108a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8118a285f7fSAndreas Gohr * 81215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 81315fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8148a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 81515fae107Sandi * 8168a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 81715fae107Sandi * @return string pronouncable password 818f3f0262cSandi */ 819*d868eb89SAndreas Gohrfunction auth_pwgen($foruser = '') 820*d868eb89SAndreas Gohr{ 82124870174SAndreas Gohr $data = [ 822d628dcf3SAndreas Gohr 'password' => '', 823d628dcf3SAndreas Gohr 'foruser' => $foruser 82424870174SAndreas Gohr ]; 8258a285f7fSAndreas Gohr 826e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 8278a285f7fSAndreas Gohr if($evt->advise_before(true)) { 828f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 829f3f0262cSandi $v = 'aeiou'; //vowels 830f3f0262cSandi $a = $c.$v; //both 831987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 832f3f0262cSandi 833987c8d26SAndreas Gohr //use thre syllables... 834987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 835483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 836483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 837483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 838f3f0262cSandi } 839987c8d26SAndreas Gohr //... and add a nice number and special 84043f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)].auth_random(10, 99); 8418a285f7fSAndreas Gohr } 8428a285f7fSAndreas Gohr $evt->advise_after(); 843f3f0262cSandi 8448a285f7fSAndreas Gohr return $data['password']; 845f3f0262cSandi} 846f3f0262cSandi 847f3f0262cSandi/** 848f3f0262cSandi * Sends a password to the given user 849f3f0262cSandi * 85015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 85142ea7f44SGerrit Uitslag * 852ab5d26daSAndreas Gohr * @param string $user Login name of the user 853ab5d26daSAndreas Gohr * @param string $password The new password in clear text 85415fae107Sandi * @return bool true on success 855f3f0262cSandi */ 856*d868eb89SAndreas Gohrfunction auth_sendPassword($user, $password) 857*d868eb89SAndreas Gohr{ 858f3f0262cSandi global $lang; 859e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 860cd52f92dSchris global $auth; 861beca106aSAdrian Lang if(!$auth) return false; 862cd52f92dSchris 863d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8642dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 865f3f0262cSandi 86687ddda95Sandi if(!$userinfo['mail']) return false; 867f3f0262cSandi 868f3f0262cSandi $text = rawLocale('password'); 86924870174SAndreas Gohr $trep = [ 870d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 871d7169d19SAndreas Gohr 'LOGIN' => $user, 872d7169d19SAndreas Gohr 'PASSWORD' => $password 87324870174SAndreas Gohr ]; 874f3f0262cSandi 875d7169d19SAndreas Gohr $mail = new Mailer(); 876102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']).' <'.$userinfo['mail'].'>'); 877d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 878d7169d19SAndreas Gohr $mail->setBody($text, $trep); 879d7169d19SAndreas Gohr return $mail->send(); 880f3f0262cSandi} 881f3f0262cSandi 882f3f0262cSandi/** 88315fae107Sandi * Register a new user 884f3f0262cSandi * 88515fae107Sandi * This registers a new user - Data is read directly from $_POST 88615fae107Sandi * 88715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 88842ea7f44SGerrit Uitslag * 88915fae107Sandi * @return bool true on success, false on any error 890f3f0262cSandi */ 891*d868eb89SAndreas Gohrfunction register() 892*d868eb89SAndreas Gohr{ 893f3f0262cSandi global $lang; 894eb5d07e4Sjan global $conf; 895e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 896cd52f92dSchris global $auth; 89764273335SAndreas Gohr global $INPUT; 898f3f0262cSandi 89964273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9003a48618aSAnika Henke if(!actionOK('register')) return false; 901640145a5Sandi 90264273335SAndreas Gohr // gather input 90364273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 90464273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 90564273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 90664273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 90764273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 908d752aedeSAndreas Gohr 90964273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 910f3f0262cSandi msg($lang['regmissing'], -1); 911f3f0262cSandi return false; 912f3f0262cSandi } 913f3f0262cSandi 914cab2716aSmatthias.grimm if($conf['autopasswd']) { 9158a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 91664273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 917bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 918cab2716aSmatthias.grimm return false; 91964273335SAndreas Gohr } elseif($pass != $passchk) { 920bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 921cab2716aSmatthias.grimm return false; 922cab2716aSmatthias.grimm } 923cab2716aSmatthias.grimm 924f3f0262cSandi //check mail 92564273335SAndreas Gohr if(!mail_isvalid($email)) { 926f3f0262cSandi msg($lang['regbadmail'], -1); 927f3f0262cSandi return false; 928f3f0262cSandi } 929f3f0262cSandi 930f3f0262cSandi //okay try to create the user 93124870174SAndreas Gohr if(!$auth->triggerUserMod('create', [$login, $pass, $fullname, $email])) { 932db9faf02SPatrick Brown msg($lang['regfail'], -1); 933f3f0262cSandi return false; 934f3f0262cSandi } 935f3f0262cSandi 936790b7720SAndreas Gohr // send notification about the new user 93775d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 93875d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 93902a498e7Schris 940790b7720SAndreas Gohr // are we done? 941cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 942cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 943cab2716aSmatthias.grimm return true; 944cab2716aSmatthias.grimm } 945cab2716aSmatthias.grimm 946790b7720SAndreas Gohr // autogenerated password? then send password to user 94764273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 948f3f0262cSandi msg($lang['regsuccess'], 1); 949f3f0262cSandi return true; 950f3f0262cSandi } else { 951f3f0262cSandi msg($lang['regmailfail'], -1); 952f3f0262cSandi return false; 953f3f0262cSandi } 954f3f0262cSandi} 955f3f0262cSandi 95610a76f6fSfrank/** 9578b06d178Schris * Update user profile 9588b06d178Schris * 9598b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9608b06d178Schris */ 961*d868eb89SAndreas Gohrfunction updateprofile() 962*d868eb89SAndreas Gohr{ 9638b06d178Schris global $conf; 9648b06d178Schris global $lang; 965e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 966cd52f92dSchris global $auth; 967bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 968bcc94b2cSAndreas Gohr global $INPUT; 9698b06d178Schris 970bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9711b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9728b06d178Schris 9733a48618aSAnika Henke if(!actionOK('profile')) { 9748b06d178Schris msg($lang['profna'], -1); 9758b06d178Schris return false; 9768b06d178Schris } 9778b06d178Schris 97824870174SAndreas Gohr $changes = []; 979bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 980bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 981bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 982bcc94b2cSAndreas Gohr 983bcc94b2cSAndreas Gohr // check misspelled passwords 984bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 985bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 9868b06d178Schris return false; 9878b06d178Schris } 9888b06d178Schris 9898b06d178Schris // clean fullname and email 990bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 991bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 9928b06d178Schris 993bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 994bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 995bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 996ab5d26daSAndreas Gohr ) { 9978b06d178Schris msg($lang['profnoempty'], -1); 9988b06d178Schris return false; 9998b06d178Schris } 1000bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10018b06d178Schris msg($lang['regbadmail'], -1); 10028b06d178Schris return false; 10038b06d178Schris } 10048b06d178Schris 1005bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10064c21b7eeSAndreas Gohr 1007bcc94b2cSAndreas Gohr // check for unavailable capabilities 1008bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 1009bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 1010bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 1011bcc94b2cSAndreas Gohr 1012bcc94b2cSAndreas Gohr // anything to do? 101324870174SAndreas Gohr if($changes === []) { 10148b06d178Schris msg($lang['profnochange'], -1); 10158b06d178Schris return false; 10168b06d178Schris } 10178b06d178Schris 10188b06d178Schris if($conf['profileconfirm']) { 1019585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 102071422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10218b06d178Schris return false; 10228b06d178Schris } 10238b06d178Schris } 10248b06d178Schris 102524870174SAndreas Gohr if(!$auth->triggerUserMod('modify', [$INPUT->server->str('REMOTE_USER'), &$changes])) { 1026db9faf02SPatrick Brown msg($lang['proffail'], -1); 1027db9faf02SPatrick Brown return false; 1028db9faf02SPatrick Brown } 1029db9faf02SPatrick Brown 103032ed2b36SAndreas Gohr if($changes['pass']) { 1031c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 103224870174SAndreas Gohr [, $sticky, ] = auth_getCookie(); 103304369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1034585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1035c276e9e8SMarcel Pennewiss } else { 1036c276e9e8SMarcel Pennewiss // make sure the session is writable 1037c276e9e8SMarcel Pennewiss @session_start(); 1038c276e9e8SMarcel Pennewiss // invalidate session cache 1039c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1040c276e9e8SMarcel Pennewiss session_write_close(); 104132ed2b36SAndreas Gohr } 1042c276e9e8SMarcel Pennewiss 104325b2a98cSMichael Klier return true; 1044a0b5b007SChris Smith} 1045ab5d26daSAndreas Gohr 104604d68ae4SGerrit Uitslag/** 104704d68ae4SGerrit Uitslag * Delete the current logged-in user 104804d68ae4SGerrit Uitslag * 104904d68ae4SGerrit Uitslag * @return bool true on success, false on any error 105004d68ae4SGerrit Uitslag */ 1051*d868eb89SAndreas Gohrfunction auth_deleteprofile() 1052*d868eb89SAndreas Gohr{ 10532a7abf2dSChristopher Smith global $conf; 10542a7abf2dSChristopher Smith global $lang; 1055e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 10562a7abf2dSChristopher Smith global $auth; 10572a7abf2dSChristopher Smith /* @var Input $INPUT */ 10582a7abf2dSChristopher Smith global $INPUT; 10592a7abf2dSChristopher Smith 10602a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 10612a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 10622a7abf2dSChristopher Smith 10632a7abf2dSChristopher Smith // action prevented or auth module disallows 10642a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10652a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10662a7abf2dSChristopher Smith return false; 10672a7abf2dSChristopher Smith } 10682a7abf2dSChristopher Smith 10692a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 10702a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10712a7abf2dSChristopher Smith return false; 10722a7abf2dSChristopher Smith } 10732a7abf2dSChristopher Smith 10742a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1075585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10762a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10772a7abf2dSChristopher Smith return false; 10782a7abf2dSChristopher Smith } 10792a7abf2dSChristopher Smith } 10802a7abf2dSChristopher Smith 108124870174SAndreas Gohr $deleted = []; 1082585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 108324870174SAndreas Gohr if($auth->triggerUserMod('delete', [$deleted])) { 10842a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 10852a7abf2dSChristopher Smith auth_logoff(); 10862a7abf2dSChristopher Smith return true; 10872a7abf2dSChristopher Smith } 10882a7abf2dSChristopher Smith 10892a7abf2dSChristopher Smith return false; 10902a7abf2dSChristopher Smith} 10912a7abf2dSChristopher Smith 10928b06d178Schris/** 10938b06d178Schris * Send a new password 10948b06d178Schris * 10951d5856cfSAndreas Gohr * This function handles both phases of the password reset: 10961d5856cfSAndreas Gohr * 10971d5856cfSAndreas Gohr * - handling the first request of password reset 10981d5856cfSAndreas Gohr * - validating the password reset auth token 10991d5856cfSAndreas Gohr * 11008b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11018b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11021d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11038b06d178Schris * 11048b06d178Schris * @return bool true on success, false on any error 11058b06d178Schris */ 1106*d868eb89SAndreas Gohrfunction act_resendpwd() 1107*d868eb89SAndreas Gohr{ 11088b06d178Schris global $lang; 11098b06d178Schris global $conf; 1110e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1111cd52f92dSchris global $auth; 1112bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1113bcc94b2cSAndreas Gohr global $INPUT; 11148b06d178Schris 11153a48618aSAnika Henke if(!actionOK('resendpwd')) { 11168b06d178Schris msg($lang['resendna'], -1); 11178b06d178Schris return false; 11188b06d178Schris } 11198b06d178Schris 1120bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11218b06d178Schris 11221d5856cfSAndreas Gohr if($token) { 1123cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11241d5856cfSAndreas Gohr 11252401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 112679e79377SAndreas Gohr if(!file_exists($tfile)) { 11271d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1128bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11291d5856cfSAndreas Gohr return false; 11301d5856cfSAndreas Gohr } 11318a9735e3SAndreas Gohr // token is only valid for 3 days 11328a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11338a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1134bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11351d5856cfSAndreas Gohr @unlink($tfile); 11368a9735e3SAndreas Gohr return false; 11378a9735e3SAndreas Gohr } 11388a9735e3SAndreas Gohr 11398b06d178Schris $user = io_readfile($tfile); 11402dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11418b06d178Schris if(!$userinfo['mail']) { 11428b06d178Schris msg($lang['resendpwdnouser'], -1); 11438b06d178Schris return false; 11448b06d178Schris } 11458b06d178Schris 1146cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1147bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1148bcc94b2cSAndreas Gohr 1149cc204bbdSAndreas Gohr // password given correctly? 1150bcc94b2cSAndreas Gohr if(!$pass) return false; 1151bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1152451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1153cc204bbdSAndreas Gohr return false; 1154cc204bbdSAndreas Gohr } 1155cc204bbdSAndreas Gohr 1156bcc94b2cSAndreas Gohr // change it 115724870174SAndreas Gohr if(!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1158db9faf02SPatrick Brown msg($lang['proffail'], -1); 1159cc204bbdSAndreas Gohr return false; 1160cc204bbdSAndreas Gohr } 1161cc204bbdSAndreas Gohr 1162cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1163cc204bbdSAndreas Gohr 11648a285f7fSAndreas Gohr $pass = auth_pwgen($user); 116524870174SAndreas Gohr if(!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1166db9faf02SPatrick Brown msg($lang['proffail'], -1); 11678b06d178Schris return false; 11688b06d178Schris } 11698b06d178Schris 11708b06d178Schris if(auth_sendPassword($user, $pass)) { 11718b06d178Schris msg($lang['resendpwdsuccess'], 1); 11728b06d178Schris } else { 11738b06d178Schris msg($lang['regmailfail'], -1); 11748b06d178Schris } 1175cc204bbdSAndreas Gohr } 1176cc204bbdSAndreas Gohr 1177cc204bbdSAndreas Gohr @unlink($tfile); 11788b06d178Schris return true; 11791d5856cfSAndreas Gohr 11801d5856cfSAndreas Gohr } else { 11811d5856cfSAndreas Gohr // we're in request phase 11821d5856cfSAndreas Gohr 1183bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 11841d5856cfSAndreas Gohr 1185bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 11861d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 11871d5856cfSAndreas Gohr return false; 11881d5856cfSAndreas Gohr } else { 1189bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 11901d5856cfSAndreas Gohr } 11911d5856cfSAndreas Gohr 11922dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11931d5856cfSAndreas Gohr if(!$userinfo['mail']) { 11941d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 11951d5856cfSAndreas Gohr return false; 11961d5856cfSAndreas Gohr } 11971d5856cfSAndreas Gohr 11981d5856cfSAndreas Gohr // generate auth token 1199483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12002401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 120124870174SAndreas Gohr $url = wl('', ['do'=> 'resendpwd', 'pwauth'=> $token], true, '&'); 12021d5856cfSAndreas Gohr 12031d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12041d5856cfSAndreas Gohr 12051d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 120624870174SAndreas Gohr $trep = ['FULLNAME' => $userinfo['name'], 'LOGIN' => $user, 'CONFIRM' => $url]; 12071d5856cfSAndreas Gohr 1208d7169d19SAndreas Gohr $mail = new Mailer(); 1209d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1210d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1211d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1212d7169d19SAndreas Gohr if($mail->send()) { 12131d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12141d5856cfSAndreas Gohr } else { 12151d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12161d5856cfSAndreas Gohr } 12171d5856cfSAndreas Gohr return true; 12181d5856cfSAndreas Gohr } 1219ab5d26daSAndreas Gohr // never reached 12208b06d178Schris} 12218b06d178Schris 12228b06d178Schris/** 1223b0855b11Sandi * Encrypts a password using the given method and salt 1224b0855b11Sandi * 1225b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1226b0855b11Sandi * is chosen. 1227b0855b11Sandi * 1228b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 122942ea7f44SGerrit Uitslag * 1230ab5d26daSAndreas Gohr * @param string $clear The clear text password 1231ab5d26daSAndreas Gohr * @param string $method The hashing method 1232ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1233b0855b11Sandi * @return string The crypted password 1234b0855b11Sandi */ 1235*d868eb89SAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) 1236*d868eb89SAndreas Gohr{ 1237b0855b11Sandi global $conf; 1238b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 123910a76f6fSfrank 12403a0a2d05SAndreas Gohr $pass = new PassHash(); 12413a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1242b0855b11Sandi 12433a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1244b0855b11Sandi msg("Unsupported crypt method $method", -1); 12453a0a2d05SAndreas Gohr return false; 1246b0855b11Sandi } 12473a0a2d05SAndreas Gohr 12483a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1249b0855b11Sandi} 1250b0855b11Sandi 1251b0855b11Sandi/** 1252b0855b11Sandi * Verifies a cleartext password against a crypted hash 1253b0855b11Sandi * 1254b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 125542ea7f44SGerrit Uitslag * 1256ab5d26daSAndreas Gohr * @param string $clear The clear text password 1257ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1258ab5d26daSAndreas Gohr * @return bool true if both match 1259b0855b11Sandi */ 1260*d868eb89SAndreas Gohrfunction auth_verifyPassword($clear, $crypt) 1261*d868eb89SAndreas Gohr{ 12623a0a2d05SAndreas Gohr $pass = new PassHash(); 12633a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1264b0855b11Sandi} 1265340756e4Sandi 1266a0b5b007SChris Smith/** 1267a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1268a0b5b007SChris Smith * 1269a0b5b007SChris Smith * @param string $user username 1270a0b5b007SChris Smith * @param string $pass encrypted password 1271a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1272ab5d26daSAndreas Gohr * @return bool 1273a0b5b007SChris Smith */ 1274*d868eb89SAndreas Gohrfunction auth_setCookie($user, $pass, $sticky) 1275*d868eb89SAndreas Gohr{ 1276a0b5b007SChris Smith global $conf; 1277e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1278a0b5b007SChris Smith global $auth; 127979d00841SOliver Geisen global $USERINFO; 1280a0b5b007SChris Smith 1281beca106aSAdrian Lang if(!$auth) return false; 1282a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1283a0b5b007SChris Smith 1284a0b5b007SChris Smith // set cookie 1285645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 128673ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1287c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1288bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, $cookie, [ 1289bf8392ebSAndreas Gohr 'expires' => $time, 1290bf8392ebSAndreas Gohr 'path' => $cookieDir, 1291bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 1292bf8392ebSAndreas Gohr 'httponly' => true, 1293486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 1294bf8392ebSAndreas Gohr ]); 129555a71a16SGerrit Uitslag 1296a0b5b007SChris Smith // set session 1297a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1298234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1299a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1300a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1301a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1302ab5d26daSAndreas Gohr 1303ab5d26daSAndreas Gohr return true; 1304a0b5b007SChris Smith} 1305a0b5b007SChris Smith 1306645c0a36SAndreas Gohr/** 1307645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1308645c0a36SAndreas Gohr * 1309645c0a36SAndreas Gohr * @returns array 1310645c0a36SAndreas Gohr */ 1311*d868eb89SAndreas Gohrfunction auth_getCookie() 1312*d868eb89SAndreas Gohr{ 1313c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 131424870174SAndreas Gohr return [null, null, null]; 1315c66972f2SAdrian Lang } 131624870174SAndreas Gohr [$user, $sticky, $pass] = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, ''); 1317645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1318645c0a36SAndreas Gohr $pass = base64_decode($pass); 1319645c0a36SAndreas Gohr $user = base64_decode($user); 132024870174SAndreas Gohr return [$user, $sticky, $pass]; 1321645c0a36SAndreas Gohr} 1322645c0a36SAndreas Gohr 1323e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1324