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 13c3cc6e05SAndreas Gohruse dokuwiki\PassHash; 1475d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 15e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 163a7140a1SAndreas Gohruse dokuwiki\Extension\PluginController; 17e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 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 1038eca974cSAndreas Gohr if(!is_null($auth) && $auth->canDo('external')) { 104f13fa892SAndreas Gohr // external trust mechanism in place 105bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 106f5cb575dSAndreas Gohr } else { 1076080c584SRobin Gareus $evdata = array( 108bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 109bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 110bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 111bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1126080c584SRobin Gareus ); 113cbb44eabSAndreas Gohr Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 114f5cb575dSAndreas Gohr } 115f5cb575dSAndreas Gohr 11616905344SAndreas Gohr //load ACL into a global array XXX 11775c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 118ab5d26daSAndreas Gohr 119ab5d26daSAndreas Gohr return true; 12075c93b77SAndreas Gohr} 12175c93b77SAndreas Gohr 12275c93b77SAndreas Gohr/** 12375c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12475c93b77SAndreas Gohr * 12575c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12642ea7f44SGerrit Uitslag * 127ab5d26daSAndreas Gohr * @return array 12875c93b77SAndreas Gohr */ 12975c93b77SAndreas Gohrfunction auth_loadACL() { 13075c93b77SAndreas Gohr global $config_cascade; 131b78bf706Sromain global $USERINFO; 132585bf44eSChristopher Smith /* @var Input $INPUT */ 133585bf44eSChristopher Smith global $INPUT; 13475c93b77SAndreas Gohr 13575c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 13675c93b77SAndreas Gohr 13775c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13875c93b77SAndreas Gohr 13932e82180SAndreas Gohr $out = array(); 1409ce556d2SAndreas Gohr foreach($acl as $line) { 1419ce556d2SAndreas Gohr $line = trim($line); 142*2401f18dSSyntaxseed if(empty($line) || ($line[0] == '#')) continue; // skip blank lines & comments 14321c3090aSChristopher Smith list($id,$rest) = preg_split('/[ \t]+/',$line,2); 14432e82180SAndreas Gohr 145443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 146ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 147ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 148585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 149ad3d68d7SChristopher Smith 150585bf44eSChristopher Smith $id = str_replace('%USER%',cleanID($INPUT->server->str('REMOTE_USER')),$id); 151585bf44eSChristopher Smith $rest = str_replace('%USER%',auth_nameencode($INPUT->server->str('REMOTE_USER')),$rest); 152ad3d68d7SChristopher Smith } 153ad3d68d7SChristopher Smith 154ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1559ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 156ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 1579ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 158b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 15932e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 16032e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 161b78bf706Sromain } 16232e82180SAndreas Gohr } else { 16332e82180SAndreas Gohr $out[] = "$id\t$rest"; 164a8fe108bSGuy Brand } 16511799630Sandi } 1669ce556d2SAndreas Gohr 16732e82180SAndreas Gohr return $out; 168f3f0262cSandi} 169f3f0262cSandi 170ab5d26daSAndreas Gohr/** 171ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 172ab5d26daSAndreas Gohr * 17342ea7f44SGerrit Uitslag * @param array $evdata 174ab5d26daSAndreas Gohr * @return bool 175ab5d26daSAndreas Gohr */ 176b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 177ab5d26daSAndreas Gohr return auth_login( 178ab5d26daSAndreas Gohr $evdata['user'], 179b5ee21aaSAdrian Lang $evdata['password'], 180b5ee21aaSAdrian Lang $evdata['sticky'], 181ab5d26daSAndreas Gohr $evdata['silent'] 182ab5d26daSAndreas Gohr ); 183b5ee21aaSAdrian Lang} 184b5ee21aaSAdrian Lang 185f3f0262cSandi/** 186f3f0262cSandi * This tries to login the user based on the sent auth credentials 187f3f0262cSandi * 188f3f0262cSandi * The authentication works like this: if a username was given 18915fae107Sandi * a new login is assumed and user/password are checked. If they 19015fae107Sandi * are correct the password is encrypted with blowfish and stored 19115fae107Sandi * together with the username in a cookie - the same info is stored 19215fae107Sandi * in the session, too. Additonally a browserID is stored in the 19315fae107Sandi * session. 19415fae107Sandi * 19515fae107Sandi * If no username was given the cookie is checked: if the username, 19615fae107Sandi * crypted password and browserID match between session and cookie 19715fae107Sandi * no further testing is done and the user is accepted 19815fae107Sandi * 19915fae107Sandi * If a cookie was found but no session info was availabe the 200136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 20115fae107Sandi * together with username rechecked by calling this function again. 202f3f0262cSandi * 203f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 204f3f0262cSandi * are set. 20515fae107Sandi * 20615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20715fae107Sandi * 20815fae107Sandi * @param string $user Username 20915fae107Sandi * @param string $pass Cleartext Password 21015fae107Sandi * @param bool $sticky Cookie should not expire 211f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 21215fae107Sandi * @return bool true on successful auth 213f3f0262cSandi */ 214f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 215f3f0262cSandi global $USERINFO; 216f3f0262cSandi global $conf; 217f3f0262cSandi global $lang; 218e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 219cd52f92dSchris global $auth; 220585bf44eSChristopher Smith /* @var Input $INPUT */ 221585bf44eSChristopher Smith global $INPUT; 222ab5d26daSAndreas Gohr 223132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 224f3f0262cSandi 225beca106aSAdrian Lang if(!$auth) return false; 226beca106aSAdrian Lang 227bbbd6568SAndreas Gohr if(!empty($user)) { 228132bdbfeSandi //usual login 2295e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 230132bdbfeSandi // make logininfo globally available 231585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 23230d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 23304369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 234132bdbfeSandi return true; 235f3f0262cSandi } else { 236f3f0262cSandi //invalid credentials - log off 237f8b1e4e7SAndreas Gohr if(!$silent) { 238f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 239f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 240f8b1e4e7SAndreas Gohr } 241f3f0262cSandi auth_logoff(); 242132bdbfeSandi return false; 243f3f0262cSandi } 244f3f0262cSandi } else { 245132bdbfeSandi // read cookie information 246645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 247132bdbfeSandi if($user && $pass) { 248132bdbfeSandi // we got a cookie - see if we can trust it 249fa7c70ffSAdrian Lang 250fa7c70ffSAdrian Lang // get session info 251fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 252132bdbfeSandi if(isset($session) && 2537172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2544c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 255132bdbfeSandi ($session['user'] == $user) && 256234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 257ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 258ab5d26daSAndreas Gohr ) { 259234ce57eSAndreas Gohr 260132bdbfeSandi // he has session, cookie and browser right - let him in 261585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 262132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 263132bdbfeSandi return true; 264132bdbfeSandi } 265f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 26630d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26704369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 268f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 269132bdbfeSandi } 270132bdbfeSandi } 271f3f0262cSandi //just to be sure 272883179a4SAndreas Gohr auth_logoff(true); 273132bdbfeSandi return false; 274f3f0262cSandi} 275132bdbfeSandi 276132bdbfeSandi/** 277136ce040Sandi * Builds a pseudo UID from browser and IP data 278132bdbfeSandi * 279132bdbfeSandi * This is neither unique nor unfakable - still it adds some 280136ce040Sandi * security. Using the first part of the IP makes sure 28180b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 28215fae107Sandi * 28315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 28415fae107Sandi * 28515fae107Sandi * @return string a MD5 sum of various browser headers 286132bdbfeSandi */ 287132bdbfeSandifunction auth_browseruid() { 288585bf44eSChristopher Smith /* @var Input $INPUT */ 289585bf44eSChristopher Smith global $INPUT; 290585bf44eSChristopher Smith 2912f9daf16SAndreas Gohr $ip = clientIP(true); 292132bdbfeSandi $uid = ''; 293585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_USER_AGENT'); 294585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_CHARSET'); 2952f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 29680b4f376SAndreas Gohr $uid = strtolower($uid); 297132bdbfeSandi return md5($uid); 298132bdbfeSandi} 299132bdbfeSandi 300132bdbfeSandi/** 301132bdbfeSandi * Creates a random key to encrypt the password in cookies 30215fae107Sandi * 30315fae107Sandi * This function tries to read the password for encrypting 30498407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 30515fae107Sandi * if no such file is found a random key is created and 30615fae107Sandi * and stored in this file. 30715fae107Sandi * 30815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30942ea7f44SGerrit Uitslag * 31032ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 31130d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 31215fae107Sandi * @return string 313132bdbfeSandi */ 31430d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 315a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 316fe745becSMichael Große return 'test'; 317a1fe3c9cSMichael Große } 318132bdbfeSandi global $conf; 31998407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 32030d544a4SMichael Hamann if ($secure || !file_exists($file)) { 32130d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 32230d544a4SMichael Hamann } 323132bdbfeSandi $salt = io_readFile($file); 324132bdbfeSandi if(empty($salt)) { 32530d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 326132bdbfeSandi io_saveFile($file, $salt); 327132bdbfeSandi } 32832ed2b36SAndreas Gohr if($addsession) { 32932ed2b36SAndreas Gohr $salt .= session_id(); 33032ed2b36SAndreas Gohr } 331132bdbfeSandi return $salt; 332f3f0262cSandi} 333f3f0262cSandi 334f3f0262cSandi/** 3357a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 336483b6238SMichael Hamann * 3377a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 33842ea7f44SGerrit Uitslag * 3397a33d2f8SNiklas Keller * @param int $length number of bytes 3407a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 341483b6238SMichael Hamann */ 342483b6238SMichael Hamannfunction auth_randombytes($length) { 3437a33d2f8SNiklas Keller return random_bytes($length); 344483b6238SMichael Hamann} 345483b6238SMichael Hamann 346483b6238SMichael Hamann/** 3477a33d2f8SNiklas Keller * Cryptographically secure random number generator. 348483b6238SMichael Hamann * 3497a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 35042ea7f44SGerrit Uitslag * 351483b6238SMichael Hamann * @param int $min 352483b6238SMichael Hamann * @param int $max 353483b6238SMichael Hamann * @return int 354483b6238SMichael Hamann */ 355483b6238SMichael Hamannfunction auth_random($min, $max) { 3567a33d2f8SNiklas Keller return random_int($min, $max); 357483b6238SMichael Hamann} 358483b6238SMichael Hamann 359483b6238SMichael Hamann/** 36004369c3eSMichael Hamann * Encrypt data using the given secret using AES 36104369c3eSMichael Hamann * 36204369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 36304369c3eSMichael Hamann * using pbkdf2. 36404369c3eSMichael Hamann * 36504369c3eSMichael Hamann * @param string $data The data that shall be encrypted 36604369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 36704369c3eSMichael Hamann * @return string The ciphertext 36804369c3eSMichael Hamann */ 36904369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 37004369c3eSMichael Hamann $iv = auth_randombytes(16); 3711af2f135SAndreas Gohr $cipher = new \phpseclib\Crypt\AES(); 37204369c3eSMichael Hamann $cipher->setPassword($secret); 37304369c3eSMichael Hamann 3747b650cefSMichael Hamann /* 3757b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 3767b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 3777b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 3787b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 3797b650cefSMichael Hamann */ 38004369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 38104369c3eSMichael Hamann} 38204369c3eSMichael Hamann 38304369c3eSMichael Hamann/** 38404369c3eSMichael Hamann * Decrypt the given AES ciphertext 38504369c3eSMichael Hamann * 38604369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 38704369c3eSMichael Hamann * 38804369c3eSMichael Hamann * @param string $ciphertext The encrypted data 38904369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 39004369c3eSMichael Hamann * @return string The decrypted data 39104369c3eSMichael Hamann */ 39204369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 3937b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 3941af2f135SAndreas Gohr $cipher = new \phpseclib\Crypt\AES(); 39504369c3eSMichael Hamann $cipher->setPassword($secret); 3967b650cefSMichael Hamann $cipher->setIV($iv); 39704369c3eSMichael Hamann 3987b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 39904369c3eSMichael Hamann} 40004369c3eSMichael Hamann 40104369c3eSMichael Hamann/** 402883179a4SAndreas Gohr * Log out the current user 403883179a4SAndreas Gohr * 404f3f0262cSandi * This clears all authentication data and thus log the user 405883179a4SAndreas Gohr * off. It also clears session data. 40615fae107Sandi * 40715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 40842ea7f44SGerrit Uitslag * 409883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 410f3f0262cSandi */ 411883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 412f3f0262cSandi global $conf; 413f3f0262cSandi global $USERINFO; 414e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4155298a619SAndreas Gohr global $auth; 416585bf44eSChristopher Smith /* @var Input $INPUT */ 417585bf44eSChristopher Smith global $INPUT; 41837065e65Sandi 419d4869846SAndreas Gohr // make sure the session is writable (it usually is) 420e9621d07SAndreas Gohr @session_start(); 421e9621d07SAndreas Gohr 422e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 423e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 424e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 425e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 426e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 427e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 428883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 429e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 430585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 431132bdbfeSandi $USERINFO = null; //FIXME 432f5c6743cSAndreas Gohr 43373ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 43473ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 4355298a619SAndreas Gohr 436880f62faSAndreas Gohr if($auth) $auth->logOff(); 437f3f0262cSandi} 438f3f0262cSandi 439f3f0262cSandi/** 440f8cc712eSAndreas Gohr * Check if a user is a manager 441f8cc712eSAndreas Gohr * 442f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 443f8cc712eSAndreas Gohr * user. 444f8cc712eSAndreas Gohr * 445f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 446f8cc712eSAndreas Gohr * 447f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 448f8cc712eSAndreas Gohr * @see auth_isadmin 44942ea7f44SGerrit Uitslag * 450ab5d26daSAndreas Gohr * @param string $user Username 451ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 452ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 453ab5d26daSAndreas Gohr * @return bool 454f8cc712eSAndreas Gohr */ 455f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 456f8cc712eSAndreas Gohr global $conf; 457f8cc712eSAndreas Gohr global $USERINFO; 458e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 459d752aedeSAndreas Gohr global $auth; 460585bf44eSChristopher Smith /* @var Input $INPUT */ 461585bf44eSChristopher Smith global $INPUT; 462585bf44eSChristopher Smith 463f8cc712eSAndreas Gohr 464beca106aSAdrian Lang if(!$auth) return false; 465c66972f2SAdrian Lang if(is_null($user)) { 466585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 467c66972f2SAdrian Lang return false; 468c66972f2SAdrian Lang } else { 469585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 470c66972f2SAdrian Lang } 471c66972f2SAdrian Lang } 472d6dc956fSAndreas Gohr if(is_null($groups)) { 473d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 474e259aa79SAndreas Gohr } 475e259aa79SAndreas Gohr 476d6dc956fSAndreas Gohr // check superuser match 477d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 478d6dc956fSAndreas Gohr if($adminonly) return false; 479e259aa79SAndreas Gohr // check managers 480d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 48100ce12daSChris Smith 482f8cc712eSAndreas Gohr return false; 483f8cc712eSAndreas Gohr} 484f8cc712eSAndreas Gohr 485f8cc712eSAndreas Gohr/** 486f8cc712eSAndreas Gohr * Check if a user is admin 487f8cc712eSAndreas Gohr * 488f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 489f8cc712eSAndreas Gohr * 490f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 491f8cc712eSAndreas Gohr * 492f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 493ab5d26daSAndreas Gohr * @see auth_ismanager() 49442ea7f44SGerrit Uitslag * 495ab5d26daSAndreas Gohr * @param string $user Username 496ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 497ab5d26daSAndreas Gohr * @return bool 498f8cc712eSAndreas Gohr */ 499f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 500f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 501f8cc712eSAndreas Gohr} 502f8cc712eSAndreas Gohr 503d6dc956fSAndreas Gohr/** 504d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 505d6dc956fSAndreas Gohr * users and groups to determine membership status 506d6dc956fSAndreas Gohr * 507d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 508d6dc956fSAndreas Gohr * 50942ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 51042ea7f44SGerrit Uitslag * @param string $user user to match against 51142ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5125446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 513d6dc956fSAndreas Gohr */ 514d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 515e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 516d6dc956fSAndreas Gohr global $auth; 517d6dc956fSAndreas Gohr if(!$auth) return false; 518d6dc956fSAndreas Gohr 519d6dc956fSAndreas Gohr // clean user and groups 5204f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 5218cbc5ee8SAndreas Gohr $user = \dokuwiki\Utf8\PhpString::strtolower($user); 522d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 523d6dc956fSAndreas Gohr } 524d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 525d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 526d6dc956fSAndreas Gohr 527d6dc956fSAndreas Gohr // extract the memberlist 528d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 529d6dc956fSAndreas Gohr $members = array_map('trim', $members); 530d6dc956fSAndreas Gohr $members = array_unique($members); 531d6dc956fSAndreas Gohr $members = array_filter($members); 532d6dc956fSAndreas Gohr 533d6dc956fSAndreas Gohr // compare cleaned values 534d6dc956fSAndreas Gohr foreach($members as $member) { 535e5204a12SJurgen Hart if($member == '@ALL' ) return true; 5368cbc5ee8SAndreas Gohr if(!$auth->isCaseSensitive()) $member = \dokuwiki\Utf8\PhpString::strtolower($member); 537d6dc956fSAndreas Gohr if($member[0] == '@') { 538d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 539d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 540d6dc956fSAndreas Gohr } else { 541d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 542d6dc956fSAndreas Gohr if($member == $user) return true; 543d6dc956fSAndreas Gohr } 544d6dc956fSAndreas Gohr } 545d6dc956fSAndreas Gohr 546d6dc956fSAndreas Gohr // still here? not a member! 547d6dc956fSAndreas Gohr return false; 548d6dc956fSAndreas Gohr} 549d6dc956fSAndreas Gohr 550f8cc712eSAndreas Gohr/** 55115fae107Sandi * Convinience function for auth_aclcheck() 55215fae107Sandi * 55315fae107Sandi * This checks the permissions for the current user 55415fae107Sandi * 55515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 55615fae107Sandi * 5571698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 55815fae107Sandi * @return int permission level 559f3f0262cSandi */ 560f3f0262cSandifunction auth_quickaclcheck($id) { 561f3f0262cSandi global $conf; 562f3f0262cSandi global $USERINFO; 563585bf44eSChristopher Smith /* @var Input $INPUT */ 564585bf44eSChristopher Smith global $INPUT; 565f3f0262cSandi # if no ACL is used always return upload rights 566f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 567585bf44eSChristopher Smith return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 568f3f0262cSandi} 569f3f0262cSandi 570f3f0262cSandi/** 571c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 57215fae107Sandi * 57315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 57442ea7f44SGerrit Uitslag * 575c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 5761698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 57715fae107Sandi * @param string $user Username 5783272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 57915fae107Sandi * @return int permission level 580f3f0262cSandi */ 581f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 582c17acc9fSAndreas Gohr $data = array( 583c17acc9fSAndreas Gohr 'id' => $id, 584c17acc9fSAndreas Gohr 'user' => $user, 585c17acc9fSAndreas Gohr 'groups' => $groups 586c17acc9fSAndreas Gohr ); 587c17acc9fSAndreas Gohr 588cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 589c17acc9fSAndreas Gohr} 590c17acc9fSAndreas Gohr 591c17acc9fSAndreas Gohr/** 592c17acc9fSAndreas Gohr * default ACL check method 593c17acc9fSAndreas Gohr * 594c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 595c17acc9fSAndreas Gohr * 596c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 59742ea7f44SGerrit Uitslag * 598c17acc9fSAndreas Gohr * @param array $data event data 599c17acc9fSAndreas Gohr * @return int permission level 600c17acc9fSAndreas Gohr */ 601c17acc9fSAndreas Gohrfunction auth_aclcheck_cb($data) { 602c17acc9fSAndreas Gohr $id =& $data['id']; 603c17acc9fSAndreas Gohr $user =& $data['user']; 604c17acc9fSAndreas Gohr $groups =& $data['groups']; 605c17acc9fSAndreas Gohr 606f3f0262cSandi global $conf; 607f3f0262cSandi global $AUTH_ACL; 608e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 609d752aedeSAndreas Gohr global $auth; 610f3f0262cSandi 61185d03f68SAndreas Gohr // if no ACL is used always return upload rights 612f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 613beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 614f3f0262cSandi 615074cf26bSandi //make sure groups is an array 616074cf26bSandi if(!is_array($groups)) $groups = array(); 617074cf26bSandi 61885d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 619ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 620ab5d26daSAndreas Gohr return AUTH_ADMIN; 621ab5d26daSAndreas Gohr } 62285d03f68SAndreas Gohr 623eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 6248cbc5ee8SAndreas Gohr $user = \dokuwiki\Utf8\PhpString::strtolower($user); 625eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 626eb3ce0d5SKazutaka Miyasaka } 62737ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 628d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 62985d03f68SAndreas Gohr 6306c2bb100SAndreas Gohr //prepend groups with @ and nameencode 63137ff2261SSascha Klopp foreach($groups as &$group) { 63237ff2261SSascha Klopp $group = '@'.auth_nameencode($group); 63310a76f6fSfrank } 63410a76f6fSfrank 635f3f0262cSandi $ns = getNS($id); 636f3f0262cSandi $perm = -1; 637f3f0262cSandi 638f3f0262cSandi //add ALL group 639f3f0262cSandi $groups[] = '@ALL'; 64037ff2261SSascha Klopp 641f3f0262cSandi //add User 64234aeb4afSAndreas Gohr if($user) $groups[] = $user; 643f3f0262cSandi 644f3f0262cSandi //check exact match first 64521c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 646f3f0262cSandi if(count($matches)) { 647f3f0262cSandi foreach($matches as $match) { 648f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 64921c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 650eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 6518cbc5ee8SAndreas Gohr $acl[1] = \dokuwiki\Utf8\PhpString::strtolower($acl[1]); 652eb3ce0d5SKazutaka Miyasaka } 65348d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 65448d7b7a6SDominik Eckelmann continue; 65548d7b7a6SDominik Eckelmann } 6568ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 657f3f0262cSandi if($acl[2] > $perm) { 658f3f0262cSandi $perm = $acl[2]; 659f3f0262cSandi } 660f3f0262cSandi } 661f3f0262cSandi if($perm > -1) { 662f3f0262cSandi //we had a match - return it 663def492a2SGuillaume Turri return (int) $perm; 664f3f0262cSandi } 665f3f0262cSandi } 666f3f0262cSandi 667f3f0262cSandi //still here? do the namespace checks 668f3f0262cSandi if($ns) { 6693e304b55SMichael Hamann $path = $ns.':*'; 670f3f0262cSandi } else { 6713e304b55SMichael Hamann $path = '*'; //root document 672f3f0262cSandi } 673f3f0262cSandi 674f3f0262cSandi do { 67521c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 676f3f0262cSandi if(count($matches)) { 677f3f0262cSandi foreach($matches as $match) { 678f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 67921c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 680eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 6818cbc5ee8SAndreas Gohr $acl[1] = \dokuwiki\Utf8\PhpString::strtolower($acl[1]); 682eb3ce0d5SKazutaka Miyasaka } 68348d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 68448d7b7a6SDominik Eckelmann continue; 68548d7b7a6SDominik Eckelmann } 6868ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 687f3f0262cSandi if($acl[2] > $perm) { 688f3f0262cSandi $perm = $acl[2]; 689f3f0262cSandi } 690f3f0262cSandi } 691f3f0262cSandi //we had a match - return it 69248d7b7a6SDominik Eckelmann if($perm != -1) { 693def492a2SGuillaume Turri return (int) $perm; 694f3f0262cSandi } 69548d7b7a6SDominik Eckelmann } 696f3f0262cSandi //get next higher namespace 697f3f0262cSandi $ns = getNS($ns); 698f3f0262cSandi 6993e304b55SMichael Hamann if($path != '*') { 7003e304b55SMichael Hamann $path = $ns.':*'; 7013e304b55SMichael Hamann if($path == ':*') $path = '*'; 702f3f0262cSandi } else { 703f3f0262cSandi //we did this already 704f3f0262cSandi //looks like there is something wrong with the ACL 705f3f0262cSandi //break here 706d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 707d5ce66f6SAndreas Gohr return AUTH_NONE; 708f3f0262cSandi } 709f3f0262cSandi } while(1); //this should never loop endless 710ab5d26daSAndreas Gohr return AUTH_NONE; 711f3f0262cSandi} 712f3f0262cSandi 713f3f0262cSandi/** 7146c2bb100SAndreas Gohr * Encode ASCII special chars 7156c2bb100SAndreas Gohr * 7166c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7176c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7186c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7196c2bb100SAndreas Gohr * urlencoding!). 7206c2bb100SAndreas Gohr * 7216c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7226c2bb100SAndreas Gohr * 7236c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7246c2bb100SAndreas Gohr * @see rawurldecode() 72542ea7f44SGerrit Uitslag * 72642ea7f44SGerrit Uitslag * @param string $name 72742ea7f44SGerrit Uitslag * @param bool $skip_group 72842ea7f44SGerrit Uitslag * @return string 7296c2bb100SAndreas Gohr */ 730e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 731a424cd8eSchris global $cache_authname; 732a424cd8eSchris $cache =& $cache_authname; 73331784267SAndreas Gohr $name = (string) $name; 734a424cd8eSchris 73580601d26SAndreas Gohr // never encode wildcard FS#1955 73680601d26SAndreas Gohr if($name == '%USER%') return $name; 737b78bf706Sromain if($name == '%GROUP%') return $name; 73880601d26SAndreas Gohr 739a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 740*2401f18dSSyntaxseed if($skip_group && $name[0] == '@') { 74130f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 74230f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 74330f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 744ab5d26daSAndreas Gohr ); 745e838fc2eSAndreas Gohr } else { 74630f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 74730f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 74830f6faf0SChristopher Smith 'auth_nameencode_callback', $name 749ab5d26daSAndreas Gohr ); 750e838fc2eSAndreas Gohr } 7516c2bb100SAndreas Gohr } 7526c2bb100SAndreas Gohr 753a424cd8eSchris return $cache[$name][$skip_group]; 754a424cd8eSchris} 755a424cd8eSchris 75604d68ae4SGerrit Uitslag/** 75704d68ae4SGerrit Uitslag * callback encodes the matches 75804d68ae4SGerrit Uitslag * 75904d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 76004d68ae4SGerrit Uitslag * @return string 76104d68ae4SGerrit Uitslag */ 76230f6faf0SChristopher Smithfunction auth_nameencode_callback($matches) { 76330f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1],-1))); 76430f6faf0SChristopher Smith} 76530f6faf0SChristopher Smith 7666c2bb100SAndreas Gohr/** 767f3f0262cSandi * Create a pronouncable password 768f3f0262cSandi * 7698a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 7708a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 7718a285f7fSAndreas Gohr * 77215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 77315fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 7748a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 77515fae107Sandi * 7768a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 77715fae107Sandi * @return string pronouncable password 778f3f0262cSandi */ 7798a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 7808a285f7fSAndreas Gohr $data = array( 781d628dcf3SAndreas Gohr 'password' => '', 782d628dcf3SAndreas Gohr 'foruser' => $foruser 7838a285f7fSAndreas Gohr ); 7848a285f7fSAndreas Gohr 785e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 7868a285f7fSAndreas Gohr if($evt->advise_before(true)) { 787f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 788f3f0262cSandi $v = 'aeiou'; //vowels 789f3f0262cSandi $a = $c.$v; //both 790987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 791f3f0262cSandi 792987c8d26SAndreas Gohr //use thre syllables... 793987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 794483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 795483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 796483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 797f3f0262cSandi } 798987c8d26SAndreas Gohr //... and add a nice number and special 79943f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)].auth_random(10, 99); 8008a285f7fSAndreas Gohr } 8018a285f7fSAndreas Gohr $evt->advise_after(); 802f3f0262cSandi 8038a285f7fSAndreas Gohr return $data['password']; 804f3f0262cSandi} 805f3f0262cSandi 806f3f0262cSandi/** 807f3f0262cSandi * Sends a password to the given user 808f3f0262cSandi * 80915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 81042ea7f44SGerrit Uitslag * 811ab5d26daSAndreas Gohr * @param string $user Login name of the user 812ab5d26daSAndreas Gohr * @param string $password The new password in clear text 81315fae107Sandi * @return bool true on success 814f3f0262cSandi */ 815f3f0262cSandifunction auth_sendPassword($user, $password) { 816f3f0262cSandi global $lang; 817e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 818cd52f92dSchris global $auth; 819beca106aSAdrian Lang if(!$auth) return false; 820cd52f92dSchris 821d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8222dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 823f3f0262cSandi 82487ddda95Sandi if(!$userinfo['mail']) return false; 825f3f0262cSandi 826f3f0262cSandi $text = rawLocale('password'); 827d7169d19SAndreas Gohr $trep = array( 828d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 829d7169d19SAndreas Gohr 'LOGIN' => $user, 830d7169d19SAndreas Gohr 'PASSWORD' => $password 831d7169d19SAndreas Gohr ); 832f3f0262cSandi 833d7169d19SAndreas Gohr $mail = new Mailer(); 834102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']).' <'.$userinfo['mail'].'>'); 835d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 836d7169d19SAndreas Gohr $mail->setBody($text, $trep); 837d7169d19SAndreas Gohr return $mail->send(); 838f3f0262cSandi} 839f3f0262cSandi 840f3f0262cSandi/** 84115fae107Sandi * Register a new user 842f3f0262cSandi * 84315fae107Sandi * This registers a new user - Data is read directly from $_POST 84415fae107Sandi * 84515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 84642ea7f44SGerrit Uitslag * 84715fae107Sandi * @return bool true on success, false on any error 848f3f0262cSandi */ 849f3f0262cSandifunction register() { 850f3f0262cSandi global $lang; 851eb5d07e4Sjan global $conf; 852e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 853cd52f92dSchris global $auth; 85464273335SAndreas Gohr global $INPUT; 855f3f0262cSandi 85664273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 8573a48618aSAnika Henke if(!actionOK('register')) return false; 858640145a5Sandi 85964273335SAndreas Gohr // gather input 86064273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 86164273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 86264273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 86364273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 86464273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 865d752aedeSAndreas Gohr 86664273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 867f3f0262cSandi msg($lang['regmissing'], -1); 868f3f0262cSandi return false; 869f3f0262cSandi } 870f3f0262cSandi 871cab2716aSmatthias.grimm if($conf['autopasswd']) { 8728a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 87364273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 874bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 875cab2716aSmatthias.grimm return false; 87664273335SAndreas Gohr } elseif($pass != $passchk) { 877bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 878cab2716aSmatthias.grimm return false; 879cab2716aSmatthias.grimm } 880cab2716aSmatthias.grimm 881f3f0262cSandi //check mail 88264273335SAndreas Gohr if(!mail_isvalid($email)) { 883f3f0262cSandi msg($lang['regbadmail'], -1); 884f3f0262cSandi return false; 885f3f0262cSandi } 886f3f0262cSandi 887f3f0262cSandi //okay try to create the user 88864273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 889db9faf02SPatrick Brown msg($lang['regfail'], -1); 890f3f0262cSandi return false; 891f3f0262cSandi } 892f3f0262cSandi 893790b7720SAndreas Gohr // send notification about the new user 89475d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 89575d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 89602a498e7Schris 897790b7720SAndreas Gohr // are we done? 898cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 899cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 900cab2716aSmatthias.grimm return true; 901cab2716aSmatthias.grimm } 902cab2716aSmatthias.grimm 903790b7720SAndreas Gohr // autogenerated password? then send password to user 90464273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 905f3f0262cSandi msg($lang['regsuccess'], 1); 906f3f0262cSandi return true; 907f3f0262cSandi } else { 908f3f0262cSandi msg($lang['regmailfail'], -1); 909f3f0262cSandi return false; 910f3f0262cSandi } 911f3f0262cSandi} 912f3f0262cSandi 91310a76f6fSfrank/** 9148b06d178Schris * Update user profile 9158b06d178Schris * 9168b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9178b06d178Schris */ 9188b06d178Schrisfunction updateprofile() { 9198b06d178Schris global $conf; 9208b06d178Schris global $lang; 921e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 922cd52f92dSchris global $auth; 923bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 924bcc94b2cSAndreas Gohr global $INPUT; 9258b06d178Schris 926bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9271b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9288b06d178Schris 9293a48618aSAnika Henke if(!actionOK('profile')) { 9308b06d178Schris msg($lang['profna'], -1); 9318b06d178Schris return false; 9328b06d178Schris } 9338b06d178Schris 934bcc94b2cSAndreas Gohr $changes = array(); 935bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 936bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 937bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 938bcc94b2cSAndreas Gohr 939bcc94b2cSAndreas Gohr // check misspelled passwords 940bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 941bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 9428b06d178Schris return false; 9438b06d178Schris } 9448b06d178Schris 9458b06d178Schris // clean fullname and email 946bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 947bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 9488b06d178Schris 949bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 950bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 951bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 952ab5d26daSAndreas Gohr ) { 9538b06d178Schris msg($lang['profnoempty'], -1); 9548b06d178Schris return false; 9558b06d178Schris } 956bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 9578b06d178Schris msg($lang['regbadmail'], -1); 9588b06d178Schris return false; 9598b06d178Schris } 9608b06d178Schris 961bcc94b2cSAndreas Gohr $changes = array_filter($changes); 9624c21b7eeSAndreas Gohr 963bcc94b2cSAndreas Gohr // check for unavailable capabilities 964bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 965bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 966bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 967bcc94b2cSAndreas Gohr 968bcc94b2cSAndreas Gohr // anything to do? 9698b06d178Schris if(!count($changes)) { 9708b06d178Schris msg($lang['profnochange'], -1); 9718b06d178Schris return false; 9728b06d178Schris } 9738b06d178Schris 9748b06d178Schris if($conf['profileconfirm']) { 975585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 97671422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 9778b06d178Schris return false; 9788b06d178Schris } 9798b06d178Schris } 9808b06d178Schris 981e6c4392fSPatrick Brown if(!$auth->triggerUserMod('modify', array($INPUT->server->str('REMOTE_USER'), &$changes))) { 982db9faf02SPatrick Brown msg($lang['proffail'], -1); 983db9faf02SPatrick Brown return false; 984db9faf02SPatrick Brown } 985db9faf02SPatrick Brown 98632ed2b36SAndreas Gohr if($changes['pass']) { 987c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 988ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 98904369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 990585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 991c276e9e8SMarcel Pennewiss } else { 992c276e9e8SMarcel Pennewiss // make sure the session is writable 993c276e9e8SMarcel Pennewiss @session_start(); 994c276e9e8SMarcel Pennewiss // invalidate session cache 995c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 996c276e9e8SMarcel Pennewiss session_write_close(); 99732ed2b36SAndreas Gohr } 998c276e9e8SMarcel Pennewiss 99925b2a98cSMichael Klier return true; 1000a0b5b007SChris Smith} 1001ab5d26daSAndreas Gohr 100204d68ae4SGerrit Uitslag/** 100304d68ae4SGerrit Uitslag * Delete the current logged-in user 100404d68ae4SGerrit Uitslag * 100504d68ae4SGerrit Uitslag * @return bool true on success, false on any error 100604d68ae4SGerrit Uitslag */ 10072a7abf2dSChristopher Smithfunction auth_deleteprofile(){ 10082a7abf2dSChristopher Smith global $conf; 10092a7abf2dSChristopher Smith global $lang; 1010e1d9dcc8SAndreas Gohr /* @var \dokuwiki\Extension\AuthPlugin $auth */ 10112a7abf2dSChristopher Smith global $auth; 10122a7abf2dSChristopher Smith /* @var Input $INPUT */ 10132a7abf2dSChristopher Smith global $INPUT; 10142a7abf2dSChristopher Smith 10152a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 10162a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 10172a7abf2dSChristopher Smith 10182a7abf2dSChristopher Smith // action prevented or auth module disallows 10192a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10202a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10212a7abf2dSChristopher Smith return false; 10222a7abf2dSChristopher Smith } 10232a7abf2dSChristopher Smith 10242a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 10252a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10262a7abf2dSChristopher Smith return false; 10272a7abf2dSChristopher Smith } 10282a7abf2dSChristopher Smith 10292a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1030585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10312a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10322a7abf2dSChristopher Smith return false; 10332a7abf2dSChristopher Smith } 10342a7abf2dSChristopher Smith } 10352a7abf2dSChristopher Smith 103659bc3b48SGerrit Uitslag $deleted = array(); 1037585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 103873012efdSChristopher Smith if($auth->triggerUserMod('delete', array($deleted))) { 10392a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 10402a7abf2dSChristopher Smith auth_logoff(); 10412a7abf2dSChristopher Smith return true; 10422a7abf2dSChristopher Smith } 10432a7abf2dSChristopher Smith 10442a7abf2dSChristopher Smith return false; 10452a7abf2dSChristopher Smith} 10462a7abf2dSChristopher Smith 10478b06d178Schris/** 10488b06d178Schris * Send a new password 10498b06d178Schris * 10501d5856cfSAndreas Gohr * This function handles both phases of the password reset: 10511d5856cfSAndreas Gohr * 10521d5856cfSAndreas Gohr * - handling the first request of password reset 10531d5856cfSAndreas Gohr * - validating the password reset auth token 10541d5856cfSAndreas Gohr * 10558b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 10568b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 10571d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10588b06d178Schris * 10598b06d178Schris * @return bool true on success, false on any error 10608b06d178Schris */ 10618b06d178Schrisfunction act_resendpwd() { 10628b06d178Schris global $lang; 10638b06d178Schris global $conf; 1064e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1065cd52f92dSchris global $auth; 1066bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1067bcc94b2cSAndreas Gohr global $INPUT; 10688b06d178Schris 10693a48618aSAnika Henke if(!actionOK('resendpwd')) { 10708b06d178Schris msg($lang['resendna'], -1); 10718b06d178Schris return false; 10728b06d178Schris } 10738b06d178Schris 1074bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 10758b06d178Schris 10761d5856cfSAndreas Gohr if($token) { 1077cc204bbdSAndreas Gohr // we're in token phase - get user info from token 10781d5856cfSAndreas Gohr 1079*2401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 108079e79377SAndreas Gohr if(!file_exists($tfile)) { 10811d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1082bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 10831d5856cfSAndreas Gohr return false; 10841d5856cfSAndreas Gohr } 10858a9735e3SAndreas Gohr // token is only valid for 3 days 10868a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 10878a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1088bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 10891d5856cfSAndreas Gohr @unlink($tfile); 10908a9735e3SAndreas Gohr return false; 10918a9735e3SAndreas Gohr } 10928a9735e3SAndreas Gohr 10938b06d178Schris $user = io_readfile($tfile); 10942dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 10958b06d178Schris if(!$userinfo['mail']) { 10968b06d178Schris msg($lang['resendpwdnouser'], -1); 10978b06d178Schris return false; 10988b06d178Schris } 10998b06d178Schris 1100cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1101bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1102bcc94b2cSAndreas Gohr 1103cc204bbdSAndreas Gohr // password given correctly? 1104bcc94b2cSAndreas Gohr if(!$pass) return false; 1105bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1106451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1107cc204bbdSAndreas Gohr return false; 1108cc204bbdSAndreas Gohr } 1109cc204bbdSAndreas Gohr 1110bcc94b2cSAndreas Gohr // change it 1111cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1112db9faf02SPatrick Brown msg($lang['proffail'], -1); 1113cc204bbdSAndreas Gohr return false; 1114cc204bbdSAndreas Gohr } 1115cc204bbdSAndreas Gohr 1116cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1117cc204bbdSAndreas Gohr 11188a285f7fSAndreas Gohr $pass = auth_pwgen($user); 11197d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1120db9faf02SPatrick Brown msg($lang['proffail'], -1); 11218b06d178Schris return false; 11228b06d178Schris } 11238b06d178Schris 11248b06d178Schris if(auth_sendPassword($user, $pass)) { 11258b06d178Schris msg($lang['resendpwdsuccess'], 1); 11268b06d178Schris } else { 11278b06d178Schris msg($lang['regmailfail'], -1); 11288b06d178Schris } 1129cc204bbdSAndreas Gohr } 1130cc204bbdSAndreas Gohr 1131cc204bbdSAndreas Gohr @unlink($tfile); 11328b06d178Schris return true; 11331d5856cfSAndreas Gohr 11341d5856cfSAndreas Gohr } else { 11351d5856cfSAndreas Gohr // we're in request phase 11361d5856cfSAndreas Gohr 1137bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 11381d5856cfSAndreas Gohr 1139bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 11401d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 11411d5856cfSAndreas Gohr return false; 11421d5856cfSAndreas Gohr } else { 1143bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 11441d5856cfSAndreas Gohr } 11451d5856cfSAndreas Gohr 11462dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11471d5856cfSAndreas Gohr if(!$userinfo['mail']) { 11481d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 11491d5856cfSAndreas Gohr return false; 11501d5856cfSAndreas Gohr } 11511d5856cfSAndreas Gohr 11521d5856cfSAndreas Gohr // generate auth token 1153483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 1154*2401f18dSSyntaxseed $tfile = $conf['cachedir'].'/'.$token[0].'/'.$token.'.pwauth'; 11551d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 11561d5856cfSAndreas Gohr 11571d5856cfSAndreas Gohr io_saveFile($tfile, $user); 11581d5856cfSAndreas Gohr 11591d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1160d7169d19SAndreas Gohr $trep = array( 1161d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1162d7169d19SAndreas Gohr 'LOGIN' => $user, 1163d7169d19SAndreas Gohr 'CONFIRM' => $url 1164d7169d19SAndreas Gohr ); 11651d5856cfSAndreas Gohr 1166d7169d19SAndreas Gohr $mail = new Mailer(); 1167d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1168d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1169d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1170d7169d19SAndreas Gohr if($mail->send()) { 11711d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 11721d5856cfSAndreas Gohr } else { 11731d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 11741d5856cfSAndreas Gohr } 11751d5856cfSAndreas Gohr return true; 11761d5856cfSAndreas Gohr } 1177ab5d26daSAndreas Gohr // never reached 11788b06d178Schris} 11798b06d178Schris 11808b06d178Schris/** 1181b0855b11Sandi * Encrypts a password using the given method and salt 1182b0855b11Sandi * 1183b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1184b0855b11Sandi * is chosen. 1185b0855b11Sandi * 1186b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 118742ea7f44SGerrit Uitslag * 1188ab5d26daSAndreas Gohr * @param string $clear The clear text password 1189ab5d26daSAndreas Gohr * @param string $method The hashing method 1190ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1191b0855b11Sandi * @return string The crypted password 1192b0855b11Sandi */ 1193577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1194b0855b11Sandi global $conf; 1195b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 119610a76f6fSfrank 11973a0a2d05SAndreas Gohr $pass = new PassHash(); 11983a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1199b0855b11Sandi 12003a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1201b0855b11Sandi msg("Unsupported crypt method $method", -1); 12023a0a2d05SAndreas Gohr return false; 1203b0855b11Sandi } 12043a0a2d05SAndreas Gohr 12053a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1206b0855b11Sandi} 1207b0855b11Sandi 1208b0855b11Sandi/** 1209b0855b11Sandi * Verifies a cleartext password against a crypted hash 1210b0855b11Sandi * 1211b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 121242ea7f44SGerrit Uitslag * 1213ab5d26daSAndreas Gohr * @param string $clear The clear text password 1214ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1215ab5d26daSAndreas Gohr * @return bool true if both match 1216b0855b11Sandi */ 1217b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 12183a0a2d05SAndreas Gohr $pass = new PassHash(); 12193a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1220b0855b11Sandi} 1221340756e4Sandi 1222a0b5b007SChris Smith/** 1223a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1224a0b5b007SChris Smith * 1225a0b5b007SChris Smith * @param string $user username 1226a0b5b007SChris Smith * @param string $pass encrypted password 1227a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1228ab5d26daSAndreas Gohr * @return bool 1229a0b5b007SChris Smith */ 1230a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1231a0b5b007SChris Smith global $conf; 1232e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1233a0b5b007SChris Smith global $auth; 123479d00841SOliver Geisen global $USERINFO; 1235a0b5b007SChris Smith 1236beca106aSAdrian Lang if(!$auth) return false; 1237a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1238a0b5b007SChris Smith 1239a0b5b007SChris Smith // set cookie 1240645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 124173ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1242c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 124373ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 124455a71a16SGerrit Uitslag 1245a0b5b007SChris Smith // set session 1246a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1247234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1248a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1249a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1250a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1251ab5d26daSAndreas Gohr 1252ab5d26daSAndreas Gohr return true; 1253a0b5b007SChris Smith} 1254a0b5b007SChris Smith 1255645c0a36SAndreas Gohr/** 1256645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1257645c0a36SAndreas Gohr * 1258645c0a36SAndreas Gohr * @returns array 1259645c0a36SAndreas Gohr */ 1260645c0a36SAndreas Gohrfunction auth_getCookie() { 1261c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1262c66972f2SAdrian Lang return array(null, null, null); 1263c66972f2SAdrian Lang } 1264645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1265645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1266645c0a36SAndreas Gohr $pass = base64_decode($pass); 1267645c0a36SAndreas Gohr $user = base64_decode($user); 1268645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1269645c0a36SAndreas Gohr} 1270645c0a36SAndreas Gohr 1271e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1272