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 */ 1115fae107Sandi 12ebf97c8fSAndreas Gohr// some ACL level defines 13*96348f27SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 14*96348f27SAndreas Gohruse dokuwiki\Extension\Event; 15*96348f27SAndreas Gohruse dokuwiki\Extension\PluginController; 16c3cc6e05SAndreas Gohruse dokuwiki\PassHash; 1775d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 18c3cc6e05SAndreas Gohr 19ebf97c8fSAndreas Gohrdefine('AUTH_NONE', 0); 20ebf97c8fSAndreas Gohrdefine('AUTH_READ', 1); 21ebf97c8fSAndreas Gohrdefine('AUTH_EDIT', 2); 22ebf97c8fSAndreas Gohrdefine('AUTH_CREATE', 4); 23ebf97c8fSAndreas Gohrdefine('AUTH_UPLOAD', 8); 24ebf97c8fSAndreas Gohrdefine('AUTH_DELETE', 16); 25ebf97c8fSAndreas Gohrdefine('AUTH_ADMIN', 255); 26ebf97c8fSAndreas Gohr 2716905344SAndreas Gohr/** 2816905344SAndreas Gohr * Initialize the auth system. 2916905344SAndreas Gohr * 3016905344SAndreas Gohr * This function is automatically called at the end of init.php 3116905344SAndreas Gohr * 3216905344SAndreas Gohr * This used to be the main() of the auth.php 3316905344SAndreas Gohr * 3416905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 3516905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 36ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 37ab5d26daSAndreas Gohr * @return bool 3816905344SAndreas Gohr */ 3916905344SAndreas Gohrfunction auth_setup() { 40742c66f8Schris global $conf; 41e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4203c4aec3Schris global $auth; 43bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 44bcc94b2cSAndreas Gohr global $INPUT; 459a9714acSDominik Eckelmann global $AUTH_ACL; 469a9714acSDominik Eckelmann global $lang; 473a7140a1SAndreas Gohr /* @var PluginController $plugin_controller */ 489c29eea5SJan Schumann global $plugin_controller; 499a9714acSDominik Eckelmann $AUTH_ACL = array(); 5003c4aec3Schris 5116905344SAndreas Gohr if(!$conf['useacl']) return false; 5216905344SAndreas Gohr 539c29eea5SJan Schumann // try to load auth backend from plugins 549c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 559c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 56f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 579c29eea5SJan Schumann break; 589c29eea5SJan Schumann } 599c29eea5SJan Schumann } 608b06d178Schris 616416b708SMichael Hamann if(!isset($auth) || !$auth){ 623094e817SAndreas Gohr msg($lang['authtempfail'], -1); 633094e817SAndreas Gohr return false; 643094e817SAndreas Gohr } 658b06d178Schris 666416b708SMichael Hamann if ($auth->success == false) { 670f4f4adfSAndreas Gohr // degrade to unauthenticated user 68d2dde4ebSMatthias Grimm unset($auth); 690f4f4adfSAndreas Gohr auth_logoff(); 70cd52f92dSchris msg($lang['authtempfail'], -1); 716416b708SMichael Hamann return false; 72d2dde4ebSMatthias Grimm } 7316905344SAndreas Gohr 7416905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 75bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 76bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 77bbbd6568SAndreas Gohr 78b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 79b2665af7SMichael Hamann // the one presented at 80b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 81b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 82b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 83b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 84528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 8506156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 86528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 87528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 88528ddc7cSAndreas Gohr } 89528ddc7cSAndreas Gohr 901e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 91bcc94b2cSAndreas Gohr if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 92bcc94b2cSAndreas Gohr $INPUT->set('u', $_SERVER['PHP_AUTH_USER']); 93bcc94b2cSAndreas Gohr $INPUT->set('p', $_SERVER['PHP_AUTH_PW']); 94bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 951e8c9c90SAndreas Gohr } 961e8c9c90SAndreas Gohr 97395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 9893a7873eSAndreas Gohr if (true === $auth->success) { 99395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 100395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 101f4476bd9SJan Schumann } 102191bb90aSAndreas Gohr 10381e99965SPhy $ok = null; 1048eca974cSAndreas Gohr if (!is_null($auth) && $auth->canDo('external')) { 10581e99965SPhy $ok = $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 10681e99965SPhy } 10781e99965SPhy 10881e99965SPhy if ($ok === null) { 10981e99965SPhy // external trust mechanism not in place, or returns no result, 11081e99965SPhy // then attempt auth_login 1116080c584SRobin Gareus $evdata = array( 112bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 113bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 114bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 115bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1166080c584SRobin Gareus ); 117cbb44eabSAndreas Gohr Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 118f5cb575dSAndreas Gohr } 119f5cb575dSAndreas Gohr 12016905344SAndreas Gohr //load ACL into a global array XXX 12175c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 122ab5d26daSAndreas Gohr 123ab5d26daSAndreas Gohr return true; 12475c93b77SAndreas Gohr} 12575c93b77SAndreas Gohr 12675c93b77SAndreas Gohr/** 12775c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12875c93b77SAndreas Gohr * 12975c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 13042ea7f44SGerrit Uitslag * 131ab5d26daSAndreas Gohr * @return array 13275c93b77SAndreas Gohr */ 13375c93b77SAndreas Gohrfunction auth_loadACL() { 13475c93b77SAndreas Gohr global $config_cascade; 135b78bf706Sromain global $USERINFO; 136585bf44eSChristopher Smith /* @var Input $INPUT */ 137585bf44eSChristopher Smith global $INPUT; 13875c93b77SAndreas Gohr 13975c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 14075c93b77SAndreas Gohr 14175c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 14275c93b77SAndreas Gohr 14332e82180SAndreas Gohr $out = array(); 1449ce556d2SAndreas Gohr foreach($acl as $line) { 1459ce556d2SAndreas Gohr $line = trim($line); 1462401f18dSSyntaxseed if(empty($line) || ($line[0] == '#')) continue; // skip blank lines & comments 14721c3090aSChristopher Smith list($id,$rest) = preg_split('/[ \t]+/',$line,2); 14832e82180SAndreas Gohr 149443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 150ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 151ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 152585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 153ad3d68d7SChristopher Smith 154585bf44eSChristopher Smith $id = str_replace('%USER%',cleanID($INPUT->server->str('REMOTE_USER')),$id); 155585bf44eSChristopher Smith $rest = str_replace('%USER%',auth_nameencode($INPUT->server->str('REMOTE_USER')),$rest); 156ad3d68d7SChristopher Smith } 157ad3d68d7SChristopher Smith 158ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1599ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 160ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 16106f34f54SPhy if(isset($USERINFO['grps'])){ 1629ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 163b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 16432e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 16532e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 166b78bf706Sromain } 16706f34f54SPhy } 16832e82180SAndreas Gohr } else { 16932e82180SAndreas Gohr $out[] = "$id\t$rest"; 170a8fe108bSGuy Brand } 17111799630Sandi } 1729ce556d2SAndreas Gohr 17332e82180SAndreas Gohr return $out; 174f3f0262cSandi} 175f3f0262cSandi 176ab5d26daSAndreas Gohr/** 177ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 178ab5d26daSAndreas Gohr * 17942ea7f44SGerrit Uitslag * @param array $evdata 180ab5d26daSAndreas Gohr * @return bool 181ab5d26daSAndreas Gohr */ 182b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 183ab5d26daSAndreas Gohr return auth_login( 184ab5d26daSAndreas Gohr $evdata['user'], 185b5ee21aaSAdrian Lang $evdata['password'], 186b5ee21aaSAdrian Lang $evdata['sticky'], 187ab5d26daSAndreas Gohr $evdata['silent'] 188ab5d26daSAndreas Gohr ); 189b5ee21aaSAdrian Lang} 190b5ee21aaSAdrian Lang 191f3f0262cSandi/** 192f3f0262cSandi * This tries to login the user based on the sent auth credentials 193f3f0262cSandi * 194f3f0262cSandi * The authentication works like this: if a username was given 19515fae107Sandi * a new login is assumed and user/password are checked. If they 19615fae107Sandi * are correct the password is encrypted with blowfish and stored 19715fae107Sandi * together with the username in a cookie - the same info is stored 19815fae107Sandi * in the session, too. Additonally a browserID is stored in the 19915fae107Sandi * session. 20015fae107Sandi * 20115fae107Sandi * If no username was given the cookie is checked: if the username, 20215fae107Sandi * crypted password and browserID match between session and cookie 20315fae107Sandi * no further testing is done and the user is accepted 20415fae107Sandi * 20515fae107Sandi * If a cookie was found but no session info was availabe the 206136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 20715fae107Sandi * together with username rechecked by calling this function again. 208f3f0262cSandi * 209f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 210f3f0262cSandi * are set. 21115fae107Sandi * 21215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 21315fae107Sandi * 21415fae107Sandi * @param string $user Username 21515fae107Sandi * @param string $pass Cleartext Password 21615fae107Sandi * @param bool $sticky Cookie should not expire 217f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 21815fae107Sandi * @return bool true on successful auth 219f3f0262cSandi */ 220f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 221f3f0262cSandi global $USERINFO; 222f3f0262cSandi global $conf; 223f3f0262cSandi global $lang; 224e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 225cd52f92dSchris global $auth; 226585bf44eSChristopher Smith /* @var Input $INPUT */ 227585bf44eSChristopher Smith global $INPUT; 228ab5d26daSAndreas Gohr 229132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 230f3f0262cSandi 231beca106aSAdrian Lang if(!$auth) return false; 232beca106aSAdrian Lang 233bbbd6568SAndreas Gohr if(!empty($user)) { 234132bdbfeSandi //usual login 2355e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 236132bdbfeSandi // make logininfo globally available 237585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 23830d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 23904369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 240132bdbfeSandi return true; 241f3f0262cSandi } else { 242f3f0262cSandi //invalid credentials - log off 243f8b1e4e7SAndreas Gohr if(!$silent) { 244f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 245f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 246f8b1e4e7SAndreas Gohr } 247f3f0262cSandi auth_logoff(); 248132bdbfeSandi return false; 249f3f0262cSandi } 250f3f0262cSandi } else { 251132bdbfeSandi // read cookie information 252645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 253132bdbfeSandi if($user && $pass) { 254132bdbfeSandi // we got a cookie - see if we can trust it 255fa7c70ffSAdrian Lang 256fa7c70ffSAdrian Lang // get session info 257fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 258132bdbfeSandi if(isset($session) && 2597172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2604c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 261132bdbfeSandi ($session['user'] == $user) && 262234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 263ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 264ab5d26daSAndreas Gohr ) { 265234ce57eSAndreas Gohr 266132bdbfeSandi // he has session, cookie and browser right - let him in 267585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 268132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 269132bdbfeSandi return true; 270132bdbfeSandi } 271f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 27230d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 27304369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 274f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 275132bdbfeSandi } 276132bdbfeSandi } 277f3f0262cSandi //just to be sure 278883179a4SAndreas Gohr auth_logoff(true); 279132bdbfeSandi return false; 280f3f0262cSandi} 281132bdbfeSandi 282132bdbfeSandi/** 283136ce040Sandi * Builds a pseudo UID from browser and IP data 284132bdbfeSandi * 285132bdbfeSandi * This is neither unique nor unfakable - still it adds some 286136ce040Sandi * security. Using the first part of the IP makes sure 28780b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 28815fae107Sandi * 28915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 29015fae107Sandi * 29115fae107Sandi * @return string a MD5 sum of various browser headers 292132bdbfeSandi */ 293132bdbfeSandifunction auth_browseruid() { 294585bf44eSChristopher Smith /* @var Input $INPUT */ 295585bf44eSChristopher Smith global $INPUT; 296585bf44eSChristopher Smith 2972f9daf16SAndreas Gohr $ip = clientIP(true); 298132bdbfeSandi $uid = ''; 299585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_USER_AGENT'); 300585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_CHARSET'); 3012f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 30280b4f376SAndreas Gohr $uid = strtolower($uid); 303132bdbfeSandi return md5($uid); 304132bdbfeSandi} 305132bdbfeSandi 306132bdbfeSandi/** 307132bdbfeSandi * Creates a random key to encrypt the password in cookies 30815fae107Sandi * 30915fae107Sandi * This function tries to read the password for encrypting 31098407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 31115fae107Sandi * if no such file is found a random key is created and 31215fae107Sandi * and stored in this file. 31315fae107Sandi * 31415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 31542ea7f44SGerrit Uitslag * 31632ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 31730d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 31815fae107Sandi * @return string 319132bdbfeSandi */ 32030d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 321a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 322fe745becSMichael Große return 'test'; 323a1fe3c9cSMichael Große } 324132bdbfeSandi global $conf; 32598407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 32630d544a4SMichael Hamann if ($secure || !file_exists($file)) { 32730d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 32830d544a4SMichael Hamann } 329132bdbfeSandi $salt = io_readFile($file); 330132bdbfeSandi if(empty($salt)) { 33130d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 332132bdbfeSandi io_saveFile($file, $salt); 333132bdbfeSandi } 33432ed2b36SAndreas Gohr if($addsession) { 33532ed2b36SAndreas Gohr $salt .= session_id(); 33632ed2b36SAndreas Gohr } 337132bdbfeSandi return $salt; 338f3f0262cSandi} 339f3f0262cSandi 340f3f0262cSandi/** 3417a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 342483b6238SMichael Hamann * 3437a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 34442ea7f44SGerrit Uitslag * 3457a33d2f8SNiklas Keller * @param int $length number of bytes 3467a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 347483b6238SMichael Hamann */ 348483b6238SMichael Hamannfunction auth_randombytes($length) { 3497a33d2f8SNiklas Keller return random_bytes($length); 350483b6238SMichael Hamann} 351483b6238SMichael Hamann 352483b6238SMichael Hamann/** 3537a33d2f8SNiklas Keller * Cryptographically secure random number generator. 354483b6238SMichael Hamann * 3557a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 35642ea7f44SGerrit Uitslag * 357483b6238SMichael Hamann * @param int $min 358483b6238SMichael Hamann * @param int $max 359483b6238SMichael Hamann * @return int 360483b6238SMichael Hamann */ 361483b6238SMichael Hamannfunction auth_random($min, $max) { 3627a33d2f8SNiklas Keller return random_int($min, $max); 363483b6238SMichael Hamann} 364483b6238SMichael Hamann 365483b6238SMichael Hamann/** 36604369c3eSMichael Hamann * Encrypt data using the given secret using AES 36704369c3eSMichael Hamann * 36804369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 36904369c3eSMichael Hamann * using pbkdf2. 37004369c3eSMichael Hamann * 37104369c3eSMichael Hamann * @param string $data The data that shall be encrypted 37204369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 37304369c3eSMichael Hamann * @return string The ciphertext 37404369c3eSMichael Hamann */ 37504369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 37604369c3eSMichael Hamann $iv = auth_randombytes(16); 3771af2f135SAndreas Gohr $cipher = new \phpseclib\Crypt\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 */ 39804369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 3997b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 4001af2f135SAndreas Gohr $cipher = new \phpseclib\Crypt\AES(); 40104369c3eSMichael Hamann $cipher->setPassword($secret); 4027b650cefSMichael Hamann $cipher->setIV($iv); 40304369c3eSMichael Hamann 4047b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 40504369c3eSMichael Hamann} 40604369c3eSMichael Hamann 40704369c3eSMichael Hamann/** 408883179a4SAndreas Gohr * Log out the current user 409883179a4SAndreas Gohr * 410f3f0262cSandi * This clears all authentication data and thus log the user 411883179a4SAndreas Gohr * off. It also clears session data. 41215fae107Sandi * 41315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 41442ea7f44SGerrit Uitslag * 415883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 416f3f0262cSandi */ 417883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 418f3f0262cSandi global $conf; 419f3f0262cSandi global $USERINFO; 420e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4215298a619SAndreas Gohr global $auth; 422585bf44eSChristopher Smith /* @var Input $INPUT */ 423585bf44eSChristopher Smith global $INPUT; 42437065e65Sandi 425d4869846SAndreas Gohr // make sure the session is writable (it usually is) 426e9621d07SAndreas Gohr @session_start(); 427e9621d07SAndreas Gohr 428e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 429e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 430e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 431e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 432e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 433e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 434883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 435e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 436585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 437132bdbfeSandi $USERINFO = null; //FIXME 438f5c6743cSAndreas Gohr 43973ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 44073ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 4415298a619SAndreas Gohr 442880f62faSAndreas Gohr if($auth) $auth->logOff(); 443f3f0262cSandi} 444f3f0262cSandi 445f3f0262cSandi/** 446f8cc712eSAndreas Gohr * Check if a user is a manager 447f8cc712eSAndreas Gohr * 448f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 449f8cc712eSAndreas Gohr * user. 450f8cc712eSAndreas Gohr * 451f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 452f8cc712eSAndreas Gohr * 453ab5d26daSAndreas Gohr * @param string $user Username 454ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 455ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 456*96348f27SAndreas Gohr * @param bool $recache set to true to skip cached results 457ab5d26daSAndreas Gohr * @return bool 458*96348f27SAndreas Gohr * @see auth_isadmin 459*96348f27SAndreas Gohr * 460*96348f27SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 461f8cc712eSAndreas Gohr */ 462*96348f27SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache=false) { 463f8cc712eSAndreas Gohr global $conf; 464f8cc712eSAndreas Gohr global $USERINFO; 465e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 466d752aedeSAndreas Gohr global $auth; 467585bf44eSChristopher Smith /* @var Input $INPUT */ 468585bf44eSChristopher Smith global $INPUT; 469585bf44eSChristopher Smith 470f8cc712eSAndreas Gohr 471beca106aSAdrian Lang if(!$auth) return false; 472c66972f2SAdrian Lang if(is_null($user)) { 473585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 474c66972f2SAdrian Lang return false; 475c66972f2SAdrian Lang } else { 476585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 477c66972f2SAdrian Lang } 478c66972f2SAdrian Lang } 479d6dc956fSAndreas Gohr if(is_null($groups)) { 4803e9ae63dSPhy $groups = $USERINFO ? (array) $USERINFO['grps'] : array(); 481e259aa79SAndreas Gohr } 482e259aa79SAndreas Gohr 483*96348f27SAndreas Gohr // prefer cached result 484*96348f27SAndreas Gohr static $cache = []; 485*96348f27SAndreas Gohr $cachekey = 'c-' . $user . '-' . $adminonly . '-' . join(':', $groups); 486*96348f27SAndreas Gohr if (!isset($cache[$cachekey]) || $recache) { 487d6dc956fSAndreas Gohr // check superuser match 488*96348f27SAndreas Gohr $ok = auth_isMember($conf['superuser'], $user, $groups); 48900ce12daSChris Smith 490*96348f27SAndreas Gohr // check managers 491*96348f27SAndreas Gohr if (!$ok && !$adminonly) { 492*96348f27SAndreas Gohr $ok = auth_isMember($conf['manager'], $user, $groups); 493*96348f27SAndreas Gohr } 494*96348f27SAndreas Gohr 495*96348f27SAndreas Gohr $cache[$cachekey] = $ok; 496*96348f27SAndreas Gohr } 497*96348f27SAndreas Gohr 498*96348f27SAndreas Gohr return $cache[$cachekey]; 499f8cc712eSAndreas Gohr} 500f8cc712eSAndreas Gohr 501f8cc712eSAndreas Gohr/** 502f8cc712eSAndreas Gohr * Check if a user is admin 503f8cc712eSAndreas Gohr * 504f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 505f8cc712eSAndreas Gohr * 506f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 507f8cc712eSAndreas Gohr * 508*96348f27SAndreas Gohr * @param string $user Username 509*96348f27SAndreas Gohr * @param array $groups List of groups the user is in 510*96348f27SAndreas Gohr * @param bool $recache set to true to skip cached results 511*96348f27SAndreas Gohr * @return bool 512f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 513ab5d26daSAndreas Gohr * @see auth_ismanager() 51442ea7f44SGerrit Uitslag * 515f8cc712eSAndreas Gohr */ 516*96348f27SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache=false) { 517*96348f27SAndreas Gohr return auth_ismanager($user, $groups, true, $recache); 518f8cc712eSAndreas Gohr} 519f8cc712eSAndreas Gohr 520d6dc956fSAndreas Gohr/** 521d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 522d6dc956fSAndreas Gohr * users and groups to determine membership status 523d6dc956fSAndreas Gohr * 524d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 525d6dc956fSAndreas Gohr * 52642ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 52742ea7f44SGerrit Uitslag * @param string $user user to match against 52842ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5295446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 530d6dc956fSAndreas Gohr */ 531d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 532e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 533d6dc956fSAndreas Gohr global $auth; 534d6dc956fSAndreas Gohr if(!$auth) return false; 535d6dc956fSAndreas Gohr 536d6dc956fSAndreas Gohr // clean user and groups 5374f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 5388cbc5ee8SAndreas Gohr $user = \dokuwiki\Utf8\PhpString::strtolower($user); 539d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 540d6dc956fSAndreas Gohr } 541d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 542d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 543d6dc956fSAndreas Gohr 544d6dc956fSAndreas Gohr // extract the memberlist 545d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 546d6dc956fSAndreas Gohr $members = array_map('trim', $members); 547d6dc956fSAndreas Gohr $members = array_unique($members); 548d6dc956fSAndreas Gohr $members = array_filter($members); 549d6dc956fSAndreas Gohr 550d6dc956fSAndreas Gohr // compare cleaned values 551d6dc956fSAndreas Gohr foreach($members as $member) { 552e5204a12SJurgen Hart if($member == '@ALL' ) return true; 5538cbc5ee8SAndreas Gohr if(!$auth->isCaseSensitive()) $member = \dokuwiki\Utf8\PhpString::strtolower($member); 554d6dc956fSAndreas Gohr if($member[0] == '@') { 555d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 556d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 557d6dc956fSAndreas Gohr } else { 558d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 559d6dc956fSAndreas Gohr if($member == $user) return true; 560d6dc956fSAndreas Gohr } 561d6dc956fSAndreas Gohr } 562d6dc956fSAndreas Gohr 563d6dc956fSAndreas Gohr // still here? not a member! 564d6dc956fSAndreas Gohr return false; 565d6dc956fSAndreas Gohr} 566d6dc956fSAndreas Gohr 567f8cc712eSAndreas Gohr/** 56815fae107Sandi * Convinience function for auth_aclcheck() 56915fae107Sandi * 57015fae107Sandi * This checks the permissions for the current user 57115fae107Sandi * 57215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 57315fae107Sandi * 5741698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 57515fae107Sandi * @return int permission level 576f3f0262cSandi */ 577f3f0262cSandifunction auth_quickaclcheck($id) { 578f3f0262cSandi global $conf; 579f3f0262cSandi global $USERINFO; 580585bf44eSChristopher Smith /* @var Input $INPUT */ 581585bf44eSChristopher Smith global $INPUT; 582f3f0262cSandi # if no ACL is used always return upload rights 583f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 5843e9ae63dSPhy return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), is_array($USERINFO) ? $USERINFO['grps'] : array()); 585f3f0262cSandi} 586f3f0262cSandi 587f3f0262cSandi/** 588c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 58915fae107Sandi * 59015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 59142ea7f44SGerrit Uitslag * 592c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 5931698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 59415fae107Sandi * @param string $user Username 5953272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 59615fae107Sandi * @return int permission level 597f3f0262cSandi */ 598f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 599c17acc9fSAndreas Gohr $data = array( 600c17acc9fSAndreas Gohr 'id' => $id, 601c17acc9fSAndreas Gohr 'user' => $user, 602c17acc9fSAndreas Gohr 'groups' => $groups 603c17acc9fSAndreas Gohr ); 604c17acc9fSAndreas Gohr 605cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 606c17acc9fSAndreas Gohr} 607c17acc9fSAndreas Gohr 608c17acc9fSAndreas Gohr/** 609c17acc9fSAndreas Gohr * default ACL check method 610c17acc9fSAndreas Gohr * 611c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 612c17acc9fSAndreas Gohr * 613c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 61442ea7f44SGerrit Uitslag * 615c17acc9fSAndreas Gohr * @param array $data event data 616c17acc9fSAndreas Gohr * @return int permission level 617c17acc9fSAndreas Gohr */ 618c17acc9fSAndreas Gohrfunction auth_aclcheck_cb($data) { 619c17acc9fSAndreas Gohr $id =& $data['id']; 620c17acc9fSAndreas Gohr $user =& $data['user']; 621c17acc9fSAndreas Gohr $groups =& $data['groups']; 622c17acc9fSAndreas Gohr 623f3f0262cSandi global $conf; 624f3f0262cSandi global $AUTH_ACL; 625e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 626d752aedeSAndreas Gohr global $auth; 627f3f0262cSandi 62885d03f68SAndreas Gohr // if no ACL is used always return upload rights 629f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 630beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 631f3f0262cSandi 632074cf26bSandi //make sure groups is an array 633074cf26bSandi if(!is_array($groups)) $groups = array(); 634074cf26bSandi 63585d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 636ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 637ab5d26daSAndreas Gohr return AUTH_ADMIN; 638ab5d26daSAndreas Gohr } 63985d03f68SAndreas Gohr 640eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 6418cbc5ee8SAndreas Gohr $user = \dokuwiki\Utf8\PhpString::strtolower($user); 642eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 643eb3ce0d5SKazutaka Miyasaka } 64437ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 645d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 64685d03f68SAndreas Gohr 6476c2bb100SAndreas Gohr //prepend groups with @ and nameencode 64837ff2261SSascha Klopp foreach($groups as &$group) { 64937ff2261SSascha Klopp $group = '@'.auth_nameencode($group); 65010a76f6fSfrank } 65110a76f6fSfrank 652f3f0262cSandi $ns = getNS($id); 653f3f0262cSandi $perm = -1; 654f3f0262cSandi 655f3f0262cSandi //add ALL group 656f3f0262cSandi $groups[] = '@ALL'; 65737ff2261SSascha Klopp 658f3f0262cSandi //add User 65934aeb4afSAndreas Gohr if($user) $groups[] = $user; 660f3f0262cSandi 661f3f0262cSandi //check exact match first 66221c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 663f3f0262cSandi if(count($matches)) { 664f3f0262cSandi foreach($matches as $match) { 665f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 66621c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 667eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 6688cbc5ee8SAndreas Gohr $acl[1] = \dokuwiki\Utf8\PhpString::strtolower($acl[1]); 669eb3ce0d5SKazutaka Miyasaka } 67048d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 67148d7b7a6SDominik Eckelmann continue; 67248d7b7a6SDominik Eckelmann } 6738ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 674f3f0262cSandi if($acl[2] > $perm) { 675f3f0262cSandi $perm = $acl[2]; 676f3f0262cSandi } 677f3f0262cSandi } 678f3f0262cSandi if($perm > -1) { 679f3f0262cSandi //we had a match - return it 680def492a2SGuillaume Turri return (int) $perm; 681f3f0262cSandi } 682f3f0262cSandi } 683f3f0262cSandi 684f3f0262cSandi //still here? do the namespace checks 685f3f0262cSandi if($ns) { 6863e304b55SMichael Hamann $path = $ns.':*'; 687f3f0262cSandi } else { 6883e304b55SMichael Hamann $path = '*'; //root document 689f3f0262cSandi } 690f3f0262cSandi 691f3f0262cSandi do { 69221c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 693f3f0262cSandi if(count($matches)) { 694f3f0262cSandi foreach($matches as $match) { 695f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 69621c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 697eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 6988cbc5ee8SAndreas Gohr $acl[1] = \dokuwiki\Utf8\PhpString::strtolower($acl[1]); 699eb3ce0d5SKazutaka Miyasaka } 70048d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 70148d7b7a6SDominik Eckelmann continue; 70248d7b7a6SDominik Eckelmann } 7038ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 704f3f0262cSandi if($acl[2] > $perm) { 705f3f0262cSandi $perm = $acl[2]; 706f3f0262cSandi } 707f3f0262cSandi } 708f3f0262cSandi //we had a match - return it 70948d7b7a6SDominik Eckelmann if($perm != -1) { 710def492a2SGuillaume Turri return (int) $perm; 711f3f0262cSandi } 71248d7b7a6SDominik Eckelmann } 713f3f0262cSandi //get next higher namespace 714f3f0262cSandi $ns = getNS($ns); 715f3f0262cSandi 7163e304b55SMichael Hamann if($path != '*') { 7173e304b55SMichael Hamann $path = $ns.':*'; 7183e304b55SMichael Hamann if($path == ':*') $path = '*'; 719f3f0262cSandi } else { 720f3f0262cSandi //we did this already 721f3f0262cSandi //looks like there is something wrong with the ACL 722f3f0262cSandi //break here 723d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 724d5ce66f6SAndreas Gohr return AUTH_NONE; 725f3f0262cSandi } 726f3f0262cSandi } while(1); //this should never loop endless 727ab5d26daSAndreas Gohr return AUTH_NONE; 728f3f0262cSandi} 729f3f0262cSandi 730f3f0262cSandi/** 7316c2bb100SAndreas Gohr * Encode ASCII special chars 7326c2bb100SAndreas Gohr * 7336c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7346c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7356c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7366c2bb100SAndreas Gohr * urlencoding!). 7376c2bb100SAndreas Gohr * 7386c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7396c2bb100SAndreas Gohr * 7406c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7416c2bb100SAndreas Gohr * @see rawurldecode() 74242ea7f44SGerrit Uitslag * 74342ea7f44SGerrit Uitslag * @param string $name 74442ea7f44SGerrit Uitslag * @param bool $skip_group 74542ea7f44SGerrit Uitslag * @return string 7466c2bb100SAndreas Gohr */ 747e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 748a424cd8eSchris global $cache_authname; 749a424cd8eSchris $cache =& $cache_authname; 75031784267SAndreas Gohr $name = (string) $name; 751a424cd8eSchris 75280601d26SAndreas Gohr // never encode wildcard FS#1955 75380601d26SAndreas Gohr if($name == '%USER%') return $name; 754b78bf706Sromain if($name == '%GROUP%') return $name; 75580601d26SAndreas Gohr 756a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 7572401f18dSSyntaxseed if($skip_group && $name[0] == '@') { 75830f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 75930f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 76030f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 761ab5d26daSAndreas Gohr ); 762e838fc2eSAndreas Gohr } else { 76330f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 76430f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 76530f6faf0SChristopher Smith 'auth_nameencode_callback', $name 766ab5d26daSAndreas Gohr ); 767e838fc2eSAndreas Gohr } 7686c2bb100SAndreas Gohr } 7696c2bb100SAndreas Gohr 770a424cd8eSchris return $cache[$name][$skip_group]; 771a424cd8eSchris} 772a424cd8eSchris 77304d68ae4SGerrit Uitslag/** 77404d68ae4SGerrit Uitslag * callback encodes the matches 77504d68ae4SGerrit Uitslag * 77604d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 77704d68ae4SGerrit Uitslag * @return string 77804d68ae4SGerrit Uitslag */ 77930f6faf0SChristopher Smithfunction auth_nameencode_callback($matches) { 78030f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1],-1))); 78130f6faf0SChristopher Smith} 78230f6faf0SChristopher Smith 7836c2bb100SAndreas Gohr/** 784f3f0262cSandi * Create a pronouncable password 785f3f0262cSandi * 7868a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 7878a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 7888a285f7fSAndreas Gohr * 78915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 79015fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 7918a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 79215fae107Sandi * 7938a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 79415fae107Sandi * @return string pronouncable password 795f3f0262cSandi */ 7968a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 7978a285f7fSAndreas Gohr $data = array( 798d628dcf3SAndreas Gohr 'password' => '', 799d628dcf3SAndreas Gohr 'foruser' => $foruser 8008a285f7fSAndreas Gohr ); 8018a285f7fSAndreas Gohr 802e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 8038a285f7fSAndreas Gohr if($evt->advise_before(true)) { 804f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 805f3f0262cSandi $v = 'aeiou'; //vowels 806f3f0262cSandi $a = $c.$v; //both 807987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 808f3f0262cSandi 809987c8d26SAndreas Gohr //use thre syllables... 810987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 811483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 812483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 813483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 814f3f0262cSandi } 815987c8d26SAndreas Gohr //... and add a nice number and special 81643f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)].auth_random(10, 99); 8178a285f7fSAndreas Gohr } 8188a285f7fSAndreas Gohr $evt->advise_after(); 819f3f0262cSandi 8208a285f7fSAndreas Gohr return $data['password']; 821f3f0262cSandi} 822f3f0262cSandi 823f3f0262cSandi/** 824f3f0262cSandi * Sends a password to the given user 825f3f0262cSandi * 82615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 82742ea7f44SGerrit Uitslag * 828ab5d26daSAndreas Gohr * @param string $user Login name of the user 829ab5d26daSAndreas Gohr * @param string $password The new password in clear text 83015fae107Sandi * @return bool true on success 831f3f0262cSandi */ 832f3f0262cSandifunction auth_sendPassword($user, $password) { 833f3f0262cSandi global $lang; 834e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 835cd52f92dSchris global $auth; 836beca106aSAdrian Lang if(!$auth) return false; 837cd52f92dSchris 838d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8392dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 840f3f0262cSandi 84187ddda95Sandi if(!$userinfo['mail']) return false; 842f3f0262cSandi 843f3f0262cSandi $text = rawLocale('password'); 844d7169d19SAndreas Gohr $trep = array( 845d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 846d7169d19SAndreas Gohr 'LOGIN' => $user, 847d7169d19SAndreas Gohr 'PASSWORD' => $password 848d7169d19SAndreas Gohr ); 849f3f0262cSandi 850d7169d19SAndreas Gohr $mail = new Mailer(); 851102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']).' <'.$userinfo['mail'].'>'); 852d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 853d7169d19SAndreas Gohr $mail->setBody($text, $trep); 854d7169d19SAndreas Gohr return $mail->send(); 855f3f0262cSandi} 856f3f0262cSandi 857f3f0262cSandi/** 85815fae107Sandi * Register a new user 859f3f0262cSandi * 86015fae107Sandi * This registers a new user - Data is read directly from $_POST 86115fae107Sandi * 86215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 86342ea7f44SGerrit Uitslag * 86415fae107Sandi * @return bool true on success, false on any error 865f3f0262cSandi */ 866f3f0262cSandifunction register() { 867f3f0262cSandi global $lang; 868eb5d07e4Sjan global $conf; 869e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 870cd52f92dSchris global $auth; 87164273335SAndreas Gohr global $INPUT; 872f3f0262cSandi 87364273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 8743a48618aSAnika Henke if(!actionOK('register')) return false; 875640145a5Sandi 87664273335SAndreas Gohr // gather input 87764273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 87864273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 87964273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 88064273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 88164273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 882d752aedeSAndreas Gohr 88364273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 884f3f0262cSandi msg($lang['regmissing'], -1); 885f3f0262cSandi return false; 886f3f0262cSandi } 887f3f0262cSandi 888cab2716aSmatthias.grimm if($conf['autopasswd']) { 8898a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 89064273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 891bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 892cab2716aSmatthias.grimm return false; 89364273335SAndreas Gohr } elseif($pass != $passchk) { 894bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 895cab2716aSmatthias.grimm return false; 896cab2716aSmatthias.grimm } 897cab2716aSmatthias.grimm 898f3f0262cSandi //check mail 89964273335SAndreas Gohr if(!mail_isvalid($email)) { 900f3f0262cSandi msg($lang['regbadmail'], -1); 901f3f0262cSandi return false; 902f3f0262cSandi } 903f3f0262cSandi 904f3f0262cSandi //okay try to create the user 90564273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 906db9faf02SPatrick Brown msg($lang['regfail'], -1); 907f3f0262cSandi return false; 908f3f0262cSandi } 909f3f0262cSandi 910790b7720SAndreas Gohr // send notification about the new user 91175d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 91275d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 91302a498e7Schris 914790b7720SAndreas Gohr // are we done? 915cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 916cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 917cab2716aSmatthias.grimm return true; 918cab2716aSmatthias.grimm } 919cab2716aSmatthias.grimm 920790b7720SAndreas Gohr // autogenerated password? then send password to user 92164273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 922f3f0262cSandi msg($lang['regsuccess'], 1); 923f3f0262cSandi return true; 924f3f0262cSandi } else { 925f3f0262cSandi msg($lang['regmailfail'], -1); 926f3f0262cSandi return false; 927f3f0262cSandi } 928f3f0262cSandi} 929f3f0262cSandi 93010a76f6fSfrank/** 9318b06d178Schris * Update user profile 9328b06d178Schris * 9338b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9348b06d178Schris */ 9358b06d178Schrisfunction updateprofile() { 9368b06d178Schris global $conf; 9378b06d178Schris global $lang; 938e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 939cd52f92dSchris global $auth; 940bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 941bcc94b2cSAndreas Gohr global $INPUT; 9428b06d178Schris 943bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9441b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9458b06d178Schris 9463a48618aSAnika Henke if(!actionOK('profile')) { 9478b06d178Schris msg($lang['profna'], -1); 9488b06d178Schris return false; 9498b06d178Schris } 9508b06d178Schris 951bcc94b2cSAndreas Gohr $changes = array(); 952bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 953bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 954bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 955bcc94b2cSAndreas Gohr 956bcc94b2cSAndreas Gohr // check misspelled passwords 957bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 958bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 9598b06d178Schris return false; 9608b06d178Schris } 9618b06d178Schris 9628b06d178Schris // clean fullname and email 963bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 964bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 9658b06d178Schris 966bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 967bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 968bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 969ab5d26daSAndreas Gohr ) { 9708b06d178Schris msg($lang['profnoempty'], -1); 9718b06d178Schris return false; 9728b06d178Schris } 973bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 9748b06d178Schris msg($lang['regbadmail'], -1); 9758b06d178Schris return false; 9768b06d178Schris } 9778b06d178Schris 978bcc94b2cSAndreas Gohr $changes = array_filter($changes); 9794c21b7eeSAndreas Gohr 980bcc94b2cSAndreas Gohr // check for unavailable capabilities 981bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 982bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 983bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 984bcc94b2cSAndreas Gohr 985bcc94b2cSAndreas Gohr // anything to do? 9868b06d178Schris if(!count($changes)) { 9878b06d178Schris msg($lang['profnochange'], -1); 9888b06d178Schris return false; 9898b06d178Schris } 9908b06d178Schris 9918b06d178Schris if($conf['profileconfirm']) { 992585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 99371422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 9948b06d178Schris return false; 9958b06d178Schris } 9968b06d178Schris } 9978b06d178Schris 998e6c4392fSPatrick Brown if(!$auth->triggerUserMod('modify', array($INPUT->server->str('REMOTE_USER'), &$changes))) { 999db9faf02SPatrick Brown msg($lang['proffail'], -1); 1000db9faf02SPatrick Brown return false; 1001db9faf02SPatrick Brown } 1002db9faf02SPatrick Brown 100332ed2b36SAndreas Gohr if($changes['pass']) { 1004c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1005ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 100604369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1007585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1008c276e9e8SMarcel Pennewiss } else { 1009c276e9e8SMarcel Pennewiss // make sure the session is writable 1010c276e9e8SMarcel Pennewiss @session_start(); 1011c276e9e8SMarcel Pennewiss // invalidate session cache 1012c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1013c276e9e8SMarcel Pennewiss session_write_close(); 101432ed2b36SAndreas Gohr } 1015c276e9e8SMarcel Pennewiss 101625b2a98cSMichael Klier return true; 1017a0b5b007SChris Smith} 1018ab5d26daSAndreas Gohr 101904d68ae4SGerrit Uitslag/** 102004d68ae4SGerrit Uitslag * Delete the current logged-in user 102104d68ae4SGerrit Uitslag * 102204d68ae4SGerrit Uitslag * @return bool true on success, false on any error 102304d68ae4SGerrit Uitslag */ 10242a7abf2dSChristopher Smithfunction auth_deleteprofile(){ 10252a7abf2dSChristopher Smith global $conf; 10262a7abf2dSChristopher Smith global $lang; 1027e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 10282a7abf2dSChristopher Smith global $auth; 10292a7abf2dSChristopher Smith /* @var Input $INPUT */ 10302a7abf2dSChristopher Smith global $INPUT; 10312a7abf2dSChristopher Smith 10322a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 10332a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 10342a7abf2dSChristopher Smith 10352a7abf2dSChristopher Smith // action prevented or auth module disallows 10362a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10372a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10382a7abf2dSChristopher Smith return false; 10392a7abf2dSChristopher Smith } 10402a7abf2dSChristopher Smith 10412a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 10422a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10432a7abf2dSChristopher Smith return false; 10442a7abf2dSChristopher Smith } 10452a7abf2dSChristopher Smith 10462a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1047585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10482a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10492a7abf2dSChristopher Smith return false; 10502a7abf2dSChristopher Smith } 10512a7abf2dSChristopher Smith } 10522a7abf2dSChristopher Smith 105359bc3b48SGerrit Uitslag $deleted = array(); 1054585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 105573012efdSChristopher Smith if($auth->triggerUserMod('delete', array($deleted))) { 10562a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 10572a7abf2dSChristopher Smith auth_logoff(); 10582a7abf2dSChristopher Smith return true; 10592a7abf2dSChristopher Smith } 10602a7abf2dSChristopher Smith 10612a7abf2dSChristopher Smith return false; 10622a7abf2dSChristopher Smith} 10632a7abf2dSChristopher Smith 10648b06d178Schris/** 10658b06d178Schris * Send a new password 10668b06d178Schris * 10671d5856cfSAndreas Gohr * This function handles both phases of the password reset: 10681d5856cfSAndreas Gohr * 10691d5856cfSAndreas Gohr * - handling the first request of password reset 10701d5856cfSAndreas Gohr * - validating the password reset auth token 10711d5856cfSAndreas Gohr * 10728b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 10738b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 10741d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10758b06d178Schris * 10768b06d178Schris * @return bool true on success, false on any error 10778b06d178Schris */ 10788b06d178Schrisfunction act_resendpwd() { 10798b06d178Schris global $lang; 10808b06d178Schris global $conf; 1081e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1082cd52f92dSchris global $auth; 1083bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1084bcc94b2cSAndreas Gohr global $INPUT; 10858b06d178Schris 10863a48618aSAnika Henke if(!actionOK('resendpwd')) { 10878b06d178Schris msg($lang['resendna'], -1); 10888b06d178Schris return false; 10898b06d178Schris } 10908b06d178Schris 1091bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 10928b06d178Schris 10931d5856cfSAndreas Gohr if($token) { 1094cc204bbdSAndreas Gohr // we're in token phase - get user info from token 10951d5856cfSAndreas Gohr 10962401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 109779e79377SAndreas Gohr if(!file_exists($tfile)) { 10981d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1099bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11001d5856cfSAndreas Gohr return false; 11011d5856cfSAndreas Gohr } 11028a9735e3SAndreas Gohr // token is only valid for 3 days 11038a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11048a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1105bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11061d5856cfSAndreas Gohr @unlink($tfile); 11078a9735e3SAndreas Gohr return false; 11088a9735e3SAndreas Gohr } 11098a9735e3SAndreas Gohr 11108b06d178Schris $user = io_readfile($tfile); 11112dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11128b06d178Schris if(!$userinfo['mail']) { 11138b06d178Schris msg($lang['resendpwdnouser'], -1); 11148b06d178Schris return false; 11158b06d178Schris } 11168b06d178Schris 1117cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1118bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1119bcc94b2cSAndreas Gohr 1120cc204bbdSAndreas Gohr // password given correctly? 1121bcc94b2cSAndreas Gohr if(!$pass) return false; 1122bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1123451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1124cc204bbdSAndreas Gohr return false; 1125cc204bbdSAndreas Gohr } 1126cc204bbdSAndreas Gohr 1127bcc94b2cSAndreas Gohr // change it 1128cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1129db9faf02SPatrick Brown msg($lang['proffail'], -1); 1130cc204bbdSAndreas Gohr return false; 1131cc204bbdSAndreas Gohr } 1132cc204bbdSAndreas Gohr 1133cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1134cc204bbdSAndreas Gohr 11358a285f7fSAndreas Gohr $pass = auth_pwgen($user); 11367d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1137db9faf02SPatrick Brown msg($lang['proffail'], -1); 11388b06d178Schris return false; 11398b06d178Schris } 11408b06d178Schris 11418b06d178Schris if(auth_sendPassword($user, $pass)) { 11428b06d178Schris msg($lang['resendpwdsuccess'], 1); 11438b06d178Schris } else { 11448b06d178Schris msg($lang['regmailfail'], -1); 11458b06d178Schris } 1146cc204bbdSAndreas Gohr } 1147cc204bbdSAndreas Gohr 1148cc204bbdSAndreas Gohr @unlink($tfile); 11498b06d178Schris return true; 11501d5856cfSAndreas Gohr 11511d5856cfSAndreas Gohr } else { 11521d5856cfSAndreas Gohr // we're in request phase 11531d5856cfSAndreas Gohr 1154bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 11551d5856cfSAndreas Gohr 1156bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 11571d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 11581d5856cfSAndreas Gohr return false; 11591d5856cfSAndreas Gohr } else { 1160bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 11611d5856cfSAndreas Gohr } 11621d5856cfSAndreas Gohr 11632dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11641d5856cfSAndreas Gohr if(!$userinfo['mail']) { 11651d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 11661d5856cfSAndreas Gohr return false; 11671d5856cfSAndreas Gohr } 11681d5856cfSAndreas Gohr 11691d5856cfSAndreas Gohr // generate auth token 1170483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 11712401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 11721d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 11731d5856cfSAndreas Gohr 11741d5856cfSAndreas Gohr io_saveFile($tfile, $user); 11751d5856cfSAndreas Gohr 11761d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1177d7169d19SAndreas Gohr $trep = array( 1178d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1179d7169d19SAndreas Gohr 'LOGIN' => $user, 1180d7169d19SAndreas Gohr 'CONFIRM' => $url 1181d7169d19SAndreas Gohr ); 11821d5856cfSAndreas Gohr 1183d7169d19SAndreas Gohr $mail = new Mailer(); 1184d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1185d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1186d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1187d7169d19SAndreas Gohr if($mail->send()) { 11881d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 11891d5856cfSAndreas Gohr } else { 11901d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 11911d5856cfSAndreas Gohr } 11921d5856cfSAndreas Gohr return true; 11931d5856cfSAndreas Gohr } 1194ab5d26daSAndreas Gohr // never reached 11958b06d178Schris} 11968b06d178Schris 11978b06d178Schris/** 1198b0855b11Sandi * Encrypts a password using the given method and salt 1199b0855b11Sandi * 1200b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1201b0855b11Sandi * is chosen. 1202b0855b11Sandi * 1203b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 120442ea7f44SGerrit Uitslag * 1205ab5d26daSAndreas Gohr * @param string $clear The clear text password 1206ab5d26daSAndreas Gohr * @param string $method The hashing method 1207ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1208b0855b11Sandi * @return string The crypted password 1209b0855b11Sandi */ 1210577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1211b0855b11Sandi global $conf; 1212b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 121310a76f6fSfrank 12143a0a2d05SAndreas Gohr $pass = new PassHash(); 12153a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1216b0855b11Sandi 12173a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1218b0855b11Sandi msg("Unsupported crypt method $method", -1); 12193a0a2d05SAndreas Gohr return false; 1220b0855b11Sandi } 12213a0a2d05SAndreas Gohr 12223a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1223b0855b11Sandi} 1224b0855b11Sandi 1225b0855b11Sandi/** 1226b0855b11Sandi * Verifies a cleartext password against a crypted hash 1227b0855b11Sandi * 1228b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 122942ea7f44SGerrit Uitslag * 1230ab5d26daSAndreas Gohr * @param string $clear The clear text password 1231ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1232ab5d26daSAndreas Gohr * @return bool true if both match 1233b0855b11Sandi */ 1234b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 12353a0a2d05SAndreas Gohr $pass = new PassHash(); 12363a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1237b0855b11Sandi} 1238340756e4Sandi 1239a0b5b007SChris Smith/** 1240a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1241a0b5b007SChris Smith * 1242a0b5b007SChris Smith * @param string $user username 1243a0b5b007SChris Smith * @param string $pass encrypted password 1244a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1245ab5d26daSAndreas Gohr * @return bool 1246a0b5b007SChris Smith */ 1247a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1248a0b5b007SChris Smith global $conf; 1249e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1250a0b5b007SChris Smith global $auth; 125179d00841SOliver Geisen global $USERINFO; 1252a0b5b007SChris Smith 1253beca106aSAdrian Lang if(!$auth) return false; 1254a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1255a0b5b007SChris Smith 1256a0b5b007SChris Smith // set cookie 1257645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 125873ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1259c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 126073ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 126155a71a16SGerrit Uitslag 1262a0b5b007SChris Smith // set session 1263a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1264234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1265a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1266a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1267a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1268ab5d26daSAndreas Gohr 1269ab5d26daSAndreas Gohr return true; 1270a0b5b007SChris Smith} 1271a0b5b007SChris Smith 1272645c0a36SAndreas Gohr/** 1273645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1274645c0a36SAndreas Gohr * 1275645c0a36SAndreas Gohr * @returns array 1276645c0a36SAndreas Gohr */ 1277645c0a36SAndreas Gohrfunction auth_getCookie() { 1278c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1279c66972f2SAdrian Lang return array(null, null, null); 1280c66972f2SAdrian Lang } 1281645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1282645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1283645c0a36SAndreas Gohr $pass = base64_decode($pass); 1284645c0a36SAndreas Gohr $user = base64_decode($user); 1285645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1286645c0a36SAndreas Gohr} 1287645c0a36SAndreas Gohr 1288e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1289