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; 14*75d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 15c3cc6e05SAndreas Gohr 16ebf97c8fSAndreas Gohrdefine('AUTH_NONE', 0); 17ebf97c8fSAndreas Gohrdefine('AUTH_READ', 1); 18ebf97c8fSAndreas Gohrdefine('AUTH_EDIT', 2); 19ebf97c8fSAndreas Gohrdefine('AUTH_CREATE', 4); 20ebf97c8fSAndreas Gohrdefine('AUTH_UPLOAD', 8); 21ebf97c8fSAndreas Gohrdefine('AUTH_DELETE', 16); 22ebf97c8fSAndreas Gohrdefine('AUTH_ADMIN', 255); 23ebf97c8fSAndreas Gohr 2416905344SAndreas Gohr/** 2516905344SAndreas Gohr * Initialize the auth system. 2616905344SAndreas Gohr * 2716905344SAndreas Gohr * This function is automatically called at the end of init.php 2816905344SAndreas Gohr * 2916905344SAndreas Gohr * This used to be the main() of the auth.php 3016905344SAndreas Gohr * 3116905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 3216905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 33ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 34ab5d26daSAndreas Gohr * @return bool 3516905344SAndreas Gohr */ 3616905344SAndreas Gohrfunction auth_setup() { 37742c66f8Schris global $conf; 3893a7873eSAndreas Gohr /* @var DokuWiki_Auth_Plugin $auth */ 3903c4aec3Schris global $auth; 40bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 41bcc94b2cSAndreas Gohr global $INPUT; 429a9714acSDominik Eckelmann global $AUTH_ACL; 439a9714acSDominik Eckelmann global $lang; 4427058a05SMichael Hamann /* @var Doku_Plugin_Controller $plugin_controller */ 459c29eea5SJan Schumann global $plugin_controller; 469a9714acSDominik Eckelmann $AUTH_ACL = array(); 4703c4aec3Schris 4816905344SAndreas Gohr if(!$conf['useacl']) return false; 4916905344SAndreas Gohr 509c29eea5SJan Schumann // try to load auth backend from plugins 519c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 529c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 53f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 549c29eea5SJan Schumann break; 559c29eea5SJan Schumann } 569c29eea5SJan Schumann } 578b06d178Schris 586416b708SMichael Hamann if(!isset($auth) || !$auth){ 593094e817SAndreas Gohr msg($lang['authtempfail'], -1); 603094e817SAndreas Gohr return false; 613094e817SAndreas Gohr } 628b06d178Schris 636416b708SMichael Hamann if ($auth->success == false) { 640f4f4adfSAndreas Gohr // degrade to unauthenticated user 65d2dde4ebSMatthias Grimm unset($auth); 660f4f4adfSAndreas Gohr auth_logoff(); 67cd52f92dSchris msg($lang['authtempfail'], -1); 686416b708SMichael Hamann return false; 69d2dde4ebSMatthias Grimm } 7016905344SAndreas Gohr 7116905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 72bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 73bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 74bbbd6568SAndreas Gohr 75b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 76b2665af7SMichael Hamann // the one presented at 77b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 78b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 79b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 80b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 81528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 8206156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 83528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 84528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 85528ddc7cSAndreas Gohr } 86528ddc7cSAndreas Gohr 871e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 88bcc94b2cSAndreas Gohr if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 89bcc94b2cSAndreas Gohr $INPUT->set('u', $_SERVER['PHP_AUTH_USER']); 90bcc94b2cSAndreas Gohr $INPUT->set('p', $_SERVER['PHP_AUTH_PW']); 91bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 921e8c9c90SAndreas Gohr } 931e8c9c90SAndreas Gohr 94395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 9593a7873eSAndreas Gohr if (true === $auth->success) { 96395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 97395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 98f4476bd9SJan Schumann } 99191bb90aSAndreas Gohr 1008eca974cSAndreas Gohr if(!is_null($auth) && $auth->canDo('external')) { 101f13fa892SAndreas Gohr // external trust mechanism in place 102bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 103f5cb575dSAndreas Gohr } else { 1046080c584SRobin Gareus $evdata = array( 105bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 106bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 107bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 108bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1096080c584SRobin Gareus ); 110b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 111f5cb575dSAndreas Gohr } 112f5cb575dSAndreas Gohr 11316905344SAndreas Gohr //load ACL into a global array XXX 11475c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 115ab5d26daSAndreas Gohr 116ab5d26daSAndreas Gohr return true; 11775c93b77SAndreas Gohr} 11875c93b77SAndreas Gohr 11975c93b77SAndreas Gohr/** 12075c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12175c93b77SAndreas Gohr * 12275c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12342ea7f44SGerrit Uitslag * 124ab5d26daSAndreas Gohr * @return array 12575c93b77SAndreas Gohr */ 12675c93b77SAndreas Gohrfunction auth_loadACL() { 12775c93b77SAndreas Gohr global $config_cascade; 128b78bf706Sromain global $USERINFO; 129585bf44eSChristopher Smith /* @var Input $INPUT */ 130585bf44eSChristopher Smith global $INPUT; 13175c93b77SAndreas Gohr 13275c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 13375c93b77SAndreas Gohr 13475c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13575c93b77SAndreas Gohr 13632e82180SAndreas Gohr $out = array(); 1379ce556d2SAndreas Gohr foreach($acl as $line) { 1389ce556d2SAndreas Gohr $line = trim($line); 139443e135dSChristopher Smith if(empty($line) || ($line{0} == '#')) continue; // skip blank lines & comments 14021c3090aSChristopher Smith list($id,$rest) = preg_split('/[ \t]+/',$line,2); 14132e82180SAndreas Gohr 142443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 143ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 144ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 145585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 146ad3d68d7SChristopher Smith 147585bf44eSChristopher Smith $id = str_replace('%USER%',cleanID($INPUT->server->str('REMOTE_USER')),$id); 148585bf44eSChristopher Smith $rest = str_replace('%USER%',auth_nameencode($INPUT->server->str('REMOTE_USER')),$rest); 149ad3d68d7SChristopher Smith } 150ad3d68d7SChristopher Smith 151ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1529ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 153ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 1549ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 155b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 15632e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 15732e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 158b78bf706Sromain } 15932e82180SAndreas Gohr } else { 16032e82180SAndreas Gohr $out[] = "$id\t$rest"; 161a8fe108bSGuy Brand } 16211799630Sandi } 1639ce556d2SAndreas Gohr 16432e82180SAndreas Gohr return $out; 165f3f0262cSandi} 166f3f0262cSandi 167ab5d26daSAndreas Gohr/** 168ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 169ab5d26daSAndreas Gohr * 17042ea7f44SGerrit Uitslag * @param array $evdata 171ab5d26daSAndreas Gohr * @return bool 172ab5d26daSAndreas Gohr */ 173b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 174ab5d26daSAndreas Gohr return auth_login( 175ab5d26daSAndreas Gohr $evdata['user'], 176b5ee21aaSAdrian Lang $evdata['password'], 177b5ee21aaSAdrian Lang $evdata['sticky'], 178ab5d26daSAndreas Gohr $evdata['silent'] 179ab5d26daSAndreas Gohr ); 180b5ee21aaSAdrian Lang} 181b5ee21aaSAdrian Lang 182f3f0262cSandi/** 183f3f0262cSandi * This tries to login the user based on the sent auth credentials 184f3f0262cSandi * 185f3f0262cSandi * The authentication works like this: if a username was given 18615fae107Sandi * a new login is assumed and user/password are checked. If they 18715fae107Sandi * are correct the password is encrypted with blowfish and stored 18815fae107Sandi * together with the username in a cookie - the same info is stored 18915fae107Sandi * in the session, too. Additonally a browserID is stored in the 19015fae107Sandi * session. 19115fae107Sandi * 19215fae107Sandi * If no username was given the cookie is checked: if the username, 19315fae107Sandi * crypted password and browserID match between session and cookie 19415fae107Sandi * no further testing is done and the user is accepted 19515fae107Sandi * 19615fae107Sandi * If a cookie was found but no session info was availabe the 197136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 19815fae107Sandi * together with username rechecked by calling this function again. 199f3f0262cSandi * 200f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 201f3f0262cSandi * are set. 20215fae107Sandi * 20315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20415fae107Sandi * 20515fae107Sandi * @param string $user Username 20615fae107Sandi * @param string $pass Cleartext Password 20715fae107Sandi * @param bool $sticky Cookie should not expire 208f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 20915fae107Sandi * @return bool true on successful auth 210f3f0262cSandi */ 211f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 212f3f0262cSandi global $USERINFO; 213f3f0262cSandi global $conf; 214f3f0262cSandi global $lang; 21527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 216cd52f92dSchris global $auth; 217585bf44eSChristopher Smith /* @var Input $INPUT */ 218585bf44eSChristopher Smith global $INPUT; 219ab5d26daSAndreas Gohr 220132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 221f3f0262cSandi 222beca106aSAdrian Lang if(!$auth) return false; 223beca106aSAdrian Lang 224bbbd6568SAndreas Gohr if(!empty($user)) { 225132bdbfeSandi //usual login 2265e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 227132bdbfeSandi // make logininfo globally available 228585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 22930d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 23004369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 231132bdbfeSandi return true; 232f3f0262cSandi } else { 233f3f0262cSandi //invalid credentials - log off 234f8b1e4e7SAndreas Gohr if(!$silent) { 235f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 236f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 237f8b1e4e7SAndreas Gohr } 238f3f0262cSandi auth_logoff(); 239132bdbfeSandi return false; 240f3f0262cSandi } 241f3f0262cSandi } else { 242132bdbfeSandi // read cookie information 243645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 244132bdbfeSandi if($user && $pass) { 245132bdbfeSandi // we got a cookie - see if we can trust it 246fa7c70ffSAdrian Lang 247fa7c70ffSAdrian Lang // get session info 248fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 249132bdbfeSandi if(isset($session) && 2507172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2514c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 252132bdbfeSandi ($session['user'] == $user) && 253234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 254ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 255ab5d26daSAndreas Gohr ) { 256234ce57eSAndreas Gohr 257132bdbfeSandi // he has session, cookie and browser right - let him in 258585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 259132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 260132bdbfeSandi return true; 261132bdbfeSandi } 262f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 26330d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26404369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 265f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 266132bdbfeSandi } 267132bdbfeSandi } 268f3f0262cSandi //just to be sure 269883179a4SAndreas Gohr auth_logoff(true); 270132bdbfeSandi return false; 271f3f0262cSandi} 272132bdbfeSandi 273132bdbfeSandi/** 274136ce040Sandi * Builds a pseudo UID from browser and IP data 275132bdbfeSandi * 276132bdbfeSandi * This is neither unique nor unfakable - still it adds some 277136ce040Sandi * security. Using the first part of the IP makes sure 27880b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 27915fae107Sandi * 28015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 28115fae107Sandi * 28215fae107Sandi * @return string a MD5 sum of various browser headers 283132bdbfeSandi */ 284132bdbfeSandifunction auth_browseruid() { 285585bf44eSChristopher Smith /* @var Input $INPUT */ 286585bf44eSChristopher Smith global $INPUT; 287585bf44eSChristopher Smith 2882f9daf16SAndreas Gohr $ip = clientIP(true); 289132bdbfeSandi $uid = ''; 290585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_USER_AGENT'); 291585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_CHARSET'); 2922f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 29380b4f376SAndreas Gohr $uid = strtolower($uid); 294132bdbfeSandi return md5($uid); 295132bdbfeSandi} 296132bdbfeSandi 297132bdbfeSandi/** 298132bdbfeSandi * Creates a random key to encrypt the password in cookies 29915fae107Sandi * 30015fae107Sandi * This function tries to read the password for encrypting 30198407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 30215fae107Sandi * if no such file is found a random key is created and 30315fae107Sandi * and stored in this file. 30415fae107Sandi * 30515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30642ea7f44SGerrit Uitslag * 30732ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 30830d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 30915fae107Sandi * @return string 310132bdbfeSandi */ 31130d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 312a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 313fe745becSMichael Große return 'test'; 314a1fe3c9cSMichael Große } 315132bdbfeSandi global $conf; 31698407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 31730d544a4SMichael Hamann if ($secure || !file_exists($file)) { 31830d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 31930d544a4SMichael Hamann } 320132bdbfeSandi $salt = io_readFile($file); 321132bdbfeSandi if(empty($salt)) { 32230d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 323132bdbfeSandi io_saveFile($file, $salt); 324132bdbfeSandi } 32532ed2b36SAndreas Gohr if($addsession) { 32632ed2b36SAndreas Gohr $salt .= session_id(); 32732ed2b36SAndreas Gohr } 328132bdbfeSandi return $salt; 329f3f0262cSandi} 330f3f0262cSandi 331f3f0262cSandi/** 3327a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 333483b6238SMichael Hamann * 3347a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 33542ea7f44SGerrit Uitslag * 3367a33d2f8SNiklas Keller * @param int $length number of bytes 3377a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 338483b6238SMichael Hamann */ 339483b6238SMichael Hamannfunction auth_randombytes($length) { 3407a33d2f8SNiklas Keller return random_bytes($length); 341483b6238SMichael Hamann} 342483b6238SMichael Hamann 343483b6238SMichael Hamann/** 3447a33d2f8SNiklas Keller * Cryptographically secure random number generator. 345483b6238SMichael Hamann * 3467a33d2f8SNiklas Keller * @author Niklas Keller <me@kelunik.com> 34742ea7f44SGerrit Uitslag * 348483b6238SMichael Hamann * @param int $min 349483b6238SMichael Hamann * @param int $max 350483b6238SMichael Hamann * @return int 351483b6238SMichael Hamann */ 352483b6238SMichael Hamannfunction auth_random($min, $max) { 3537a33d2f8SNiklas Keller return random_int($min, $max); 354483b6238SMichael Hamann} 355483b6238SMichael Hamann 356483b6238SMichael Hamann/** 35704369c3eSMichael Hamann * Encrypt data using the given secret using AES 35804369c3eSMichael Hamann * 35904369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 36004369c3eSMichael Hamann * using pbkdf2. 36104369c3eSMichael Hamann * 36204369c3eSMichael Hamann * @param string $data The data that shall be encrypted 36304369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 36404369c3eSMichael Hamann * @return string The ciphertext 36504369c3eSMichael Hamann */ 36604369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 36704369c3eSMichael Hamann $iv = auth_randombytes(16); 3681af2f135SAndreas Gohr $cipher = new \phpseclib\Crypt\AES(); 36904369c3eSMichael Hamann $cipher->setPassword($secret); 37004369c3eSMichael Hamann 3717b650cefSMichael Hamann /* 3727b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 3737b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 3747b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 3757b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 3767b650cefSMichael Hamann */ 37704369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 37804369c3eSMichael Hamann} 37904369c3eSMichael Hamann 38004369c3eSMichael Hamann/** 38104369c3eSMichael Hamann * Decrypt the given AES ciphertext 38204369c3eSMichael Hamann * 38304369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 38404369c3eSMichael Hamann * 38504369c3eSMichael Hamann * @param string $ciphertext The encrypted data 38604369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 38704369c3eSMichael Hamann * @return string The decrypted data 38804369c3eSMichael Hamann */ 38904369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 3907b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 3911af2f135SAndreas Gohr $cipher = new \phpseclib\Crypt\AES(); 39204369c3eSMichael Hamann $cipher->setPassword($secret); 3937b650cefSMichael Hamann $cipher->setIV($iv); 39404369c3eSMichael Hamann 3957b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 39604369c3eSMichael Hamann} 39704369c3eSMichael Hamann 39804369c3eSMichael Hamann/** 399883179a4SAndreas Gohr * Log out the current user 400883179a4SAndreas Gohr * 401f3f0262cSandi * This clears all authentication data and thus log the user 402883179a4SAndreas Gohr * off. It also clears session data. 40315fae107Sandi * 40415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 40542ea7f44SGerrit Uitslag * 406883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 407f3f0262cSandi */ 408883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 409f3f0262cSandi global $conf; 410f3f0262cSandi global $USERINFO; 41127058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 4125298a619SAndreas Gohr global $auth; 413585bf44eSChristopher Smith /* @var Input $INPUT */ 414585bf44eSChristopher Smith global $INPUT; 41537065e65Sandi 416d4869846SAndreas Gohr // make sure the session is writable (it usually is) 417e9621d07SAndreas Gohr @session_start(); 418e9621d07SAndreas Gohr 419e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 420e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 421e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 422e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 423e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 424e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 425883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 426e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 427585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 428132bdbfeSandi $USERINFO = null; //FIXME 429f5c6743cSAndreas Gohr 43073ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 43173ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 4325298a619SAndreas Gohr 433880f62faSAndreas Gohr if($auth) $auth->logOff(); 434f3f0262cSandi} 435f3f0262cSandi 436f3f0262cSandi/** 437f8cc712eSAndreas Gohr * Check if a user is a manager 438f8cc712eSAndreas Gohr * 439f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 440f8cc712eSAndreas Gohr * user. 441f8cc712eSAndreas Gohr * 442f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 443f8cc712eSAndreas Gohr * 444f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 445f8cc712eSAndreas Gohr * @see auth_isadmin 44642ea7f44SGerrit Uitslag * 447ab5d26daSAndreas Gohr * @param string $user Username 448ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 449ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 450ab5d26daSAndreas Gohr * @return bool 451f8cc712eSAndreas Gohr */ 452f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 453f8cc712eSAndreas Gohr global $conf; 454f8cc712eSAndreas Gohr global $USERINFO; 45527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 456d752aedeSAndreas Gohr global $auth; 457585bf44eSChristopher Smith /* @var Input $INPUT */ 458585bf44eSChristopher Smith global $INPUT; 459585bf44eSChristopher Smith 460f8cc712eSAndreas Gohr 461beca106aSAdrian Lang if(!$auth) return false; 462c66972f2SAdrian Lang if(is_null($user)) { 463585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 464c66972f2SAdrian Lang return false; 465c66972f2SAdrian Lang } else { 466585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 467c66972f2SAdrian Lang } 468c66972f2SAdrian Lang } 469d6dc956fSAndreas Gohr if(is_null($groups)) { 470d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 471e259aa79SAndreas Gohr } 472e259aa79SAndreas Gohr 473d6dc956fSAndreas Gohr // check superuser match 474d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 475d6dc956fSAndreas Gohr if($adminonly) return false; 476e259aa79SAndreas Gohr // check managers 477d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 47800ce12daSChris Smith 479f8cc712eSAndreas Gohr return false; 480f8cc712eSAndreas Gohr} 481f8cc712eSAndreas Gohr 482f8cc712eSAndreas Gohr/** 483f8cc712eSAndreas Gohr * Check if a user is admin 484f8cc712eSAndreas Gohr * 485f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 486f8cc712eSAndreas Gohr * 487f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 488f8cc712eSAndreas Gohr * 489f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 490ab5d26daSAndreas Gohr * @see auth_ismanager() 49142ea7f44SGerrit Uitslag * 492ab5d26daSAndreas Gohr * @param string $user Username 493ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 494ab5d26daSAndreas Gohr * @return bool 495f8cc712eSAndreas Gohr */ 496f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 497f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 498f8cc712eSAndreas Gohr} 499f8cc712eSAndreas Gohr 500d6dc956fSAndreas Gohr/** 501d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 502d6dc956fSAndreas Gohr * users and groups to determine membership status 503d6dc956fSAndreas Gohr * 504d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 505d6dc956fSAndreas Gohr * 50642ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 50742ea7f44SGerrit Uitslag * @param string $user user to match against 50842ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5095446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 510d6dc956fSAndreas Gohr */ 511d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 51227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 513d6dc956fSAndreas Gohr global $auth; 514d6dc956fSAndreas Gohr if(!$auth) return false; 515d6dc956fSAndreas Gohr 516d6dc956fSAndreas Gohr // clean user and groups 5174f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 518d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 519d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 520d6dc956fSAndreas Gohr } 521d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 522d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 523d6dc956fSAndreas Gohr 524d6dc956fSAndreas Gohr // extract the memberlist 525d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 526d6dc956fSAndreas Gohr $members = array_map('trim', $members); 527d6dc956fSAndreas Gohr $members = array_unique($members); 528d6dc956fSAndreas Gohr $members = array_filter($members); 529d6dc956fSAndreas Gohr 530d6dc956fSAndreas Gohr // compare cleaned values 531d6dc956fSAndreas Gohr foreach($members as $member) { 532e5204a12SJurgen Hart if($member == '@ALL' ) return true; 5334f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 534d6dc956fSAndreas Gohr if($member[0] == '@') { 535d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 536d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 537d6dc956fSAndreas Gohr } else { 538d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 539d6dc956fSAndreas Gohr if($member == $user) return true; 540d6dc956fSAndreas Gohr } 541d6dc956fSAndreas Gohr } 542d6dc956fSAndreas Gohr 543d6dc956fSAndreas Gohr // still here? not a member! 544d6dc956fSAndreas Gohr return false; 545d6dc956fSAndreas Gohr} 546d6dc956fSAndreas Gohr 547f8cc712eSAndreas Gohr/** 54815fae107Sandi * Convinience function for auth_aclcheck() 54915fae107Sandi * 55015fae107Sandi * This checks the permissions for the current user 55115fae107Sandi * 55215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 55315fae107Sandi * 5541698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 55515fae107Sandi * @return int permission level 556f3f0262cSandi */ 557f3f0262cSandifunction auth_quickaclcheck($id) { 558f3f0262cSandi global $conf; 559f3f0262cSandi global $USERINFO; 560585bf44eSChristopher Smith /* @var Input $INPUT */ 561585bf44eSChristopher Smith global $INPUT; 562f3f0262cSandi # if no ACL is used always return upload rights 563f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 564585bf44eSChristopher Smith return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 565f3f0262cSandi} 566f3f0262cSandi 567f3f0262cSandi/** 568c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 56915fae107Sandi * 57015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 57142ea7f44SGerrit Uitslag * 572c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 5731698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 57415fae107Sandi * @param string $user Username 5753272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 57615fae107Sandi * @return int permission level 577f3f0262cSandi */ 578f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 579c17acc9fSAndreas Gohr $data = array( 580c17acc9fSAndreas Gohr 'id' => $id, 581c17acc9fSAndreas Gohr 'user' => $user, 582c17acc9fSAndreas Gohr 'groups' => $groups 583c17acc9fSAndreas Gohr ); 584c17acc9fSAndreas Gohr 585c17acc9fSAndreas Gohr return trigger_event('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 586c17acc9fSAndreas Gohr} 587c17acc9fSAndreas Gohr 588c17acc9fSAndreas Gohr/** 589c17acc9fSAndreas Gohr * default ACL check method 590c17acc9fSAndreas Gohr * 591c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 592c17acc9fSAndreas Gohr * 593c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 59442ea7f44SGerrit Uitslag * 595c17acc9fSAndreas Gohr * @param array $data event data 596c17acc9fSAndreas Gohr * @return int permission level 597c17acc9fSAndreas Gohr */ 598c17acc9fSAndreas Gohrfunction auth_aclcheck_cb($data) { 599c17acc9fSAndreas Gohr $id =& $data['id']; 600c17acc9fSAndreas Gohr $user =& $data['user']; 601c17acc9fSAndreas Gohr $groups =& $data['groups']; 602c17acc9fSAndreas Gohr 603f3f0262cSandi global $conf; 604f3f0262cSandi global $AUTH_ACL; 60527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 606d752aedeSAndreas Gohr global $auth; 607f3f0262cSandi 60885d03f68SAndreas Gohr // if no ACL is used always return upload rights 609f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 610beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 611f3f0262cSandi 612074cf26bSandi //make sure groups is an array 613074cf26bSandi if(!is_array($groups)) $groups = array(); 614074cf26bSandi 61585d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 616ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 617ab5d26daSAndreas Gohr return AUTH_ADMIN; 618ab5d26daSAndreas Gohr } 61985d03f68SAndreas Gohr 620eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 621eb3ce0d5SKazutaka Miyasaka $user = utf8_strtolower($user); 622eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 623eb3ce0d5SKazutaka Miyasaka } 62437ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 625d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 62685d03f68SAndreas Gohr 6276c2bb100SAndreas Gohr //prepend groups with @ and nameencode 62837ff2261SSascha Klopp foreach($groups as &$group) { 62937ff2261SSascha Klopp $group = '@'.auth_nameencode($group); 63010a76f6fSfrank } 63110a76f6fSfrank 632f3f0262cSandi $ns = getNS($id); 633f3f0262cSandi $perm = -1; 634f3f0262cSandi 635f3f0262cSandi //add ALL group 636f3f0262cSandi $groups[] = '@ALL'; 63737ff2261SSascha Klopp 638f3f0262cSandi //add User 63934aeb4afSAndreas Gohr if($user) $groups[] = $user; 640f3f0262cSandi 641f3f0262cSandi //check exact match first 64221c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 643f3f0262cSandi if(count($matches)) { 644f3f0262cSandi foreach($matches as $match) { 645f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 64621c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 647eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 648eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 649eb3ce0d5SKazutaka Miyasaka } 65048d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 65148d7b7a6SDominik Eckelmann continue; 65248d7b7a6SDominik Eckelmann } 6538ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 654f3f0262cSandi if($acl[2] > $perm) { 655f3f0262cSandi $perm = $acl[2]; 656f3f0262cSandi } 657f3f0262cSandi } 658f3f0262cSandi if($perm > -1) { 659f3f0262cSandi //we had a match - return it 660def492a2SGuillaume Turri return (int) $perm; 661f3f0262cSandi } 662f3f0262cSandi } 663f3f0262cSandi 664f3f0262cSandi //still here? do the namespace checks 665f3f0262cSandi if($ns) { 6663e304b55SMichael Hamann $path = $ns.':*'; 667f3f0262cSandi } else { 6683e304b55SMichael Hamann $path = '*'; //root document 669f3f0262cSandi } 670f3f0262cSandi 671f3f0262cSandi do { 67221c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 673f3f0262cSandi if(count($matches)) { 674f3f0262cSandi foreach($matches as $match) { 675f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 67621c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 677eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 678eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 679eb3ce0d5SKazutaka Miyasaka } 68048d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 68148d7b7a6SDominik Eckelmann continue; 68248d7b7a6SDominik Eckelmann } 6838ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 684f3f0262cSandi if($acl[2] > $perm) { 685f3f0262cSandi $perm = $acl[2]; 686f3f0262cSandi } 687f3f0262cSandi } 688f3f0262cSandi //we had a match - return it 68948d7b7a6SDominik Eckelmann if($perm != -1) { 690def492a2SGuillaume Turri return (int) $perm; 691f3f0262cSandi } 69248d7b7a6SDominik Eckelmann } 693f3f0262cSandi //get next higher namespace 694f3f0262cSandi $ns = getNS($ns); 695f3f0262cSandi 6963e304b55SMichael Hamann if($path != '*') { 6973e304b55SMichael Hamann $path = $ns.':*'; 6983e304b55SMichael Hamann if($path == ':*') $path = '*'; 699f3f0262cSandi } else { 700f3f0262cSandi //we did this already 701f3f0262cSandi //looks like there is something wrong with the ACL 702f3f0262cSandi //break here 703d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 704d5ce66f6SAndreas Gohr return AUTH_NONE; 705f3f0262cSandi } 706f3f0262cSandi } while(1); //this should never loop endless 707ab5d26daSAndreas Gohr return AUTH_NONE; 708f3f0262cSandi} 709f3f0262cSandi 710f3f0262cSandi/** 7116c2bb100SAndreas Gohr * Encode ASCII special chars 7126c2bb100SAndreas Gohr * 7136c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7146c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7156c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7166c2bb100SAndreas Gohr * urlencoding!). 7176c2bb100SAndreas Gohr * 7186c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7196c2bb100SAndreas Gohr * 7206c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7216c2bb100SAndreas Gohr * @see rawurldecode() 72242ea7f44SGerrit Uitslag * 72342ea7f44SGerrit Uitslag * @param string $name 72442ea7f44SGerrit Uitslag * @param bool $skip_group 72542ea7f44SGerrit Uitslag * @return string 7266c2bb100SAndreas Gohr */ 727e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 728a424cd8eSchris global $cache_authname; 729a424cd8eSchris $cache =& $cache_authname; 73031784267SAndreas Gohr $name = (string) $name; 731a424cd8eSchris 73280601d26SAndreas Gohr // never encode wildcard FS#1955 73380601d26SAndreas Gohr if($name == '%USER%') return $name; 734b78bf706Sromain if($name == '%GROUP%') return $name; 73580601d26SAndreas Gohr 736a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 737e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 73830f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 73930f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 74030f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 741ab5d26daSAndreas Gohr ); 742e838fc2eSAndreas Gohr } else { 74330f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 74430f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 74530f6faf0SChristopher Smith 'auth_nameencode_callback', $name 746ab5d26daSAndreas Gohr ); 747e838fc2eSAndreas Gohr } 7486c2bb100SAndreas Gohr } 7496c2bb100SAndreas Gohr 750a424cd8eSchris return $cache[$name][$skip_group]; 751a424cd8eSchris} 752a424cd8eSchris 75304d68ae4SGerrit Uitslag/** 75404d68ae4SGerrit Uitslag * callback encodes the matches 75504d68ae4SGerrit Uitslag * 75604d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 75704d68ae4SGerrit Uitslag * @return string 75804d68ae4SGerrit Uitslag */ 75930f6faf0SChristopher Smithfunction auth_nameencode_callback($matches) { 76030f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1],-1))); 76130f6faf0SChristopher Smith} 76230f6faf0SChristopher Smith 7636c2bb100SAndreas Gohr/** 764f3f0262cSandi * Create a pronouncable password 765f3f0262cSandi * 7668a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 7678a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 7688a285f7fSAndreas Gohr * 76915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 77015fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 7718a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 77215fae107Sandi * 7738a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 77415fae107Sandi * @return string pronouncable password 775f3f0262cSandi */ 7768a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 7778a285f7fSAndreas Gohr $data = array( 778d628dcf3SAndreas Gohr 'password' => '', 779d628dcf3SAndreas Gohr 'foruser' => $foruser 7808a285f7fSAndreas Gohr ); 7818a285f7fSAndreas Gohr 7828a285f7fSAndreas Gohr $evt = new Doku_Event('AUTH_PASSWORD_GENERATE', $data); 7838a285f7fSAndreas Gohr if($evt->advise_before(true)) { 784f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 785f3f0262cSandi $v = 'aeiou'; //vowels 786f3f0262cSandi $a = $c.$v; //both 787987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 788f3f0262cSandi 789987c8d26SAndreas Gohr //use thre syllables... 790987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 791483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 792483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 793483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 794f3f0262cSandi } 795987c8d26SAndreas Gohr //... and add a nice number and special 796483b6238SMichael Hamann $data['password'] .= auth_random(10, 99).$s[auth_random(0, strlen($s) - 1)]; 7978a285f7fSAndreas Gohr } 7988a285f7fSAndreas Gohr $evt->advise_after(); 799f3f0262cSandi 8008a285f7fSAndreas Gohr return $data['password']; 801f3f0262cSandi} 802f3f0262cSandi 803f3f0262cSandi/** 804f3f0262cSandi * Sends a password to the given user 805f3f0262cSandi * 80615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 80742ea7f44SGerrit Uitslag * 808ab5d26daSAndreas Gohr * @param string $user Login name of the user 809ab5d26daSAndreas Gohr * @param string $password The new password in clear text 81015fae107Sandi * @return bool true on success 811f3f0262cSandi */ 812f3f0262cSandifunction auth_sendPassword($user, $password) { 813f3f0262cSandi global $lang; 81427058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 815cd52f92dSchris global $auth; 816beca106aSAdrian Lang if(!$auth) return false; 817cd52f92dSchris 818d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8192dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 820f3f0262cSandi 82187ddda95Sandi if(!$userinfo['mail']) return false; 822f3f0262cSandi 823f3f0262cSandi $text = rawLocale('password'); 824d7169d19SAndreas Gohr $trep = array( 825d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 826d7169d19SAndreas Gohr 'LOGIN' => $user, 827d7169d19SAndreas Gohr 'PASSWORD' => $password 828d7169d19SAndreas Gohr ); 829f3f0262cSandi 830d7169d19SAndreas Gohr $mail = new Mailer(); 831d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 832d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 833d7169d19SAndreas Gohr $mail->setBody($text, $trep); 834d7169d19SAndreas Gohr return $mail->send(); 835f3f0262cSandi} 836f3f0262cSandi 837f3f0262cSandi/** 83815fae107Sandi * Register a new user 839f3f0262cSandi * 84015fae107Sandi * This registers a new user - Data is read directly from $_POST 84115fae107Sandi * 84215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 84342ea7f44SGerrit Uitslag * 84415fae107Sandi * @return bool true on success, false on any error 845f3f0262cSandi */ 846f3f0262cSandifunction register() { 847f3f0262cSandi global $lang; 848eb5d07e4Sjan global $conf; 84927058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 850cd52f92dSchris global $auth; 85164273335SAndreas Gohr global $INPUT; 852f3f0262cSandi 85364273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 8543a48618aSAnika Henke if(!actionOK('register')) return false; 855640145a5Sandi 85664273335SAndreas Gohr // gather input 85764273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 85864273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 85964273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 86064273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 86164273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 862d752aedeSAndreas Gohr 86364273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 864f3f0262cSandi msg($lang['regmissing'], -1); 865f3f0262cSandi return false; 866f3f0262cSandi } 867f3f0262cSandi 868cab2716aSmatthias.grimm if($conf['autopasswd']) { 8698a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 87064273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 871bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 872cab2716aSmatthias.grimm return false; 87364273335SAndreas Gohr } elseif($pass != $passchk) { 874bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 875cab2716aSmatthias.grimm return false; 876cab2716aSmatthias.grimm } 877cab2716aSmatthias.grimm 878f3f0262cSandi //check mail 87964273335SAndreas Gohr if(!mail_isvalid($email)) { 880f3f0262cSandi msg($lang['regbadmail'], -1); 881f3f0262cSandi return false; 882f3f0262cSandi } 883f3f0262cSandi 884f3f0262cSandi //okay try to create the user 88564273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 886db9faf02SPatrick Brown msg($lang['regfail'], -1); 887f3f0262cSandi return false; 888f3f0262cSandi } 889f3f0262cSandi 890790b7720SAndreas Gohr // send notification about the new user 891*75d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 892*75d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 89302a498e7Schris 894790b7720SAndreas Gohr // are we done? 895cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 896cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 897cab2716aSmatthias.grimm return true; 898cab2716aSmatthias.grimm } 899cab2716aSmatthias.grimm 900790b7720SAndreas Gohr // autogenerated password? then send password to user 90164273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 902f3f0262cSandi msg($lang['regsuccess'], 1); 903f3f0262cSandi return true; 904f3f0262cSandi } else { 905f3f0262cSandi msg($lang['regmailfail'], -1); 906f3f0262cSandi return false; 907f3f0262cSandi } 908f3f0262cSandi} 909f3f0262cSandi 91010a76f6fSfrank/** 9118b06d178Schris * Update user profile 9128b06d178Schris * 9138b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9148b06d178Schris */ 9158b06d178Schrisfunction updateprofile() { 9168b06d178Schris global $conf; 9178b06d178Schris global $lang; 91827058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 919cd52f92dSchris global $auth; 920bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 921bcc94b2cSAndreas Gohr global $INPUT; 9228b06d178Schris 923bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9241b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9258b06d178Schris 9263a48618aSAnika Henke if(!actionOK('profile')) { 9278b06d178Schris msg($lang['profna'], -1); 9288b06d178Schris return false; 9298b06d178Schris } 9308b06d178Schris 931bcc94b2cSAndreas Gohr $changes = array(); 932bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 933bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 934bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 935bcc94b2cSAndreas Gohr 936bcc94b2cSAndreas Gohr // check misspelled passwords 937bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 938bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 9398b06d178Schris return false; 9408b06d178Schris } 9418b06d178Schris 9428b06d178Schris // clean fullname and email 943bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 944bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 9458b06d178Schris 946bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 947bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 948bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 949ab5d26daSAndreas Gohr ) { 9508b06d178Schris msg($lang['profnoempty'], -1); 9518b06d178Schris return false; 9528b06d178Schris } 953bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 9548b06d178Schris msg($lang['regbadmail'], -1); 9558b06d178Schris return false; 9568b06d178Schris } 9578b06d178Schris 958bcc94b2cSAndreas Gohr $changes = array_filter($changes); 9594c21b7eeSAndreas Gohr 960bcc94b2cSAndreas Gohr // check for unavailable capabilities 961bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 962bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 963bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 964bcc94b2cSAndreas Gohr 965bcc94b2cSAndreas Gohr // anything to do? 9668b06d178Schris if(!count($changes)) { 9678b06d178Schris msg($lang['profnochange'], -1); 9688b06d178Schris return false; 9698b06d178Schris } 9708b06d178Schris 9718b06d178Schris if($conf['profileconfirm']) { 972585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 97371422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 9748b06d178Schris return false; 9758b06d178Schris } 9768b06d178Schris } 9778b06d178Schris 978e6c4392fSPatrick Brown if(!$auth->triggerUserMod('modify', array($INPUT->server->str('REMOTE_USER'), &$changes))) { 979db9faf02SPatrick Brown msg($lang['proffail'], -1); 980db9faf02SPatrick Brown return false; 981db9faf02SPatrick Brown } 982db9faf02SPatrick Brown 98332ed2b36SAndreas Gohr if($changes['pass']) { 984c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 985ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 98604369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 987585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 988c276e9e8SMarcel Pennewiss } else { 989c276e9e8SMarcel Pennewiss // make sure the session is writable 990c276e9e8SMarcel Pennewiss @session_start(); 991c276e9e8SMarcel Pennewiss // invalidate session cache 992c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 993c276e9e8SMarcel Pennewiss session_write_close(); 99432ed2b36SAndreas Gohr } 995c276e9e8SMarcel Pennewiss 99625b2a98cSMichael Klier return true; 997a0b5b007SChris Smith} 998ab5d26daSAndreas Gohr 99904d68ae4SGerrit Uitslag/** 100004d68ae4SGerrit Uitslag * Delete the current logged-in user 100104d68ae4SGerrit Uitslag * 100204d68ae4SGerrit Uitslag * @return bool true on success, false on any error 100304d68ae4SGerrit Uitslag */ 10042a7abf2dSChristopher Smithfunction auth_deleteprofile(){ 10052a7abf2dSChristopher Smith global $conf; 10062a7abf2dSChristopher Smith global $lang; 100773012efdSChristopher Smith /* @var DokuWiki_Auth_Plugin $auth */ 10082a7abf2dSChristopher Smith global $auth; 10092a7abf2dSChristopher Smith /* @var Input $INPUT */ 10102a7abf2dSChristopher Smith global $INPUT; 10112a7abf2dSChristopher Smith 10122a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 10132a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 10142a7abf2dSChristopher Smith 10152a7abf2dSChristopher Smith // action prevented or auth module disallows 10162a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10172a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10182a7abf2dSChristopher Smith return false; 10192a7abf2dSChristopher Smith } 10202a7abf2dSChristopher Smith 10212a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 10222a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10232a7abf2dSChristopher Smith return false; 10242a7abf2dSChristopher Smith } 10252a7abf2dSChristopher Smith 10262a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1027585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10282a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10292a7abf2dSChristopher Smith return false; 10302a7abf2dSChristopher Smith } 10312a7abf2dSChristopher Smith } 10322a7abf2dSChristopher Smith 103359bc3b48SGerrit Uitslag $deleted = array(); 1034585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 103573012efdSChristopher Smith if($auth->triggerUserMod('delete', array($deleted))) { 10362a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 10372a7abf2dSChristopher Smith auth_logoff(); 10382a7abf2dSChristopher Smith return true; 10392a7abf2dSChristopher Smith } 10402a7abf2dSChristopher Smith 10412a7abf2dSChristopher Smith return false; 10422a7abf2dSChristopher Smith} 10432a7abf2dSChristopher Smith 10448b06d178Schris/** 10458b06d178Schris * Send a new password 10468b06d178Schris * 10471d5856cfSAndreas Gohr * This function handles both phases of the password reset: 10481d5856cfSAndreas Gohr * 10491d5856cfSAndreas Gohr * - handling the first request of password reset 10501d5856cfSAndreas Gohr * - validating the password reset auth token 10511d5856cfSAndreas Gohr * 10528b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 10538b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 10541d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10558b06d178Schris * 10568b06d178Schris * @return bool true on success, false on any error 10578b06d178Schris */ 10588b06d178Schrisfunction act_resendpwd() { 10598b06d178Schris global $lang; 10608b06d178Schris global $conf; 106127058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1062cd52f92dSchris global $auth; 1063bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1064bcc94b2cSAndreas Gohr global $INPUT; 10658b06d178Schris 10663a48618aSAnika Henke if(!actionOK('resendpwd')) { 10678b06d178Schris msg($lang['resendna'], -1); 10688b06d178Schris return false; 10698b06d178Schris } 10708b06d178Schris 1071bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 10728b06d178Schris 10731d5856cfSAndreas Gohr if($token) { 1074cc204bbdSAndreas Gohr // we're in token phase - get user info from token 10751d5856cfSAndreas Gohr 10761d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 107779e79377SAndreas Gohr if(!file_exists($tfile)) { 10781d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1079bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 10801d5856cfSAndreas Gohr return false; 10811d5856cfSAndreas Gohr } 10828a9735e3SAndreas Gohr // token is only valid for 3 days 10838a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 10848a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1085bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 10861d5856cfSAndreas Gohr @unlink($tfile); 10878a9735e3SAndreas Gohr return false; 10888a9735e3SAndreas Gohr } 10898a9735e3SAndreas Gohr 10908b06d178Schris $user = io_readfile($tfile); 10912dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 10928b06d178Schris if(!$userinfo['mail']) { 10938b06d178Schris msg($lang['resendpwdnouser'], -1); 10948b06d178Schris return false; 10958b06d178Schris } 10968b06d178Schris 1097cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1098bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1099bcc94b2cSAndreas Gohr 1100cc204bbdSAndreas Gohr // password given correctly? 1101bcc94b2cSAndreas Gohr if(!$pass) return false; 1102bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1103451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1104cc204bbdSAndreas Gohr return false; 1105cc204bbdSAndreas Gohr } 1106cc204bbdSAndreas Gohr 1107bcc94b2cSAndreas Gohr // change it 1108cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1109db9faf02SPatrick Brown msg($lang['proffail'], -1); 1110cc204bbdSAndreas Gohr return false; 1111cc204bbdSAndreas Gohr } 1112cc204bbdSAndreas Gohr 1113cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1114cc204bbdSAndreas Gohr 11158a285f7fSAndreas Gohr $pass = auth_pwgen($user); 11167d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1117db9faf02SPatrick Brown msg($lang['proffail'], -1); 11188b06d178Schris return false; 11198b06d178Schris } 11208b06d178Schris 11218b06d178Schris if(auth_sendPassword($user, $pass)) { 11228b06d178Schris msg($lang['resendpwdsuccess'], 1); 11238b06d178Schris } else { 11248b06d178Schris msg($lang['regmailfail'], -1); 11258b06d178Schris } 1126cc204bbdSAndreas Gohr } 1127cc204bbdSAndreas Gohr 1128cc204bbdSAndreas Gohr @unlink($tfile); 11298b06d178Schris return true; 11301d5856cfSAndreas Gohr 11311d5856cfSAndreas Gohr } else { 11321d5856cfSAndreas Gohr // we're in request phase 11331d5856cfSAndreas Gohr 1134bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 11351d5856cfSAndreas Gohr 1136bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 11371d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 11381d5856cfSAndreas Gohr return false; 11391d5856cfSAndreas Gohr } else { 1140bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 11411d5856cfSAndreas Gohr } 11421d5856cfSAndreas Gohr 11432dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11441d5856cfSAndreas Gohr if(!$userinfo['mail']) { 11451d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 11461d5856cfSAndreas Gohr return false; 11471d5856cfSAndreas Gohr } 11481d5856cfSAndreas Gohr 11491d5856cfSAndreas Gohr // generate auth token 1150483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 11511d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 11521d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 11531d5856cfSAndreas Gohr 11541d5856cfSAndreas Gohr io_saveFile($tfile, $user); 11551d5856cfSAndreas Gohr 11561d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1157d7169d19SAndreas Gohr $trep = array( 1158d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1159d7169d19SAndreas Gohr 'LOGIN' => $user, 1160d7169d19SAndreas Gohr 'CONFIRM' => $url 1161d7169d19SAndreas Gohr ); 11621d5856cfSAndreas Gohr 1163d7169d19SAndreas Gohr $mail = new Mailer(); 1164d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1165d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1166d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1167d7169d19SAndreas Gohr if($mail->send()) { 11681d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 11691d5856cfSAndreas Gohr } else { 11701d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 11711d5856cfSAndreas Gohr } 11721d5856cfSAndreas Gohr return true; 11731d5856cfSAndreas Gohr } 1174ab5d26daSAndreas Gohr // never reached 11758b06d178Schris} 11768b06d178Schris 11778b06d178Schris/** 1178b0855b11Sandi * Encrypts a password using the given method and salt 1179b0855b11Sandi * 1180b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1181b0855b11Sandi * is chosen. 1182b0855b11Sandi * 1183b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 118442ea7f44SGerrit Uitslag * 1185ab5d26daSAndreas Gohr * @param string $clear The clear text password 1186ab5d26daSAndreas Gohr * @param string $method The hashing method 1187ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1188b0855b11Sandi * @return string The crypted password 1189b0855b11Sandi */ 1190577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1191b0855b11Sandi global $conf; 1192b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 119310a76f6fSfrank 11943a0a2d05SAndreas Gohr $pass = new PassHash(); 11953a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1196b0855b11Sandi 11973a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1198b0855b11Sandi msg("Unsupported crypt method $method", -1); 11993a0a2d05SAndreas Gohr return false; 1200b0855b11Sandi } 12013a0a2d05SAndreas Gohr 12023a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1203b0855b11Sandi} 1204b0855b11Sandi 1205b0855b11Sandi/** 1206b0855b11Sandi * Verifies a cleartext password against a crypted hash 1207b0855b11Sandi * 1208b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 120942ea7f44SGerrit Uitslag * 1210ab5d26daSAndreas Gohr * @param string $clear The clear text password 1211ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1212ab5d26daSAndreas Gohr * @return bool true if both match 1213b0855b11Sandi */ 1214b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 12153a0a2d05SAndreas Gohr $pass = new PassHash(); 12163a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1217b0855b11Sandi} 1218340756e4Sandi 1219a0b5b007SChris Smith/** 1220a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1221a0b5b007SChris Smith * 1222a0b5b007SChris Smith * @param string $user username 1223a0b5b007SChris Smith * @param string $pass encrypted password 1224a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1225ab5d26daSAndreas Gohr * @return bool 1226a0b5b007SChris Smith */ 1227a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1228a0b5b007SChris Smith global $conf; 122927058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1230a0b5b007SChris Smith global $auth; 123179d00841SOliver Geisen global $USERINFO; 1232a0b5b007SChris Smith 1233beca106aSAdrian Lang if(!$auth) return false; 1234a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1235a0b5b007SChris Smith 1236a0b5b007SChris Smith // set cookie 1237645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 123873ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1239c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 124073ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 124155a71a16SGerrit Uitslag 1242a0b5b007SChris Smith // set session 1243a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1244234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1245a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1246a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1247a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1248ab5d26daSAndreas Gohr 1249ab5d26daSAndreas Gohr return true; 1250a0b5b007SChris Smith} 1251a0b5b007SChris Smith 1252645c0a36SAndreas Gohr/** 1253645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1254645c0a36SAndreas Gohr * 1255645c0a36SAndreas Gohr * @returns array 1256645c0a36SAndreas Gohr */ 1257645c0a36SAndreas Gohrfunction auth_getCookie() { 1258c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1259c66972f2SAdrian Lang return array(null, null, null); 1260c66972f2SAdrian Lang } 1261645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1262645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1263645c0a36SAndreas Gohr $pass = base64_decode($pass); 1264645c0a36SAndreas Gohr $user = base64_decode($user); 1265645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1266645c0a36SAndreas Gohr} 1267645c0a36SAndreas Gohr 1268e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1269