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 */ 31d868eb89SAndreas Gohrfunction auth_setup() 32d868eb89SAndreas 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 */ 122d868eb89SAndreas Gohrfunction auth_loadACL() 123d868eb89SAndreas 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 */ 172d868eb89SAndreas Gohrfunction auth_login_wrapper($evdata) 173d868eb89SAndreas 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 */ 211d868eb89SAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) 212d868eb89SAndreas 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 */ 285d868eb89SAndreas Gohrfunction auth_browseruid() 286d868eb89SAndreas 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 */ 316d868eb89SAndreas Gohrfunction auth_cookiesalt($addsession = false, $secure = false) 317d868eb89SAndreas 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 */ 345d868eb89SAndreas Gohrfunction auth_randombytes($length) 346d868eb89SAndreas 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 */ 359d868eb89SAndreas Gohrfunction auth_random($min, $max) 360d868eb89SAndreas 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 */ 374d868eb89SAndreas Gohrfunction auth_encrypt($data, $secret) 375d868eb89SAndreas 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 */ 398d868eb89SAndreas Gohrfunction auth_decrypt($ciphertext, $secret) 399d868eb89SAndreas 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 */ 418d868eb89SAndreas Gohrfunction auth_logoff($keepbc = false) 419d868eb89SAndreas 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 */ 470d868eb89SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache = false) 471d868eb89SAndreas 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 */ 531d868eb89SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache = false) 532d868eb89SAndreas 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 */ 547d868eb89SAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) 548d868eb89SAndreas 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 */ 594d868eb89SAndreas Gohrfunction auth_quickaclcheck($id) 595d868eb89SAndreas 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 */ 616d868eb89SAndreas Gohrfunction auth_aclcheck($id, $user, $groups) 617d868eb89SAndreas 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 */ 637d868eb89SAndreas Gohrfunction auth_aclcheck_cb($data) 638d868eb89SAndreas 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 */ 768d868eb89SAndreas Gohrfunction auth_nameencode($name, $skip_group = false) 769d868eb89SAndreas 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])/', 782*dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 783*dccd6b2bSAndreas Gohr substr($name, 1) 784ab5d26daSAndreas Gohr ); 785e838fc2eSAndreas Gohr } else { 78630f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 78730f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 788*dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 789*dccd6b2bSAndreas Gohr $name 790ab5d26daSAndreas Gohr ); 791e838fc2eSAndreas Gohr } 7926c2bb100SAndreas Gohr } 7936c2bb100SAndreas Gohr 794a424cd8eSchris return $cache[$name][$skip_group]; 795a424cd8eSchris} 796a424cd8eSchris 79704d68ae4SGerrit Uitslag/** 79804d68ae4SGerrit Uitslag * callback encodes the matches 79904d68ae4SGerrit Uitslag * 80004d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 80104d68ae4SGerrit Uitslag * @return string 80204d68ae4SGerrit Uitslag */ 803d868eb89SAndreas Gohrfunction auth_nameencode_callback($matches) 804d868eb89SAndreas Gohr{ 80530f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1], -1))); 80630f6faf0SChristopher Smith} 80730f6faf0SChristopher Smith 8086c2bb100SAndreas Gohr/** 809f3f0262cSandi * Create a pronouncable password 810f3f0262cSandi * 8118a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8128a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8138a285f7fSAndreas Gohr * 81415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 81515fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8168a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 81715fae107Sandi * 8188a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 81915fae107Sandi * @return string pronouncable password 820f3f0262cSandi */ 821d868eb89SAndreas Gohrfunction auth_pwgen($foruser = '') 822d868eb89SAndreas Gohr{ 82324870174SAndreas Gohr $data = [ 824d628dcf3SAndreas Gohr 'password' => '', 825d628dcf3SAndreas Gohr 'foruser' => $foruser 82624870174SAndreas Gohr ]; 8278a285f7fSAndreas Gohr 828e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 8298a285f7fSAndreas Gohr if($evt->advise_before(true)) { 830f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 831f3f0262cSandi $v = 'aeiou'; //vowels 832f3f0262cSandi $a = $c.$v; //both 833987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 834f3f0262cSandi 835987c8d26SAndreas Gohr //use thre syllables... 836987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 837483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 838483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 839483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 840f3f0262cSandi } 841987c8d26SAndreas Gohr //... and add a nice number and special 84243f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)].auth_random(10, 99); 8438a285f7fSAndreas Gohr } 8448a285f7fSAndreas Gohr $evt->advise_after(); 845f3f0262cSandi 8468a285f7fSAndreas Gohr return $data['password']; 847f3f0262cSandi} 848f3f0262cSandi 849f3f0262cSandi/** 850f3f0262cSandi * Sends a password to the given user 851f3f0262cSandi * 85215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 85342ea7f44SGerrit Uitslag * 854ab5d26daSAndreas Gohr * @param string $user Login name of the user 855ab5d26daSAndreas Gohr * @param string $password The new password in clear text 85615fae107Sandi * @return bool true on success 857f3f0262cSandi */ 858d868eb89SAndreas Gohrfunction auth_sendPassword($user, $password) 859d868eb89SAndreas Gohr{ 860f3f0262cSandi global $lang; 861e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 862cd52f92dSchris global $auth; 863beca106aSAdrian Lang if(!$auth) return false; 864cd52f92dSchris 865d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8662dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 867f3f0262cSandi 86887ddda95Sandi if(!$userinfo['mail']) return false; 869f3f0262cSandi 870f3f0262cSandi $text = rawLocale('password'); 87124870174SAndreas Gohr $trep = [ 872d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 873d7169d19SAndreas Gohr 'LOGIN' => $user, 874d7169d19SAndreas Gohr 'PASSWORD' => $password 87524870174SAndreas Gohr ]; 876f3f0262cSandi 877d7169d19SAndreas Gohr $mail = new Mailer(); 878102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']).' <'.$userinfo['mail'].'>'); 879d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 880d7169d19SAndreas Gohr $mail->setBody($text, $trep); 881d7169d19SAndreas Gohr return $mail->send(); 882f3f0262cSandi} 883f3f0262cSandi 884f3f0262cSandi/** 88515fae107Sandi * Register a new user 886f3f0262cSandi * 88715fae107Sandi * This registers a new user - Data is read directly from $_POST 88815fae107Sandi * 88915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 89042ea7f44SGerrit Uitslag * 89115fae107Sandi * @return bool true on success, false on any error 892f3f0262cSandi */ 893d868eb89SAndreas Gohrfunction register() 894d868eb89SAndreas Gohr{ 895f3f0262cSandi global $lang; 896eb5d07e4Sjan global $conf; 897e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 898cd52f92dSchris global $auth; 89964273335SAndreas Gohr global $INPUT; 900f3f0262cSandi 90164273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9023a48618aSAnika Henke if(!actionOK('register')) return false; 903640145a5Sandi 90464273335SAndreas Gohr // gather input 90564273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 90664273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 90764273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 90864273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 90964273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 910d752aedeSAndreas Gohr 91164273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 912f3f0262cSandi msg($lang['regmissing'], -1); 913f3f0262cSandi return false; 914f3f0262cSandi } 915f3f0262cSandi 916cab2716aSmatthias.grimm if($conf['autopasswd']) { 9178a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 91864273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 919bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 920cab2716aSmatthias.grimm return false; 92164273335SAndreas Gohr } elseif($pass != $passchk) { 922bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 923cab2716aSmatthias.grimm return false; 924cab2716aSmatthias.grimm } 925cab2716aSmatthias.grimm 926f3f0262cSandi //check mail 92764273335SAndreas Gohr if(!mail_isvalid($email)) { 928f3f0262cSandi msg($lang['regbadmail'], -1); 929f3f0262cSandi return false; 930f3f0262cSandi } 931f3f0262cSandi 932f3f0262cSandi //okay try to create the user 93324870174SAndreas Gohr if(!$auth->triggerUserMod('create', [$login, $pass, $fullname, $email])) { 934db9faf02SPatrick Brown msg($lang['regfail'], -1); 935f3f0262cSandi return false; 936f3f0262cSandi } 937f3f0262cSandi 938790b7720SAndreas Gohr // send notification about the new user 93975d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 94075d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 94102a498e7Schris 942790b7720SAndreas Gohr // are we done? 943cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 944cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 945cab2716aSmatthias.grimm return true; 946cab2716aSmatthias.grimm } 947cab2716aSmatthias.grimm 948790b7720SAndreas Gohr // autogenerated password? then send password to user 94964273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 950f3f0262cSandi msg($lang['regsuccess'], 1); 951f3f0262cSandi return true; 952f3f0262cSandi } else { 953f3f0262cSandi msg($lang['regmailfail'], -1); 954f3f0262cSandi return false; 955f3f0262cSandi } 956f3f0262cSandi} 957f3f0262cSandi 95810a76f6fSfrank/** 9598b06d178Schris * Update user profile 9608b06d178Schris * 9618b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9628b06d178Schris */ 963d868eb89SAndreas Gohrfunction updateprofile() 964d868eb89SAndreas Gohr{ 9658b06d178Schris global $conf; 9668b06d178Schris global $lang; 967e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 968cd52f92dSchris global $auth; 969bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 970bcc94b2cSAndreas Gohr global $INPUT; 9718b06d178Schris 972bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9731b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9748b06d178Schris 9753a48618aSAnika Henke if(!actionOK('profile')) { 9768b06d178Schris msg($lang['profna'], -1); 9778b06d178Schris return false; 9788b06d178Schris } 9798b06d178Schris 98024870174SAndreas Gohr $changes = []; 981bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 982bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 983bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 984bcc94b2cSAndreas Gohr 985bcc94b2cSAndreas Gohr // check misspelled passwords 986bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 987bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 9888b06d178Schris return false; 9898b06d178Schris } 9908b06d178Schris 9918b06d178Schris // clean fullname and email 992bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 993bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 9948b06d178Schris 995bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 996bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 997bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 998ab5d26daSAndreas Gohr ) { 9998b06d178Schris msg($lang['profnoempty'], -1); 10008b06d178Schris return false; 10018b06d178Schris } 1002bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10038b06d178Schris msg($lang['regbadmail'], -1); 10048b06d178Schris return false; 10058b06d178Schris } 10068b06d178Schris 1007bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10084c21b7eeSAndreas Gohr 1009bcc94b2cSAndreas Gohr // check for unavailable capabilities 1010bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 1011bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 1012bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 1013bcc94b2cSAndreas Gohr 1014bcc94b2cSAndreas Gohr // anything to do? 101524870174SAndreas Gohr if($changes === []) { 10168b06d178Schris msg($lang['profnochange'], -1); 10178b06d178Schris return false; 10188b06d178Schris } 10198b06d178Schris 10208b06d178Schris if($conf['profileconfirm']) { 1021585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 102271422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10238b06d178Schris return false; 10248b06d178Schris } 10258b06d178Schris } 10268b06d178Schris 102724870174SAndreas Gohr if(!$auth->triggerUserMod('modify', [$INPUT->server->str('REMOTE_USER'), &$changes])) { 1028db9faf02SPatrick Brown msg($lang['proffail'], -1); 1029db9faf02SPatrick Brown return false; 1030db9faf02SPatrick Brown } 1031db9faf02SPatrick Brown 103232ed2b36SAndreas Gohr if($changes['pass']) { 1033c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 103424870174SAndreas Gohr [, $sticky, ] = auth_getCookie(); 103504369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1036585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1037c276e9e8SMarcel Pennewiss } else { 1038c276e9e8SMarcel Pennewiss // make sure the session is writable 1039c276e9e8SMarcel Pennewiss @session_start(); 1040c276e9e8SMarcel Pennewiss // invalidate session cache 1041c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1042c276e9e8SMarcel Pennewiss session_write_close(); 104332ed2b36SAndreas Gohr } 1044c276e9e8SMarcel Pennewiss 104525b2a98cSMichael Klier return true; 1046a0b5b007SChris Smith} 1047ab5d26daSAndreas Gohr 104804d68ae4SGerrit Uitslag/** 104904d68ae4SGerrit Uitslag * Delete the current logged-in user 105004d68ae4SGerrit Uitslag * 105104d68ae4SGerrit Uitslag * @return bool true on success, false on any error 105204d68ae4SGerrit Uitslag */ 1053d868eb89SAndreas Gohrfunction auth_deleteprofile() 1054d868eb89SAndreas Gohr{ 10552a7abf2dSChristopher Smith global $conf; 10562a7abf2dSChristopher Smith global $lang; 1057e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 10582a7abf2dSChristopher Smith global $auth; 10592a7abf2dSChristopher Smith /* @var Input $INPUT */ 10602a7abf2dSChristopher Smith global $INPUT; 10612a7abf2dSChristopher Smith 10622a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 10632a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 10642a7abf2dSChristopher Smith 10652a7abf2dSChristopher Smith // action prevented or auth module disallows 10662a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10672a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10682a7abf2dSChristopher Smith return false; 10692a7abf2dSChristopher Smith } 10702a7abf2dSChristopher Smith 10712a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 10722a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10732a7abf2dSChristopher Smith return false; 10742a7abf2dSChristopher Smith } 10752a7abf2dSChristopher Smith 10762a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1077585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10782a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10792a7abf2dSChristopher Smith return false; 10802a7abf2dSChristopher Smith } 10812a7abf2dSChristopher Smith } 10822a7abf2dSChristopher Smith 108324870174SAndreas Gohr $deleted = []; 1084585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 108524870174SAndreas Gohr if($auth->triggerUserMod('delete', [$deleted])) { 10862a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 10872a7abf2dSChristopher Smith auth_logoff(); 10882a7abf2dSChristopher Smith return true; 10892a7abf2dSChristopher Smith } 10902a7abf2dSChristopher Smith 10912a7abf2dSChristopher Smith return false; 10922a7abf2dSChristopher Smith} 10932a7abf2dSChristopher Smith 10948b06d178Schris/** 10958b06d178Schris * Send a new password 10968b06d178Schris * 10971d5856cfSAndreas Gohr * This function handles both phases of the password reset: 10981d5856cfSAndreas Gohr * 10991d5856cfSAndreas Gohr * - handling the first request of password reset 11001d5856cfSAndreas Gohr * - validating the password reset auth token 11011d5856cfSAndreas Gohr * 11028b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11038b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11041d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11058b06d178Schris * 11068b06d178Schris * @return bool true on success, false on any error 11078b06d178Schris */ 1108d868eb89SAndreas Gohrfunction act_resendpwd() 1109d868eb89SAndreas Gohr{ 11108b06d178Schris global $lang; 11118b06d178Schris global $conf; 1112e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1113cd52f92dSchris global $auth; 1114bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1115bcc94b2cSAndreas Gohr global $INPUT; 11168b06d178Schris 11173a48618aSAnika Henke if(!actionOK('resendpwd')) { 11188b06d178Schris msg($lang['resendna'], -1); 11198b06d178Schris return false; 11208b06d178Schris } 11218b06d178Schris 1122bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11238b06d178Schris 11241d5856cfSAndreas Gohr if($token) { 1125cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11261d5856cfSAndreas Gohr 11272401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 112879e79377SAndreas Gohr if(!file_exists($tfile)) { 11291d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1130bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11311d5856cfSAndreas Gohr return false; 11321d5856cfSAndreas Gohr } 11338a9735e3SAndreas Gohr // token is only valid for 3 days 11348a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11358a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1136bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11371d5856cfSAndreas Gohr @unlink($tfile); 11388a9735e3SAndreas Gohr return false; 11398a9735e3SAndreas Gohr } 11408a9735e3SAndreas Gohr 11418b06d178Schris $user = io_readfile($tfile); 11422dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11438b06d178Schris if(!$userinfo['mail']) { 11448b06d178Schris msg($lang['resendpwdnouser'], -1); 11458b06d178Schris return false; 11468b06d178Schris } 11478b06d178Schris 1148cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1149bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1150bcc94b2cSAndreas Gohr 1151cc204bbdSAndreas Gohr // password given correctly? 1152bcc94b2cSAndreas Gohr if(!$pass) return false; 1153bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1154451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1155cc204bbdSAndreas Gohr return false; 1156cc204bbdSAndreas Gohr } 1157cc204bbdSAndreas Gohr 1158bcc94b2cSAndreas Gohr // change it 115924870174SAndreas Gohr if(!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1160db9faf02SPatrick Brown msg($lang['proffail'], -1); 1161cc204bbdSAndreas Gohr return false; 1162cc204bbdSAndreas Gohr } 1163cc204bbdSAndreas Gohr 1164cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1165cc204bbdSAndreas Gohr 11668a285f7fSAndreas Gohr $pass = auth_pwgen($user); 116724870174SAndreas Gohr if(!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1168db9faf02SPatrick Brown msg($lang['proffail'], -1); 11698b06d178Schris return false; 11708b06d178Schris } 11718b06d178Schris 11728b06d178Schris if(auth_sendPassword($user, $pass)) { 11738b06d178Schris msg($lang['resendpwdsuccess'], 1); 11748b06d178Schris } else { 11758b06d178Schris msg($lang['regmailfail'], -1); 11768b06d178Schris } 1177cc204bbdSAndreas Gohr } 1178cc204bbdSAndreas Gohr 1179cc204bbdSAndreas Gohr @unlink($tfile); 11808b06d178Schris return true; 11811d5856cfSAndreas Gohr 11821d5856cfSAndreas Gohr } else { 11831d5856cfSAndreas Gohr // we're in request phase 11841d5856cfSAndreas Gohr 1185bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 11861d5856cfSAndreas Gohr 1187bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 11881d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 11891d5856cfSAndreas Gohr return false; 11901d5856cfSAndreas Gohr } else { 1191bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 11921d5856cfSAndreas Gohr } 11931d5856cfSAndreas Gohr 11942dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11951d5856cfSAndreas Gohr if(!$userinfo['mail']) { 11961d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 11971d5856cfSAndreas Gohr return false; 11981d5856cfSAndreas Gohr } 11991d5856cfSAndreas Gohr 12001d5856cfSAndreas Gohr // generate auth token 1201483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12022401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 120324870174SAndreas Gohr $url = wl('', ['do'=> 'resendpwd', 'pwauth'=> $token], true, '&'); 12041d5856cfSAndreas Gohr 12051d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12061d5856cfSAndreas Gohr 12071d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 120824870174SAndreas Gohr $trep = ['FULLNAME' => $userinfo['name'], 'LOGIN' => $user, 'CONFIRM' => $url]; 12091d5856cfSAndreas Gohr 1210d7169d19SAndreas Gohr $mail = new Mailer(); 1211d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1212d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1213d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1214d7169d19SAndreas Gohr if($mail->send()) { 12151d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12161d5856cfSAndreas Gohr } else { 12171d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12181d5856cfSAndreas Gohr } 12191d5856cfSAndreas Gohr return true; 12201d5856cfSAndreas Gohr } 1221ab5d26daSAndreas Gohr // never reached 12228b06d178Schris} 12238b06d178Schris 12248b06d178Schris/** 1225b0855b11Sandi * Encrypts a password using the given method and salt 1226b0855b11Sandi * 1227b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1228b0855b11Sandi * is chosen. 1229b0855b11Sandi * 1230b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 123142ea7f44SGerrit Uitslag * 1232ab5d26daSAndreas Gohr * @param string $clear The clear text password 1233ab5d26daSAndreas Gohr * @param string $method The hashing method 1234ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1235b0855b11Sandi * @return string The crypted password 1236b0855b11Sandi */ 1237d868eb89SAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) 1238d868eb89SAndreas Gohr{ 1239b0855b11Sandi global $conf; 1240b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 124110a76f6fSfrank 12423a0a2d05SAndreas Gohr $pass = new PassHash(); 12433a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1244b0855b11Sandi 12453a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1246b0855b11Sandi msg("Unsupported crypt method $method", -1); 12473a0a2d05SAndreas Gohr return false; 1248b0855b11Sandi } 12493a0a2d05SAndreas Gohr 12503a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1251b0855b11Sandi} 1252b0855b11Sandi 1253b0855b11Sandi/** 1254b0855b11Sandi * Verifies a cleartext password against a crypted hash 1255b0855b11Sandi * 1256b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 125742ea7f44SGerrit Uitslag * 1258ab5d26daSAndreas Gohr * @param string $clear The clear text password 1259ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1260ab5d26daSAndreas Gohr * @return bool true if both match 1261b0855b11Sandi */ 1262d868eb89SAndreas Gohrfunction auth_verifyPassword($clear, $crypt) 1263d868eb89SAndreas Gohr{ 12643a0a2d05SAndreas Gohr $pass = new PassHash(); 12653a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1266b0855b11Sandi} 1267340756e4Sandi 1268a0b5b007SChris Smith/** 1269a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1270a0b5b007SChris Smith * 1271a0b5b007SChris Smith * @param string $user username 1272a0b5b007SChris Smith * @param string $pass encrypted password 1273a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1274ab5d26daSAndreas Gohr * @return bool 1275a0b5b007SChris Smith */ 1276d868eb89SAndreas Gohrfunction auth_setCookie($user, $pass, $sticky) 1277d868eb89SAndreas Gohr{ 1278a0b5b007SChris Smith global $conf; 1279e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1280a0b5b007SChris Smith global $auth; 128179d00841SOliver Geisen global $USERINFO; 1282a0b5b007SChris Smith 1283beca106aSAdrian Lang if(!$auth) return false; 1284a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1285a0b5b007SChris Smith 1286a0b5b007SChris Smith // set cookie 1287645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 128873ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1289c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1290bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, $cookie, [ 1291bf8392ebSAndreas Gohr 'expires' => $time, 1292bf8392ebSAndreas Gohr 'path' => $cookieDir, 1293bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 1294bf8392ebSAndreas Gohr 'httponly' => true, 1295486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 1296bf8392ebSAndreas Gohr ]); 129755a71a16SGerrit Uitslag 1298a0b5b007SChris Smith // set session 1299a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1300234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1301a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1302a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1303a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1304ab5d26daSAndreas Gohr 1305ab5d26daSAndreas Gohr return true; 1306a0b5b007SChris Smith} 1307a0b5b007SChris Smith 1308645c0a36SAndreas Gohr/** 1309645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1310645c0a36SAndreas Gohr * 1311645c0a36SAndreas Gohr * @returns array 1312645c0a36SAndreas Gohr */ 1313d868eb89SAndreas Gohrfunction auth_getCookie() 1314d868eb89SAndreas Gohr{ 1315c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 131624870174SAndreas Gohr return [null, null, null]; 1317c66972f2SAdrian Lang } 131824870174SAndreas Gohr [$user, $sticky, $pass] = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, ''); 1319645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1320645c0a36SAndreas Gohr $pass = base64_decode($pass); 1321645c0a36SAndreas Gohr $user = base64_decode($user); 132224870174SAndreas Gohr return [$user, $sticky, $pass]; 1323645c0a36SAndreas Gohr} 1324645c0a36SAndreas Gohr 1325e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1326