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 12fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 131c73890cSAndreas Gohr 14ebf97c8fSAndreas Gohr// some ACL level defines 15ebf97c8fSAndreas Gohrdefine('AUTH_NONE', 0); 16ebf97c8fSAndreas Gohrdefine('AUTH_READ', 1); 17ebf97c8fSAndreas Gohrdefine('AUTH_EDIT', 2); 18ebf97c8fSAndreas Gohrdefine('AUTH_CREATE', 4); 19ebf97c8fSAndreas Gohrdefine('AUTH_UPLOAD', 8); 20ebf97c8fSAndreas Gohrdefine('AUTH_DELETE', 16); 21ebf97c8fSAndreas Gohrdefine('AUTH_ADMIN', 255); 22ebf97c8fSAndreas Gohr 2316905344SAndreas Gohr/** 2416905344SAndreas Gohr * Initialize the auth system. 2516905344SAndreas Gohr * 2616905344SAndreas Gohr * This function is automatically called at the end of init.php 2716905344SAndreas Gohr * 2816905344SAndreas Gohr * This used to be the main() of the auth.php 2916905344SAndreas Gohr * 3016905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 3116905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 32ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 33ab5d26daSAndreas Gohr * @return bool 3416905344SAndreas Gohr */ 3516905344SAndreas Gohrfunction auth_setup() { 36742c66f8Schris global $conf; 3793a7873eSAndreas Gohr /* @var DokuWiki_Auth_Plugin $auth */ 3803c4aec3Schris global $auth; 39bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 40bcc94b2cSAndreas Gohr global $INPUT; 419a9714acSDominik Eckelmann global $AUTH_ACL; 429a9714acSDominik Eckelmann global $lang; 4327058a05SMichael Hamann /* @var Doku_Plugin_Controller $plugin_controller */ 449c29eea5SJan Schumann global $plugin_controller; 459a9714acSDominik Eckelmann $AUTH_ACL = array(); 4603c4aec3Schris 4716905344SAndreas Gohr if(!$conf['useacl']) return false; 4816905344SAndreas Gohr 499c29eea5SJan Schumann // try to load auth backend from plugins 509c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 519c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 52f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 539c29eea5SJan Schumann break; 54e71b0ef7SGuy Brand } elseif ('auth' . $conf['authtype'] === $plugin) { 55e71b0ef7SGuy Brand // matches old auth backends (pre-Weatherwax) 56e71b0ef7SGuy Brand $auth = $plugin_controller->load('auth', $plugin); 57a91f1103SAnika Henke msg('Your authtype setting is deprecated. You must set $conf[\'authtype\'] = "auth' . $conf['authtype'] . '"' 5898e31f85SKlap-in . ' in your configuration (see <a href="https://www.dokuwiki.org/auth">Authentication Backends</a>)',-1,'','',MSG_ADMINS_ONLY); 599c29eea5SJan Schumann } 609c29eea5SJan Schumann } 618b06d178Schris 626416b708SMichael Hamann if(!isset($auth) || !$auth){ 633094e817SAndreas Gohr msg($lang['authtempfail'], -1); 643094e817SAndreas Gohr return false; 653094e817SAndreas Gohr } 668b06d178Schris 676416b708SMichael Hamann if ($auth->success == false) { 680f4f4adfSAndreas Gohr // degrade to unauthenticated user 69d2dde4ebSMatthias Grimm unset($auth); 700f4f4adfSAndreas Gohr auth_logoff(); 71cd52f92dSchris msg($lang['authtempfail'], -1); 726416b708SMichael Hamann return false; 73d2dde4ebSMatthias Grimm } 7416905344SAndreas Gohr 7516905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 76bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 77bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 78bbbd6568SAndreas Gohr 79b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 80b2665af7SMichael Hamann // the one presented at 81b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 82b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 83b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 84b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 85528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 8606156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 87528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 88528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 89528ddc7cSAndreas Gohr } 90528ddc7cSAndreas Gohr 911e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 92bcc94b2cSAndreas Gohr if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 93bcc94b2cSAndreas Gohr $INPUT->set('u', $_SERVER['PHP_AUTH_USER']); 94bcc94b2cSAndreas Gohr $INPUT->set('p', $_SERVER['PHP_AUTH_PW']); 95bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 961e8c9c90SAndreas Gohr } 971e8c9c90SAndreas Gohr 98191bb90aSAndreas Gohr // apply cleaning 9993a7873eSAndreas Gohr if (true === $auth->success) { 10000d58927SMichael Hamann $INPUT->set('u', $auth->cleanUser($INPUT->str('u'))); 101f4476bd9SJan Schumann } 102191bb90aSAndreas Gohr 103bcc94b2cSAndreas Gohr if($INPUT->str('authtok')) { 104f13fa892SAndreas Gohr // when an authentication token is given, trust the session 105bcc94b2cSAndreas Gohr auth_validateToken($INPUT->str('authtok')); 106f13fa892SAndreas Gohr } elseif(!is_null($auth) && $auth->canDo('external')) { 107f13fa892SAndreas Gohr // external trust mechanism in place 108bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 109f5cb575dSAndreas Gohr } else { 1106080c584SRobin Gareus $evdata = array( 111bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 112bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 113bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 114bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1156080c584SRobin Gareus ); 116b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 117f5cb575dSAndreas Gohr } 118f5cb575dSAndreas Gohr 11916905344SAndreas Gohr //load ACL into a global array XXX 12075c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 121ab5d26daSAndreas Gohr 122ab5d26daSAndreas Gohr return true; 12375c93b77SAndreas Gohr} 12475c93b77SAndreas Gohr 12575c93b77SAndreas Gohr/** 12675c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12775c93b77SAndreas Gohr * 12875c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 129ab5d26daSAndreas Gohr * @return array 13075c93b77SAndreas Gohr */ 13175c93b77SAndreas Gohrfunction auth_loadACL() { 13275c93b77SAndreas Gohr global $config_cascade; 133b78bf706Sromain global $USERINFO; 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 139191bb90aSAndreas Gohr //support user wildcard 14032e82180SAndreas Gohr $out = array(); 1419ce556d2SAndreas Gohr foreach($acl as $line) { 1429ce556d2SAndreas Gohr $line = trim($line); 1439ce556d2SAndreas Gohr if($line{0} == '#') continue; 1449ce556d2SAndreas Gohr list($id,$rest) = preg_split('/\s+/',$line,2); 14532e82180SAndreas Gohr 1469ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 1479ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 148b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 14932e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 15032e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 151b78bf706Sromain } 15232e82180SAndreas Gohr } else { 15375c93b77SAndreas Gohr $id = str_replace('%USER%',cleanID($_SERVER['REMOTE_USER']),$id); 15475c93b77SAndreas Gohr $rest = str_replace('%USER%',auth_nameencode($_SERVER['REMOTE_USER']),$rest); 15532e82180SAndreas Gohr $out[] = "$id\t$rest"; 156a8fe108bSGuy Brand } 15711799630Sandi } 1589ce556d2SAndreas Gohr 15932e82180SAndreas Gohr return $out; 160f3f0262cSandi} 161f3f0262cSandi 162ab5d26daSAndreas Gohr/** 163ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 164ab5d26daSAndreas Gohr * 165ab5d26daSAndreas Gohr * @param $evdata 166ab5d26daSAndreas Gohr * @return bool 167ab5d26daSAndreas Gohr */ 168b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 169ab5d26daSAndreas Gohr return auth_login( 170ab5d26daSAndreas Gohr $evdata['user'], 171b5ee21aaSAdrian Lang $evdata['password'], 172b5ee21aaSAdrian Lang $evdata['sticky'], 173ab5d26daSAndreas Gohr $evdata['silent'] 174ab5d26daSAndreas Gohr ); 175b5ee21aaSAdrian Lang} 176b5ee21aaSAdrian Lang 177f3f0262cSandi/** 178f3f0262cSandi * This tries to login the user based on the sent auth credentials 179f3f0262cSandi * 180f3f0262cSandi * The authentication works like this: if a username was given 18115fae107Sandi * a new login is assumed and user/password are checked. If they 18215fae107Sandi * are correct the password is encrypted with blowfish and stored 18315fae107Sandi * together with the username in a cookie - the same info is stored 18415fae107Sandi * in the session, too. Additonally a browserID is stored in the 18515fae107Sandi * session. 18615fae107Sandi * 18715fae107Sandi * If no username was given the cookie is checked: if the username, 18815fae107Sandi * crypted password and browserID match between session and cookie 18915fae107Sandi * no further testing is done and the user is accepted 19015fae107Sandi * 19115fae107Sandi * If a cookie was found but no session info was availabe the 192136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 19315fae107Sandi * together with username rechecked by calling this function again. 194f3f0262cSandi * 195f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 196f3f0262cSandi * are set. 19715fae107Sandi * 19815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 19915fae107Sandi * 20015fae107Sandi * @param string $user Username 20115fae107Sandi * @param string $pass Cleartext Password 20215fae107Sandi * @param bool $sticky Cookie should not expire 203f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 20415fae107Sandi * @return bool true on successful auth 205f3f0262cSandi */ 206f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 207f3f0262cSandi global $USERINFO; 208f3f0262cSandi global $conf; 209f3f0262cSandi global $lang; 21027058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 211cd52f92dSchris global $auth; 212ab5d26daSAndreas Gohr 213132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 214f3f0262cSandi 215beca106aSAdrian Lang if(!$auth) return false; 216beca106aSAdrian Lang 217bbbd6568SAndreas Gohr if(!empty($user)) { 218132bdbfeSandi //usual login 219cd52f92dSchris if($auth->checkPass($user, $pass)) { 220132bdbfeSandi // make logininfo globally available 221f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 22230d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 22304369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 224132bdbfeSandi return true; 225f3f0262cSandi } else { 226f3f0262cSandi //invalid credentials - log off 227f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 228f3f0262cSandi auth_logoff(); 229132bdbfeSandi return false; 230f3f0262cSandi } 231f3f0262cSandi } else { 232132bdbfeSandi // read cookie information 233645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 234132bdbfeSandi if($user && $pass) { 235132bdbfeSandi // we got a cookie - see if we can trust it 236fa7c70ffSAdrian Lang 237fa7c70ffSAdrian Lang // get session info 238fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 239132bdbfeSandi if(isset($session) && 2407172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2414c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 242132bdbfeSandi ($session['user'] == $user) && 243234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 244ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 245ab5d26daSAndreas Gohr ) { 246234ce57eSAndreas Gohr 247132bdbfeSandi // he has session, cookie and browser right - let him in 248132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 249132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 250132bdbfeSandi return true; 251132bdbfeSandi } 252f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 25330d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 25404369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 255f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 256132bdbfeSandi } 257132bdbfeSandi } 258f3f0262cSandi //just to be sure 259883179a4SAndreas Gohr auth_logoff(true); 260132bdbfeSandi return false; 261f3f0262cSandi} 262132bdbfeSandi 263132bdbfeSandi/** 264f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 265f13fa892SAndreas Gohr * 266f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 267f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 268f13fa892SAndreas Gohr * 269f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 270f13fa892SAndreas Gohr * @param string $token The authentication token 271f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 272f13fa892SAndreas Gohr */ 273f13fa892SAndreas Gohrfunction auth_validateToken($token) { 274f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']) { 275f13fa892SAndreas Gohr // bad token 2769d2e1be6SAndreas Gohr http_status(401); 277f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 278f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 279f13fa892SAndreas Gohr exit; 280f13fa892SAndreas Gohr } 281f13fa892SAndreas Gohr // still here? trust the session data 282f13fa892SAndreas Gohr global $USERINFO; 283f13fa892SAndreas Gohr $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user']; 284f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 285f13fa892SAndreas Gohr return true; 286f13fa892SAndreas Gohr} 287f13fa892SAndreas Gohr 288f13fa892SAndreas Gohr/** 289f13fa892SAndreas Gohr * Create an auth token and store it in the session 290f13fa892SAndreas Gohr * 291f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 292f13fa892SAndreas Gohr * 293f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 294f13fa892SAndreas Gohr * @return string The auth token 295f13fa892SAndreas Gohr */ 296f13fa892SAndreas Gohrfunction auth_createToken() { 297483b6238SMichael Hamann $token = md5(auth_randombytes(16)); 29809c2d803SAndreas Gohr @session_start(); // reopen the session if needed 299f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 30009c2d803SAndreas Gohr session_write_close(); 301f13fa892SAndreas Gohr return $token; 302f13fa892SAndreas Gohr} 303f13fa892SAndreas Gohr 304f13fa892SAndreas Gohr/** 305136ce040Sandi * Builds a pseudo UID from browser and IP data 306132bdbfeSandi * 307132bdbfeSandi * This is neither unique nor unfakable - still it adds some 308136ce040Sandi * security. Using the first part of the IP makes sure 30980b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 31015fae107Sandi * 31115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 31215fae107Sandi * 31315fae107Sandi * @return string a MD5 sum of various browser headers 314132bdbfeSandi */ 315132bdbfeSandifunction auth_browseruid() { 3162f9daf16SAndreas Gohr $ip = clientIP(true); 317132bdbfeSandi $uid = ''; 318132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 319132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 320132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 3212f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 32280b4f376SAndreas Gohr $uid = strtolower($uid); 323132bdbfeSandi return md5($uid); 324132bdbfeSandi} 325132bdbfeSandi 326132bdbfeSandi/** 327132bdbfeSandi * Creates a random key to encrypt the password in cookies 32815fae107Sandi * 32915fae107Sandi * This function tries to read the password for encrypting 33098407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 33115fae107Sandi * if no such file is found a random key is created and 33215fae107Sandi * and stored in this file. 33315fae107Sandi * 33415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 33532ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 33630d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 33715fae107Sandi * @return string 338132bdbfeSandi */ 33930d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 340132bdbfeSandi global $conf; 34198407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 34230d544a4SMichael Hamann if ($secure || !file_exists($file)) { 34330d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 34430d544a4SMichael Hamann } 345132bdbfeSandi $salt = io_readFile($file); 346132bdbfeSandi if(empty($salt)) { 34730d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 348132bdbfeSandi io_saveFile($file, $salt); 349132bdbfeSandi } 35032ed2b36SAndreas Gohr if($addsession) { 35132ed2b36SAndreas Gohr $salt .= session_id(); 35232ed2b36SAndreas Gohr } 353132bdbfeSandi return $salt; 354f3f0262cSandi} 355f3f0262cSandi 356f3f0262cSandi/** 357483b6238SMichael Hamann * Return truly (pseudo) random bytes if available, otherwise fall back to mt_rand 358483b6238SMichael Hamann * 359483b6238SMichael Hamann * @author Mark Seecof 360483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 361483b6238SMichael Hamann * @link http://www.php.net/manual/de/function.mt-rand.php#83655 362483b6238SMichael Hamann * @param int $length number of bytes to get 363483b6238SMichael Hamann * @return string binary random strings 364483b6238SMichael Hamann */ 365483b6238SMichael Hamannfunction auth_randombytes($length) { 366483b6238SMichael Hamann $strong = false; 367483b6238SMichael Hamann $rbytes = false; 368483b6238SMichael Hamann 369483b6238SMichael Hamann if (function_exists('openssl_random_pseudo_bytes') 370483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.4') >= 0 371483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 372483b6238SMichael Hamann ) { 373483b6238SMichael Hamann $rbytes = openssl_random_pseudo_bytes($length, $strong); 374483b6238SMichael Hamann } 375483b6238SMichael Hamann 376483b6238SMichael Hamann if (!$strong && function_exists('mcrypt_create_iv') 377483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.7') >= 0 378483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 379483b6238SMichael Hamann ) { 380483b6238SMichael Hamann $rbytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); 381483b6238SMichael Hamann if ($rbytes !== false && strlen($rbytes) === $length) { 382483b6238SMichael Hamann $strong = true; 383483b6238SMichael Hamann } 384483b6238SMichael Hamann } 385483b6238SMichael Hamann 386483b6238SMichael Hamann 387483b6238SMichael Hamann // If no strong randoms available, try OS the specific ways 388483b6238SMichael Hamann if(!$strong) { 389483b6238SMichael Hamann // Unix/Linux platform 390483b6238SMichael Hamann $fp = @fopen('/dev/urandom', 'rb'); 391483b6238SMichael Hamann if($fp !== false) { 392483b6238SMichael Hamann $rbytes = fread($fp, $length); 393483b6238SMichael Hamann fclose($fp); 394483b6238SMichael Hamann } 395483b6238SMichael Hamann 396483b6238SMichael Hamann // MS-Windows platform 397483b6238SMichael Hamann if(class_exists('COM')) { 398483b6238SMichael Hamann // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx 399483b6238SMichael Hamann try { 400483b6238SMichael Hamann $CAPI_Util = new COM('CAPICOM.Utilities.1'); 401483b6238SMichael Hamann $rbytes = $CAPI_Util->GetRandom($length, 0); 402483b6238SMichael Hamann 403483b6238SMichael Hamann // if we ask for binary data PHP munges it, so we 404483b6238SMichael Hamann // request base64 return value. 405483b6238SMichael Hamann if($rbytes) $rbytes = base64_decode($rbytes); 406483b6238SMichael Hamann } catch(Exception $ex) { 407483b6238SMichael Hamann // fail 408483b6238SMichael Hamann } 409483b6238SMichael Hamann } 410483b6238SMichael Hamann } 411483b6238SMichael Hamann if(strlen($rbytes) < $length) $rbytes = false; 412483b6238SMichael Hamann 413483b6238SMichael Hamann // still no random bytes available - fall back to mt_rand() 414483b6238SMichael Hamann if($rbytes === false) { 415483b6238SMichael Hamann $rbytes = ''; 416483b6238SMichael Hamann for ($i = 0; $i < $length; ++$i) { 417483b6238SMichael Hamann $rbytes .= chr(mt_rand(0, 255)); 418483b6238SMichael Hamann } 419483b6238SMichael Hamann } 420483b6238SMichael Hamann 421483b6238SMichael Hamann return $rbytes; 422483b6238SMichael Hamann} 423483b6238SMichael Hamann 424483b6238SMichael Hamann/** 425483b6238SMichael Hamann * Random number generator using the best available source 426483b6238SMichael Hamann * 427483b6238SMichael Hamann * @author Michael Samuel 428483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 429483b6238SMichael Hamann * @param int $min 430483b6238SMichael Hamann * @param int $max 431483b6238SMichael Hamann * @return int 432483b6238SMichael Hamann */ 433483b6238SMichael Hamannfunction auth_random($min, $max) { 434483b6238SMichael Hamann $abs_max = $max - $min; 435483b6238SMichael Hamann 436483b6238SMichael Hamann $nbits = 0; 437483b6238SMichael Hamann for ($n = $abs_max; $n > 0; $n >>= 1) { 438483b6238SMichael Hamann ++$nbits; 439483b6238SMichael Hamann } 440483b6238SMichael Hamann 441483b6238SMichael Hamann $mask = (1 << $nbits) - 1; 442483b6238SMichael Hamann do { 443483b6238SMichael Hamann $bytes = auth_randombytes(PHP_INT_SIZE); 444483b6238SMichael Hamann $integers = unpack('Inum', $bytes); 445483b6238SMichael Hamann $integer = $integers["num"] & $mask; 446483b6238SMichael Hamann } while ($integer > $abs_max); 447483b6238SMichael Hamann 448483b6238SMichael Hamann return $min + $integer; 449483b6238SMichael Hamann} 450483b6238SMichael Hamann 451483b6238SMichael Hamann/** 45204369c3eSMichael Hamann * Encrypt data using the given secret using AES 45304369c3eSMichael Hamann * 45404369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 45504369c3eSMichael Hamann * using pbkdf2. 45604369c3eSMichael Hamann * 45704369c3eSMichael Hamann * @param string $data The data that shall be encrypted 45804369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 45904369c3eSMichael Hamann * @return string The ciphertext 46004369c3eSMichael Hamann */ 46104369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 46204369c3eSMichael Hamann $iv = auth_randombytes(16); 46304369c3eSMichael Hamann $cipher = new Crypt_AES(); 46404369c3eSMichael Hamann $cipher->setPassword($secret); 46504369c3eSMichael Hamann 466*7b650cefSMichael Hamann /* 467*7b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 468*7b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 469*7b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 470*7b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 471*7b650cefSMichael Hamann */ 47204369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 47304369c3eSMichael Hamann} 47404369c3eSMichael Hamann 47504369c3eSMichael Hamann/** 47604369c3eSMichael Hamann * Decrypt the given AES ciphertext 47704369c3eSMichael Hamann * 47804369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 47904369c3eSMichael Hamann * 48004369c3eSMichael Hamann * @param string $ciphertext The encrypted data 48104369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 48204369c3eSMichael Hamann * @return string The decrypted data 48304369c3eSMichael Hamann */ 48404369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 485*7b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 48604369c3eSMichael Hamann $cipher = new Crypt_AES(); 48704369c3eSMichael Hamann $cipher->setPassword($secret); 488*7b650cefSMichael Hamann $cipher->setIV($iv); 48904369c3eSMichael Hamann 490*7b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 49104369c3eSMichael Hamann} 49204369c3eSMichael Hamann 49304369c3eSMichael Hamann/** 494883179a4SAndreas Gohr * Log out the current user 495883179a4SAndreas Gohr * 496f3f0262cSandi * This clears all authentication data and thus log the user 497883179a4SAndreas Gohr * off. It also clears session data. 49815fae107Sandi * 49915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 500883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 501f3f0262cSandi */ 502883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 503f3f0262cSandi global $conf; 504f3f0262cSandi global $USERINFO; 50527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 5065298a619SAndreas Gohr global $auth; 50737065e65Sandi 508d4869846SAndreas Gohr // make sure the session is writable (it usually is) 509e9621d07SAndreas Gohr @session_start(); 510e9621d07SAndreas Gohr 511e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 512e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 513e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 514e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 515e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 516e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 517883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 518e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 51937065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 520f3f0262cSandi unset($_SERVER['REMOTE_USER']); 521132bdbfeSandi $USERINFO = null; //FIXME 522f5c6743cSAndreas Gohr 52373ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 524f5c6743cSAndreas Gohr if(version_compare(PHP_VERSION, '5.2.0', '>')) { 52573ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 526f5c6743cSAndreas Gohr } else { 52773ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 528f5c6743cSAndreas Gohr } 5295298a619SAndreas Gohr 530880f62faSAndreas Gohr if($auth) $auth->logOff(); 531f3f0262cSandi} 532f3f0262cSandi 533f3f0262cSandi/** 534f8cc712eSAndreas Gohr * Check if a user is a manager 535f8cc712eSAndreas Gohr * 536f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 537f8cc712eSAndreas Gohr * user. 538f8cc712eSAndreas Gohr * 539f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 540f8cc712eSAndreas Gohr * 541f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 542f8cc712eSAndreas Gohr * @see auth_isadmin 543ab5d26daSAndreas Gohr * @param string $user Username 544ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 545ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 546ab5d26daSAndreas Gohr * @return bool 547f8cc712eSAndreas Gohr */ 548f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 549f8cc712eSAndreas Gohr global $conf; 550f8cc712eSAndreas Gohr global $USERINFO; 55127058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 552d752aedeSAndreas Gohr global $auth; 553f8cc712eSAndreas Gohr 554beca106aSAdrian Lang if(!$auth) return false; 555c66972f2SAdrian Lang if(is_null($user)) { 556c66972f2SAdrian Lang if(!isset($_SERVER['REMOTE_USER'])) { 557c66972f2SAdrian Lang return false; 558c66972f2SAdrian Lang } else { 559c66972f2SAdrian Lang $user = $_SERVER['REMOTE_USER']; 560c66972f2SAdrian Lang } 561c66972f2SAdrian Lang } 562d6dc956fSAndreas Gohr if(is_null($groups)) { 563d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 564e259aa79SAndreas Gohr } 565e259aa79SAndreas Gohr 566d6dc956fSAndreas Gohr // check superuser match 567d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 568d6dc956fSAndreas Gohr if($adminonly) return false; 569e259aa79SAndreas Gohr // check managers 570d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 57100ce12daSChris Smith 572f8cc712eSAndreas Gohr return false; 573f8cc712eSAndreas Gohr} 574f8cc712eSAndreas Gohr 575f8cc712eSAndreas Gohr/** 576f8cc712eSAndreas Gohr * Check if a user is admin 577f8cc712eSAndreas Gohr * 578f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 579f8cc712eSAndreas Gohr * 580f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 581f8cc712eSAndreas Gohr * 582f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 583ab5d26daSAndreas Gohr * @see auth_ismanager() 584ab5d26daSAndreas Gohr * @param string $user Username 585ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 586ab5d26daSAndreas Gohr * @return bool 587f8cc712eSAndreas Gohr */ 588f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 589f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 590f8cc712eSAndreas Gohr} 591f8cc712eSAndreas Gohr 592d6dc956fSAndreas Gohr/** 593d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 594d6dc956fSAndreas Gohr * users and groups to determine membership status 595d6dc956fSAndreas Gohr * 596d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 597d6dc956fSAndreas Gohr * 598d6dc956fSAndreas Gohr * @param $memberlist string commaseparated list of allowed users and groups 599d6dc956fSAndreas Gohr * @param $user string user to match against 600d6dc956fSAndreas Gohr * @param $groups array groups the user is member of 6015446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 602d6dc956fSAndreas Gohr */ 603d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 60427058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 605d6dc956fSAndreas Gohr global $auth; 606d6dc956fSAndreas Gohr if(!$auth) return false; 607d6dc956fSAndreas Gohr 608d6dc956fSAndreas Gohr // clean user and groups 6094f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 610d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 611d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 612d6dc956fSAndreas Gohr } 613d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 614d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 615d6dc956fSAndreas Gohr 616d6dc956fSAndreas Gohr // extract the memberlist 617d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 618d6dc956fSAndreas Gohr $members = array_map('trim', $members); 619d6dc956fSAndreas Gohr $members = array_unique($members); 620d6dc956fSAndreas Gohr $members = array_filter($members); 621d6dc956fSAndreas Gohr 622d6dc956fSAndreas Gohr // compare cleaned values 623d6dc956fSAndreas Gohr foreach($members as $member) { 6244f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 625d6dc956fSAndreas Gohr if($member[0] == '@') { 626d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 627d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 628d6dc956fSAndreas Gohr } else { 629d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 630d6dc956fSAndreas Gohr if($member == $user) return true; 631d6dc956fSAndreas Gohr } 632d6dc956fSAndreas Gohr } 633d6dc956fSAndreas Gohr 634d6dc956fSAndreas Gohr // still here? not a member! 635d6dc956fSAndreas Gohr return false; 636d6dc956fSAndreas Gohr} 637d6dc956fSAndreas Gohr 638f8cc712eSAndreas Gohr/** 63915fae107Sandi * Convinience function for auth_aclcheck() 64015fae107Sandi * 64115fae107Sandi * This checks the permissions for the current user 64215fae107Sandi * 64315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 64415fae107Sandi * 6451698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 64615fae107Sandi * @return int permission level 647f3f0262cSandi */ 648f3f0262cSandifunction auth_quickaclcheck($id) { 649f3f0262cSandi global $conf; 650f3f0262cSandi global $USERINFO; 651f3f0262cSandi # if no ACL is used always return upload rights 652f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 653f3f0262cSandi return auth_aclcheck($id, $_SERVER['REMOTE_USER'], $USERINFO['grps']); 654f3f0262cSandi} 655f3f0262cSandi 656f3f0262cSandi/** 657f3f0262cSandi * Returns the maximum rights a user has for 658f3f0262cSandi * the given ID or its namespace 65915fae107Sandi * 66015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 66115fae107Sandi * 6621698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 66315fae107Sandi * @param string $user Username 6643272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 66515fae107Sandi * @return int permission level 666f3f0262cSandi */ 667f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 668f3f0262cSandi global $conf; 669f3f0262cSandi global $AUTH_ACL; 67027058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 671d752aedeSAndreas Gohr global $auth; 672f3f0262cSandi 67385d03f68SAndreas Gohr // if no ACL is used always return upload rights 674f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 675beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 676f3f0262cSandi 677074cf26bSandi //make sure groups is an array 678074cf26bSandi if(!is_array($groups)) $groups = array(); 679074cf26bSandi 68085d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 681ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 682ab5d26daSAndreas Gohr return AUTH_ADMIN; 683ab5d26daSAndreas Gohr } 68485d03f68SAndreas Gohr 685eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 686eb3ce0d5SKazutaka Miyasaka $user = utf8_strtolower($user); 687eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 688eb3ce0d5SKazutaka Miyasaka } 689d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 690d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 69185d03f68SAndreas Gohr $user = auth_nameencode($user); 69285d03f68SAndreas Gohr 6936c2bb100SAndreas Gohr //prepend groups with @ and nameencode 6942cd2db38Sandi $cnt = count($groups); 6952cd2db38Sandi for($i = 0; $i < $cnt; $i++) { 6966c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 69710a76f6fSfrank } 69810a76f6fSfrank 699f3f0262cSandi $ns = getNS($id); 700f3f0262cSandi $perm = -1; 701f3f0262cSandi 70234aeb4afSAndreas Gohr if($user || count($groups)) { 703f3f0262cSandi //add ALL group 704f3f0262cSandi $groups[] = '@ALL'; 705f3f0262cSandi //add User 70634aeb4afSAndreas Gohr if($user) $groups[] = $user; 707f3f0262cSandi } else { 70848d7b7a6SDominik Eckelmann $groups[] = '@ALL'; 709f3f0262cSandi } 710f3f0262cSandi 711f3f0262cSandi //check exact match first 712eb3ce0d5SKazutaka Miyasaka $matches = preg_grep('/^'.preg_quote($id, '/').'\s+(\S+)\s+/u', $AUTH_ACL); 713f3f0262cSandi if(count($matches)) { 714f3f0262cSandi foreach($matches as $match) { 715f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 716f3f0262cSandi $acl = preg_split('/\s+/', $match); 717eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 718eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 719eb3ce0d5SKazutaka Miyasaka } 72048d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 72148d7b7a6SDominik Eckelmann continue; 72248d7b7a6SDominik Eckelmann } 7238ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 724f3f0262cSandi if($acl[2] > $perm) { 725f3f0262cSandi $perm = $acl[2]; 726f3f0262cSandi } 727f3f0262cSandi } 728f3f0262cSandi if($perm > -1) { 729f3f0262cSandi //we had a match - return it 730def492a2SGuillaume Turri return (int) $perm; 731f3f0262cSandi } 732f3f0262cSandi } 733f3f0262cSandi 734f3f0262cSandi //still here? do the namespace checks 735f3f0262cSandi if($ns) { 7363e304b55SMichael Hamann $path = $ns.':*'; 737f3f0262cSandi } else { 7383e304b55SMichael Hamann $path = '*'; //root document 739f3f0262cSandi } 740f3f0262cSandi 741f3f0262cSandi do { 742eb3ce0d5SKazutaka Miyasaka $matches = preg_grep('/^'.preg_quote($path, '/').'\s+(\S+)\s+/u', $AUTH_ACL); 743f3f0262cSandi if(count($matches)) { 744f3f0262cSandi foreach($matches as $match) { 745f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 746f3f0262cSandi $acl = preg_split('/\s+/', $match); 747eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 748eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 749eb3ce0d5SKazutaka Miyasaka } 75048d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 75148d7b7a6SDominik Eckelmann continue; 75248d7b7a6SDominik Eckelmann } 7538ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 754f3f0262cSandi if($acl[2] > $perm) { 755f3f0262cSandi $perm = $acl[2]; 756f3f0262cSandi } 757f3f0262cSandi } 758f3f0262cSandi //we had a match - return it 75948d7b7a6SDominik Eckelmann if($perm != -1) { 760def492a2SGuillaume Turri return (int) $perm; 761f3f0262cSandi } 76248d7b7a6SDominik Eckelmann } 763f3f0262cSandi //get next higher namespace 764f3f0262cSandi $ns = getNS($ns); 765f3f0262cSandi 7663e304b55SMichael Hamann if($path != '*') { 7673e304b55SMichael Hamann $path = $ns.':*'; 7683e304b55SMichael Hamann if($path == ':*') $path = '*'; 769f3f0262cSandi } else { 770f3f0262cSandi //we did this already 771f3f0262cSandi //looks like there is something wrong with the ACL 772f3f0262cSandi //break here 773d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 774d5ce66f6SAndreas Gohr return AUTH_NONE; 775f3f0262cSandi } 776f3f0262cSandi } while(1); //this should never loop endless 777ab5d26daSAndreas Gohr return AUTH_NONE; 778f3f0262cSandi} 779f3f0262cSandi 780f3f0262cSandi/** 7816c2bb100SAndreas Gohr * Encode ASCII special chars 7826c2bb100SAndreas Gohr * 7836c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7846c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7856c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7866c2bb100SAndreas Gohr * urlencoding!). 7876c2bb100SAndreas Gohr * 7886c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7896c2bb100SAndreas Gohr * 7906c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7916c2bb100SAndreas Gohr * @see rawurldecode() 7926c2bb100SAndreas Gohr */ 793e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 794a424cd8eSchris global $cache_authname; 795a424cd8eSchris $cache =& $cache_authname; 79631784267SAndreas Gohr $name = (string) $name; 797a424cd8eSchris 79880601d26SAndreas Gohr // never encode wildcard FS#1955 79980601d26SAndreas Gohr if($name == '%USER%') return $name; 800b78bf706Sromain if($name == '%GROUP%') return $name; 80180601d26SAndreas Gohr 802a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 803e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 804ab5d26daSAndreas Gohr $cache[$name][$skip_group] = '@'.preg_replace( 805ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 806ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", substr($name, 1) 807ab5d26daSAndreas Gohr ); 808e838fc2eSAndreas Gohr } else { 809ab5d26daSAndreas Gohr $cache[$name][$skip_group] = preg_replace( 810ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 811ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", $name 812ab5d26daSAndreas Gohr ); 813e838fc2eSAndreas Gohr } 8146c2bb100SAndreas Gohr } 8156c2bb100SAndreas Gohr 816a424cd8eSchris return $cache[$name][$skip_group]; 817a424cd8eSchris} 818a424cd8eSchris 8196c2bb100SAndreas Gohr/** 820f3f0262cSandi * Create a pronouncable password 821f3f0262cSandi * 8228a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8238a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8248a285f7fSAndreas Gohr * 82515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 82615fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8278a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 82815fae107Sandi * 8298a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 83015fae107Sandi * @return string pronouncable password 831f3f0262cSandi */ 8328a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 8338a285f7fSAndreas Gohr $data = array( 834d628dcf3SAndreas Gohr 'password' => '', 835d628dcf3SAndreas Gohr 'foruser' => $foruser 8368a285f7fSAndreas Gohr ); 8378a285f7fSAndreas Gohr 8388a285f7fSAndreas Gohr $evt = new Doku_Event('AUTH_PASSWORD_GENERATE', $data); 8398a285f7fSAndreas Gohr if($evt->advise_before(true)) { 840f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 841f3f0262cSandi $v = 'aeiou'; //vowels 842f3f0262cSandi $a = $c.$v; //both 843987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 844f3f0262cSandi 845987c8d26SAndreas Gohr //use thre syllables... 846987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 847483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 848483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 849483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 850f3f0262cSandi } 851987c8d26SAndreas Gohr //... and add a nice number and special 852483b6238SMichael Hamann $data['password'] .= auth_random(10, 99).$s[auth_random(0, strlen($s) - 1)]; 8538a285f7fSAndreas Gohr } 8548a285f7fSAndreas Gohr $evt->advise_after(); 855f3f0262cSandi 8568a285f7fSAndreas Gohr return $data['password']; 857f3f0262cSandi} 858f3f0262cSandi 859f3f0262cSandi/** 860f3f0262cSandi * Sends a password to the given user 861f3f0262cSandi * 86215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 863ab5d26daSAndreas Gohr * @param string $user Login name of the user 864ab5d26daSAndreas Gohr * @param string $password The new password in clear text 86515fae107Sandi * @return bool true on success 866f3f0262cSandi */ 867f3f0262cSandifunction auth_sendPassword($user, $password) { 868f3f0262cSandi global $lang; 86927058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 870cd52f92dSchris global $auth; 871beca106aSAdrian Lang if(!$auth) return false; 872cd52f92dSchris 873d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 874cd52f92dSchris $userinfo = $auth->getUserData($user); 875f3f0262cSandi 87687ddda95Sandi if(!$userinfo['mail']) return false; 877f3f0262cSandi 878f3f0262cSandi $text = rawLocale('password'); 879d7169d19SAndreas Gohr $trep = array( 880d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 881d7169d19SAndreas Gohr 'LOGIN' => $user, 882d7169d19SAndreas Gohr 'PASSWORD' => $password 883d7169d19SAndreas Gohr ); 884f3f0262cSandi 885d7169d19SAndreas Gohr $mail = new Mailer(); 886d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 887d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 888d7169d19SAndreas Gohr $mail->setBody($text, $trep); 889d7169d19SAndreas Gohr return $mail->send(); 890f3f0262cSandi} 891f3f0262cSandi 892f3f0262cSandi/** 89315fae107Sandi * Register a new user 894f3f0262cSandi * 89515fae107Sandi * This registers a new user - Data is read directly from $_POST 89615fae107Sandi * 89715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 89815fae107Sandi * @return bool true on success, false on any error 899f3f0262cSandi */ 900f3f0262cSandifunction register() { 901f3f0262cSandi global $lang; 902eb5d07e4Sjan global $conf; 90327058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 904cd52f92dSchris global $auth; 90564273335SAndreas Gohr global $INPUT; 906f3f0262cSandi 90764273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9083a48618aSAnika Henke if(!actionOK('register')) return false; 909640145a5Sandi 91064273335SAndreas Gohr // gather input 91164273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 91264273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 91364273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 91464273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 91564273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 916d752aedeSAndreas Gohr 91764273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 918f3f0262cSandi msg($lang['regmissing'], -1); 919f3f0262cSandi return false; 920f3f0262cSandi } 921f3f0262cSandi 922cab2716aSmatthias.grimm if($conf['autopasswd']) { 9238a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 92464273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 925bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 926cab2716aSmatthias.grimm return false; 92764273335SAndreas Gohr } elseif($pass != $passchk) { 928bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 929cab2716aSmatthias.grimm return false; 930cab2716aSmatthias.grimm } 931cab2716aSmatthias.grimm 932f3f0262cSandi //check mail 93364273335SAndreas Gohr if(!mail_isvalid($email)) { 934f3f0262cSandi msg($lang['regbadmail'], -1); 935f3f0262cSandi return false; 936f3f0262cSandi } 937f3f0262cSandi 938f3f0262cSandi //okay try to create the user 93964273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 940f3f0262cSandi msg($lang['reguexists'], -1); 941f3f0262cSandi return false; 942f3f0262cSandi } 943f3f0262cSandi 944790b7720SAndreas Gohr // send notification about the new user 945790b7720SAndreas Gohr $subscription = new Subscription(); 946790b7720SAndreas Gohr $subscription->send_register($login, $fullname, $email); 94702a498e7Schris 948790b7720SAndreas Gohr // are we done? 949cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 950cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 951cab2716aSmatthias.grimm return true; 952cab2716aSmatthias.grimm } 953cab2716aSmatthias.grimm 954790b7720SAndreas Gohr // autogenerated password? then send password to user 95564273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 956f3f0262cSandi msg($lang['regsuccess'], 1); 957f3f0262cSandi return true; 958f3f0262cSandi } else { 959f3f0262cSandi msg($lang['regmailfail'], -1); 960f3f0262cSandi return false; 961f3f0262cSandi } 962f3f0262cSandi} 963f3f0262cSandi 96410a76f6fSfrank/** 9658b06d178Schris * Update user profile 9668b06d178Schris * 9678b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9688b06d178Schris */ 9698b06d178Schrisfunction updateprofile() { 9708b06d178Schris global $conf; 9718b06d178Schris global $lang; 97227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 973cd52f92dSchris global $auth; 974bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 975bcc94b2cSAndreas Gohr global $INPUT; 9768b06d178Schris 977bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9781b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9798b06d178Schris 9803a48618aSAnika Henke if(!actionOK('profile')) { 9818b06d178Schris msg($lang['profna'], -1); 9828b06d178Schris return false; 9838b06d178Schris } 9848b06d178Schris 985bcc94b2cSAndreas Gohr $changes = array(); 986bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 987bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 988bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 989bcc94b2cSAndreas Gohr 990bcc94b2cSAndreas Gohr // check misspelled passwords 991bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 992bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 9938b06d178Schris return false; 9948b06d178Schris } 9958b06d178Schris 9968b06d178Schris // clean fullname and email 997bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 998bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 9998b06d178Schris 1000bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 1001bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 1002bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1003ab5d26daSAndreas Gohr ) { 10048b06d178Schris msg($lang['profnoempty'], -1); 10058b06d178Schris return false; 10068b06d178Schris } 1007bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10088b06d178Schris msg($lang['regbadmail'], -1); 10098b06d178Schris return false; 10108b06d178Schris } 10118b06d178Schris 1012bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10134c21b7eeSAndreas Gohr 1014bcc94b2cSAndreas Gohr // check for unavailable capabilities 1015bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 1016bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 1017bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 1018bcc94b2cSAndreas Gohr 1019bcc94b2cSAndreas Gohr // anything to do? 10208b06d178Schris if(!count($changes)) { 10218b06d178Schris msg($lang['profnochange'], -1); 10228b06d178Schris return false; 10238b06d178Schris } 10248b06d178Schris 10258b06d178Schris if($conf['profileconfirm']) { 1026bcc94b2cSAndreas Gohr if(!$auth->checkPass($_SERVER['REMOTE_USER'], $INPUT->post->str('oldpass'))) { 10278b06d178Schris msg($lang['badlogin'], -1); 10288b06d178Schris return false; 10298b06d178Schris } 10308b06d178Schris } 10318b06d178Schris 1032a0b5b007SChris Smith if($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) { 1033a0b5b007SChris Smith // update cookie and session with the changed data 103432ed2b36SAndreas Gohr if($changes['pass']) { 1035ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 103604369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1037a0b5b007SChris Smith auth_setCookie($_SERVER['REMOTE_USER'], $pass, (bool) $sticky); 103832ed2b36SAndreas Gohr } 103925b2a98cSMichael Klier return true; 1040a0b5b007SChris Smith } 1041ab5d26daSAndreas Gohr 1042ab5d26daSAndreas Gohr return false; 10438b06d178Schris} 10448b06d178Schris 10458b06d178Schris/** 10468b06d178Schris * Send a new password 10478b06d178Schris * 10481d5856cfSAndreas Gohr * This function handles both phases of the password reset: 10491d5856cfSAndreas Gohr * 10501d5856cfSAndreas Gohr * - handling the first request of password reset 10511d5856cfSAndreas Gohr * - validating the password reset auth token 10521d5856cfSAndreas Gohr * 10538b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 10548b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 10551d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10568b06d178Schris * 10578b06d178Schris * @return bool true on success, false on any error 10588b06d178Schris */ 10598b06d178Schrisfunction act_resendpwd() { 10608b06d178Schris global $lang; 10618b06d178Schris global $conf; 106227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1063cd52f92dSchris global $auth; 1064bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1065bcc94b2cSAndreas Gohr global $INPUT; 10668b06d178Schris 10673a48618aSAnika Henke if(!actionOK('resendpwd')) { 10688b06d178Schris msg($lang['resendna'], -1); 10698b06d178Schris return false; 10708b06d178Schris } 10718b06d178Schris 1072bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 10738b06d178Schris 10741d5856cfSAndreas Gohr if($token) { 1075cc204bbdSAndreas Gohr // we're in token phase - get user info from token 10761d5856cfSAndreas Gohr 10771d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 10781d5856cfSAndreas Gohr if(!@file_exists($tfile)) { 10791d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1080bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 10811d5856cfSAndreas Gohr return false; 10821d5856cfSAndreas Gohr } 10838a9735e3SAndreas Gohr // token is only valid for 3 days 10848a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 10858a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1086bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 10871d5856cfSAndreas Gohr @unlink($tfile); 10888a9735e3SAndreas Gohr return false; 10898a9735e3SAndreas Gohr } 10908a9735e3SAndreas Gohr 10918b06d178Schris $user = io_readfile($tfile); 1092cd52f92dSchris $userinfo = $auth->getUserData($user); 10938b06d178Schris if(!$userinfo['mail']) { 10948b06d178Schris msg($lang['resendpwdnouser'], -1); 10958b06d178Schris return false; 10968b06d178Schris } 10978b06d178Schris 1098cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1099bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1100bcc94b2cSAndreas Gohr 1101cc204bbdSAndreas Gohr // password given correctly? 1102bcc94b2cSAndreas Gohr if(!$pass) return false; 1103bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1104451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1105cc204bbdSAndreas Gohr return false; 1106cc204bbdSAndreas Gohr } 1107cc204bbdSAndreas Gohr 1108bcc94b2cSAndreas Gohr // change it 1109cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1110cc204bbdSAndreas Gohr msg('error modifying user data', -1); 1111cc204bbdSAndreas Gohr return false; 1112cc204bbdSAndreas Gohr } 1113cc204bbdSAndreas Gohr 1114cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1115cc204bbdSAndreas Gohr 11168a285f7fSAndreas Gohr $pass = auth_pwgen($user); 11177d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 11188b06d178Schris msg('error modifying user data', -1); 11198b06d178Schris return false; 11208b06d178Schris } 11218b06d178Schris 11228b06d178Schris if(auth_sendPassword($user, $pass)) { 11238b06d178Schris msg($lang['resendpwdsuccess'], 1); 11248b06d178Schris } else { 11258b06d178Schris msg($lang['regmailfail'], -1); 11268b06d178Schris } 1127cc204bbdSAndreas Gohr } 1128cc204bbdSAndreas Gohr 1129cc204bbdSAndreas Gohr @unlink($tfile); 11308b06d178Schris return true; 11311d5856cfSAndreas Gohr 11321d5856cfSAndreas Gohr } else { 11331d5856cfSAndreas Gohr // we're in request phase 11341d5856cfSAndreas Gohr 1135bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 11361d5856cfSAndreas Gohr 1137bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 11381d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 11391d5856cfSAndreas Gohr return false; 11401d5856cfSAndreas Gohr } else { 1141bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 11421d5856cfSAndreas Gohr } 11431d5856cfSAndreas Gohr 11441d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 11451d5856cfSAndreas Gohr if(!$userinfo['mail']) { 11461d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 11471d5856cfSAndreas Gohr return false; 11481d5856cfSAndreas Gohr } 11491d5856cfSAndreas Gohr 11501d5856cfSAndreas Gohr // generate auth token 1151483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 11521d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 11531d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 11541d5856cfSAndreas Gohr 11551d5856cfSAndreas Gohr io_saveFile($tfile, $user); 11561d5856cfSAndreas Gohr 11571d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1158d7169d19SAndreas Gohr $trep = array( 1159d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1160d7169d19SAndreas Gohr 'LOGIN' => $user, 1161d7169d19SAndreas Gohr 'CONFIRM' => $url 1162d7169d19SAndreas Gohr ); 11631d5856cfSAndreas Gohr 1164d7169d19SAndreas Gohr $mail = new Mailer(); 1165d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1166d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1167d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1168d7169d19SAndreas Gohr if($mail->send()) { 11691d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 11701d5856cfSAndreas Gohr } else { 11711d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 11721d5856cfSAndreas Gohr } 11731d5856cfSAndreas Gohr return true; 11741d5856cfSAndreas Gohr } 1175ab5d26daSAndreas Gohr // never reached 11768b06d178Schris} 11778b06d178Schris 11788b06d178Schris/** 1179b0855b11Sandi * Encrypts a password using the given method and salt 1180b0855b11Sandi * 1181b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1182b0855b11Sandi * is chosen. 1183b0855b11Sandi * 1184b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 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> 1209ab5d26daSAndreas Gohr * @param string $clear The clear text password 1210ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1211ab5d26daSAndreas Gohr * @return bool true if both match 1212b0855b11Sandi */ 1213b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 12143a0a2d05SAndreas Gohr $pass = new PassHash(); 12153a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1216b0855b11Sandi} 1217340756e4Sandi 1218a0b5b007SChris Smith/** 1219a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1220a0b5b007SChris Smith * 1221a0b5b007SChris Smith * @param string $user username 1222a0b5b007SChris Smith * @param string $pass encrypted password 1223a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1224ab5d26daSAndreas Gohr * @return bool 1225a0b5b007SChris Smith */ 1226a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1227a0b5b007SChris Smith global $conf; 122827058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1229a0b5b007SChris Smith global $auth; 123079d00841SOliver Geisen global $USERINFO; 1231a0b5b007SChris Smith 1232beca106aSAdrian Lang if(!$auth) return false; 1233a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1234a0b5b007SChris Smith 1235a0b5b007SChris Smith // set cookie 1236645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 123773ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1238c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1239a0b5b007SChris Smith if(version_compare(PHP_VERSION, '5.2.0', '>')) { 124073ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 1241a0b5b007SChris Smith } else { 124273ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 1243a0b5b007SChris Smith } 1244a0b5b007SChris Smith // set session 1245a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1246234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1247a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1248a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1249a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1250ab5d26daSAndreas Gohr 1251ab5d26daSAndreas Gohr return true; 1252a0b5b007SChris Smith} 1253a0b5b007SChris Smith 1254645c0a36SAndreas Gohr/** 1255645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1256645c0a36SAndreas Gohr * 1257645c0a36SAndreas Gohr * @returns array 1258645c0a36SAndreas Gohr */ 1259645c0a36SAndreas Gohrfunction auth_getCookie() { 1260c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1261c66972f2SAdrian Lang return array(null, null, null); 1262c66972f2SAdrian Lang } 1263645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1264645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1265645c0a36SAndreas Gohr $pass = base64_decode($pass); 1266645c0a36SAndreas Gohr $user = base64_decode($user); 1267645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1268645c0a36SAndreas Gohr} 1269645c0a36SAndreas Gohr 1270e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1271