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 98395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 9993a7873eSAndreas Gohr if (true === $auth->success) { 100395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 101395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 102f4476bd9SJan Schumann } 103191bb90aSAndreas Gohr 1048eca974cSAndreas Gohr if(!is_null($auth) && $auth->canDo('external')) { 105f13fa892SAndreas Gohr // external trust mechanism in place 106bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 107f5cb575dSAndreas Gohr } else { 1086080c584SRobin Gareus $evdata = array( 109bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 110bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 111bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 112bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1136080c584SRobin Gareus ); 114b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 115f5cb575dSAndreas Gohr } 116f5cb575dSAndreas Gohr 11716905344SAndreas Gohr //load ACL into a global array XXX 11875c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 119ab5d26daSAndreas Gohr 120ab5d26daSAndreas Gohr return true; 12175c93b77SAndreas Gohr} 12275c93b77SAndreas Gohr 12375c93b77SAndreas Gohr/** 12475c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12575c93b77SAndreas Gohr * 12675c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12742ea7f44SGerrit Uitslag * 128ab5d26daSAndreas Gohr * @return array 12975c93b77SAndreas Gohr */ 13075c93b77SAndreas Gohrfunction auth_loadACL() { 13175c93b77SAndreas Gohr global $config_cascade; 132b78bf706Sromain global $USERINFO; 133585bf44eSChristopher Smith /* @var Input $INPUT */ 134585bf44eSChristopher Smith global $INPUT; 13575c93b77SAndreas Gohr 13675c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 13775c93b77SAndreas Gohr 13875c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13975c93b77SAndreas Gohr 14032e82180SAndreas Gohr $out = array(); 1419ce556d2SAndreas Gohr foreach($acl as $line) { 1429ce556d2SAndreas Gohr $line = trim($line); 143443e135dSChristopher Smith if(empty($line) || ($line{0} == '#')) continue; // skip blank lines & comments 14421c3090aSChristopher Smith list($id,$rest) = preg_split('/[ \t]+/',$line,2); 14532e82180SAndreas Gohr 146443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 147ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 148ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 149585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 150ad3d68d7SChristopher Smith 151585bf44eSChristopher Smith $id = str_replace('%USER%',cleanID($INPUT->server->str('REMOTE_USER')),$id); 152585bf44eSChristopher Smith $rest = str_replace('%USER%',auth_nameencode($INPUT->server->str('REMOTE_USER')),$rest); 153ad3d68d7SChristopher Smith } 154ad3d68d7SChristopher Smith 155ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1569ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 157ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 1589ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 159b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 16032e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 16132e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 162b78bf706Sromain } 16332e82180SAndreas Gohr } else { 16432e82180SAndreas Gohr $out[] = "$id\t$rest"; 165a8fe108bSGuy Brand } 16611799630Sandi } 1679ce556d2SAndreas Gohr 16832e82180SAndreas Gohr return $out; 169f3f0262cSandi} 170f3f0262cSandi 171ab5d26daSAndreas Gohr/** 172ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 173ab5d26daSAndreas Gohr * 17442ea7f44SGerrit Uitslag * @param array $evdata 175ab5d26daSAndreas Gohr * @return bool 176ab5d26daSAndreas Gohr */ 177b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 178ab5d26daSAndreas Gohr return auth_login( 179ab5d26daSAndreas Gohr $evdata['user'], 180b5ee21aaSAdrian Lang $evdata['password'], 181b5ee21aaSAdrian Lang $evdata['sticky'], 182ab5d26daSAndreas Gohr $evdata['silent'] 183ab5d26daSAndreas Gohr ); 184b5ee21aaSAdrian Lang} 185b5ee21aaSAdrian Lang 186f3f0262cSandi/** 187f3f0262cSandi * This tries to login the user based on the sent auth credentials 188f3f0262cSandi * 189f3f0262cSandi * The authentication works like this: if a username was given 19015fae107Sandi * a new login is assumed and user/password are checked. If they 19115fae107Sandi * are correct the password is encrypted with blowfish and stored 19215fae107Sandi * together with the username in a cookie - the same info is stored 19315fae107Sandi * in the session, too. Additonally a browserID is stored in the 19415fae107Sandi * session. 19515fae107Sandi * 19615fae107Sandi * If no username was given the cookie is checked: if the username, 19715fae107Sandi * crypted password and browserID match between session and cookie 19815fae107Sandi * no further testing is done and the user is accepted 19915fae107Sandi * 20015fae107Sandi * If a cookie was found but no session info was availabe the 201136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 20215fae107Sandi * together with username rechecked by calling this function again. 203f3f0262cSandi * 204f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 205f3f0262cSandi * are set. 20615fae107Sandi * 20715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20815fae107Sandi * 20915fae107Sandi * @param string $user Username 21015fae107Sandi * @param string $pass Cleartext Password 21115fae107Sandi * @param bool $sticky Cookie should not expire 212f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 21315fae107Sandi * @return bool true on successful auth 214f3f0262cSandi */ 215f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 216f3f0262cSandi global $USERINFO; 217f3f0262cSandi global $conf; 218f3f0262cSandi global $lang; 21927058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 220cd52f92dSchris global $auth; 221585bf44eSChristopher Smith /* @var Input $INPUT */ 222585bf44eSChristopher Smith global $INPUT; 223ab5d26daSAndreas Gohr 224132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 225f3f0262cSandi 226beca106aSAdrian Lang if(!$auth) return false; 227beca106aSAdrian Lang 228bbbd6568SAndreas Gohr if(!empty($user)) { 229132bdbfeSandi //usual login 2305e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 231132bdbfeSandi // make logininfo globally available 232585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 23330d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 23404369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 235132bdbfeSandi return true; 236f3f0262cSandi } else { 237f3f0262cSandi //invalid credentials - log off 238f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 239f3f0262cSandi auth_logoff(); 240132bdbfeSandi return false; 241f3f0262cSandi } 242f3f0262cSandi } else { 243132bdbfeSandi // read cookie information 244645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 245132bdbfeSandi if($user && $pass) { 246132bdbfeSandi // we got a cookie - see if we can trust it 247fa7c70ffSAdrian Lang 248fa7c70ffSAdrian Lang // get session info 249fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 250132bdbfeSandi if(isset($session) && 2517172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2524c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 253132bdbfeSandi ($session['user'] == $user) && 254234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 255ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 256ab5d26daSAndreas Gohr ) { 257234ce57eSAndreas Gohr 258132bdbfeSandi // he has session, cookie and browser right - let him in 259585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 260132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 261132bdbfeSandi return true; 262132bdbfeSandi } 263f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 26430d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26504369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 266f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 267132bdbfeSandi } 268132bdbfeSandi } 269f3f0262cSandi //just to be sure 270883179a4SAndreas Gohr auth_logoff(true); 271132bdbfeSandi return false; 272f3f0262cSandi} 273132bdbfeSandi 274132bdbfeSandi/** 275136ce040Sandi * Builds a pseudo UID from browser and IP data 276132bdbfeSandi * 277132bdbfeSandi * This is neither unique nor unfakable - still it adds some 278136ce040Sandi * security. Using the first part of the IP makes sure 27980b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 28015fae107Sandi * 28115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 28215fae107Sandi * 28315fae107Sandi * @return string a MD5 sum of various browser headers 284132bdbfeSandi */ 285132bdbfeSandifunction auth_browseruid() { 286585bf44eSChristopher Smith /* @var Input $INPUT */ 287585bf44eSChristopher Smith global $INPUT; 288585bf44eSChristopher Smith 2892f9daf16SAndreas Gohr $ip = clientIP(true); 290132bdbfeSandi $uid = ''; 291585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_USER_AGENT'); 292585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_CHARSET'); 2932f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 29480b4f376SAndreas Gohr $uid = strtolower($uid); 295132bdbfeSandi return md5($uid); 296132bdbfeSandi} 297132bdbfeSandi 298132bdbfeSandi/** 299132bdbfeSandi * Creates a random key to encrypt the password in cookies 30015fae107Sandi * 30115fae107Sandi * This function tries to read the password for encrypting 30298407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 30315fae107Sandi * if no such file is found a random key is created and 30415fae107Sandi * and stored in this file. 30515fae107Sandi * 30615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30742ea7f44SGerrit Uitslag * 30832ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 30930d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 31015fae107Sandi * @return string 311132bdbfeSandi */ 31230d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 313132bdbfeSandi global $conf; 31498407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 31530d544a4SMichael Hamann if ($secure || !file_exists($file)) { 31630d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 31730d544a4SMichael Hamann } 318132bdbfeSandi $salt = io_readFile($file); 319132bdbfeSandi if(empty($salt)) { 32030d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 321132bdbfeSandi io_saveFile($file, $salt); 322132bdbfeSandi } 32332ed2b36SAndreas Gohr if($addsession) { 32432ed2b36SAndreas Gohr $salt .= session_id(); 32532ed2b36SAndreas Gohr } 326132bdbfeSandi return $salt; 327f3f0262cSandi} 328f3f0262cSandi 329f3f0262cSandi/** 330483b6238SMichael Hamann * Return truly (pseudo) random bytes if available, otherwise fall back to mt_rand 331483b6238SMichael Hamann * 332483b6238SMichael Hamann * @author Mark Seecof 333483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 334483b6238SMichael Hamann * @link http://www.php.net/manual/de/function.mt-rand.php#83655 33542ea7f44SGerrit Uitslag * 336483b6238SMichael Hamann * @param int $length number of bytes to get 337483b6238SMichael Hamann * @return string binary random strings 338483b6238SMichael Hamann */ 339483b6238SMichael Hamannfunction auth_randombytes($length) { 340483b6238SMichael Hamann $strong = false; 341483b6238SMichael Hamann $rbytes = false; 342483b6238SMichael Hamann 343483b6238SMichael Hamann if (function_exists('openssl_random_pseudo_bytes') 344483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.4') >= 0 345483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 346483b6238SMichael Hamann ) { 347483b6238SMichael Hamann $rbytes = openssl_random_pseudo_bytes($length, $strong); 348483b6238SMichael Hamann } 349483b6238SMichael Hamann 350483b6238SMichael Hamann if (!$strong && function_exists('mcrypt_create_iv') 351483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.7') >= 0 352483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 353483b6238SMichael Hamann ) { 354483b6238SMichael Hamann $rbytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); 355483b6238SMichael Hamann if ($rbytes !== false && strlen($rbytes) === $length) { 356483b6238SMichael Hamann $strong = true; 357483b6238SMichael Hamann } 358483b6238SMichael Hamann } 359483b6238SMichael Hamann 360483b6238SMichael Hamann // If no strong randoms available, try OS the specific ways 361483b6238SMichael Hamann if(!$strong) { 362483b6238SMichael Hamann // Unix/Linux platform 363483b6238SMichael Hamann $fp = @fopen('/dev/urandom', 'rb'); 364483b6238SMichael Hamann if($fp !== false) { 365483b6238SMichael Hamann $rbytes = fread($fp, $length); 366483b6238SMichael Hamann fclose($fp); 367483b6238SMichael Hamann } 368483b6238SMichael Hamann 369483b6238SMichael Hamann // MS-Windows platform 370483b6238SMichael Hamann if(class_exists('COM')) { 371483b6238SMichael Hamann // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx 372483b6238SMichael Hamann try { 373483b6238SMichael Hamann $CAPI_Util = new COM('CAPICOM.Utilities.1'); 374483b6238SMichael Hamann $rbytes = $CAPI_Util->GetRandom($length, 0); 375483b6238SMichael Hamann 376483b6238SMichael Hamann // if we ask for binary data PHP munges it, so we 377483b6238SMichael Hamann // request base64 return value. 378483b6238SMichael Hamann if($rbytes) $rbytes = base64_decode($rbytes); 379483b6238SMichael Hamann } catch(Exception $ex) { 380483b6238SMichael Hamann // fail 381483b6238SMichael Hamann } 382483b6238SMichael Hamann } 383483b6238SMichael Hamann } 384483b6238SMichael Hamann if(strlen($rbytes) < $length) $rbytes = false; 385483b6238SMichael Hamann 386483b6238SMichael Hamann // still no random bytes available - fall back to mt_rand() 387483b6238SMichael Hamann if($rbytes === false) { 388483b6238SMichael Hamann $rbytes = ''; 389483b6238SMichael Hamann for ($i = 0; $i < $length; ++$i) { 390483b6238SMichael Hamann $rbytes .= chr(mt_rand(0, 255)); 391483b6238SMichael Hamann } 392483b6238SMichael Hamann } 393483b6238SMichael Hamann 394483b6238SMichael Hamann return $rbytes; 395483b6238SMichael Hamann} 396483b6238SMichael Hamann 397483b6238SMichael Hamann/** 398483b6238SMichael Hamann * Random number generator using the best available source 399483b6238SMichael Hamann * 400483b6238SMichael Hamann * @author Michael Samuel 401483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 40242ea7f44SGerrit Uitslag * 403483b6238SMichael Hamann * @param int $min 404483b6238SMichael Hamann * @param int $max 405483b6238SMichael Hamann * @return int 406483b6238SMichael Hamann */ 407483b6238SMichael Hamannfunction auth_random($min, $max) { 408483b6238SMichael Hamann $abs_max = $max - $min; 409483b6238SMichael Hamann 410483b6238SMichael Hamann $nbits = 0; 411483b6238SMichael Hamann for ($n = $abs_max; $n > 0; $n >>= 1) { 412483b6238SMichael Hamann ++$nbits; 413483b6238SMichael Hamann } 414483b6238SMichael Hamann 415483b6238SMichael Hamann $mask = (1 << $nbits) - 1; 416483b6238SMichael Hamann do { 417483b6238SMichael Hamann $bytes = auth_randombytes(PHP_INT_SIZE); 418483b6238SMichael Hamann $integers = unpack('Inum', $bytes); 419483b6238SMichael Hamann $integer = $integers["num"] & $mask; 420483b6238SMichael Hamann } while ($integer > $abs_max); 421483b6238SMichael Hamann 422483b6238SMichael Hamann return $min + $integer; 423483b6238SMichael Hamann} 424483b6238SMichael Hamann 425483b6238SMichael Hamann/** 42604369c3eSMichael Hamann * Encrypt data using the given secret using AES 42704369c3eSMichael Hamann * 42804369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 42904369c3eSMichael Hamann * using pbkdf2. 43004369c3eSMichael Hamann * 43104369c3eSMichael Hamann * @param string $data The data that shall be encrypted 43204369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 43304369c3eSMichael Hamann * @return string The ciphertext 43404369c3eSMichael Hamann */ 43504369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 43604369c3eSMichael Hamann $iv = auth_randombytes(16); 43704369c3eSMichael Hamann $cipher = new Crypt_AES(); 43804369c3eSMichael Hamann $cipher->setPassword($secret); 43904369c3eSMichael Hamann 4407b650cefSMichael Hamann /* 4417b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 4427b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 4437b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 4447b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 4457b650cefSMichael Hamann */ 44604369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 44704369c3eSMichael Hamann} 44804369c3eSMichael Hamann 44904369c3eSMichael Hamann/** 45004369c3eSMichael Hamann * Decrypt the given AES ciphertext 45104369c3eSMichael Hamann * 45204369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 45304369c3eSMichael Hamann * 45404369c3eSMichael Hamann * @param string $ciphertext The encrypted data 45504369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 45604369c3eSMichael Hamann * @return string The decrypted data 45704369c3eSMichael Hamann */ 45804369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 4597b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 46004369c3eSMichael Hamann $cipher = new Crypt_AES(); 46104369c3eSMichael Hamann $cipher->setPassword($secret); 4627b650cefSMichael Hamann $cipher->setIV($iv); 46304369c3eSMichael Hamann 4647b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 46504369c3eSMichael Hamann} 46604369c3eSMichael Hamann 46704369c3eSMichael Hamann/** 468883179a4SAndreas Gohr * Log out the current user 469883179a4SAndreas Gohr * 470f3f0262cSandi * This clears all authentication data and thus log the user 471883179a4SAndreas Gohr * off. It also clears session data. 47215fae107Sandi * 47315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 47442ea7f44SGerrit Uitslag * 475883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 476f3f0262cSandi */ 477883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 478f3f0262cSandi global $conf; 479f3f0262cSandi global $USERINFO; 48027058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 4815298a619SAndreas Gohr global $auth; 482585bf44eSChristopher Smith /* @var Input $INPUT */ 483585bf44eSChristopher Smith global $INPUT; 48437065e65Sandi 485d4869846SAndreas Gohr // make sure the session is writable (it usually is) 486e9621d07SAndreas Gohr @session_start(); 487e9621d07SAndreas Gohr 488e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 489e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 490e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 491e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 492e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 493e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 494883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 495e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 496585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 497132bdbfeSandi $USERINFO = null; //FIXME 498f5c6743cSAndreas Gohr 49973ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 50073ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 5015298a619SAndreas Gohr 502880f62faSAndreas Gohr if($auth) $auth->logOff(); 503f3f0262cSandi} 504f3f0262cSandi 505f3f0262cSandi/** 506f8cc712eSAndreas Gohr * Check if a user is a manager 507f8cc712eSAndreas Gohr * 508f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 509f8cc712eSAndreas Gohr * user. 510f8cc712eSAndreas Gohr * 511f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 512f8cc712eSAndreas Gohr * 513f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 514f8cc712eSAndreas Gohr * @see auth_isadmin 51542ea7f44SGerrit Uitslag * 516ab5d26daSAndreas Gohr * @param string $user Username 517ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 518ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 519ab5d26daSAndreas Gohr * @return bool 520f8cc712eSAndreas Gohr */ 521f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 522f8cc712eSAndreas Gohr global $conf; 523f8cc712eSAndreas Gohr global $USERINFO; 52427058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 525d752aedeSAndreas Gohr global $auth; 526585bf44eSChristopher Smith /* @var Input $INPUT */ 527585bf44eSChristopher Smith global $INPUT; 528585bf44eSChristopher Smith 529f8cc712eSAndreas Gohr 530beca106aSAdrian Lang if(!$auth) return false; 531c66972f2SAdrian Lang if(is_null($user)) { 532585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 533c66972f2SAdrian Lang return false; 534c66972f2SAdrian Lang } else { 535585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 536c66972f2SAdrian Lang } 537c66972f2SAdrian Lang } 538d6dc956fSAndreas Gohr if(is_null($groups)) { 539d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 540e259aa79SAndreas Gohr } 541e259aa79SAndreas Gohr 542d6dc956fSAndreas Gohr // check superuser match 543d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 544d6dc956fSAndreas Gohr if($adminonly) return false; 545e259aa79SAndreas Gohr // check managers 546d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 54700ce12daSChris Smith 548f8cc712eSAndreas Gohr return false; 549f8cc712eSAndreas Gohr} 550f8cc712eSAndreas Gohr 551f8cc712eSAndreas Gohr/** 552f8cc712eSAndreas Gohr * Check if a user is admin 553f8cc712eSAndreas Gohr * 554f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 555f8cc712eSAndreas Gohr * 556f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 557f8cc712eSAndreas Gohr * 558f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 559ab5d26daSAndreas Gohr * @see auth_ismanager() 56042ea7f44SGerrit Uitslag * 561ab5d26daSAndreas Gohr * @param string $user Username 562ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 563ab5d26daSAndreas Gohr * @return bool 564f8cc712eSAndreas Gohr */ 565f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 566f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 567f8cc712eSAndreas Gohr} 568f8cc712eSAndreas Gohr 569d6dc956fSAndreas Gohr/** 570d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 571d6dc956fSAndreas Gohr * users and groups to determine membership status 572d6dc956fSAndreas Gohr * 573d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 574d6dc956fSAndreas Gohr * 57542ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 57642ea7f44SGerrit Uitslag * @param string $user user to match against 57742ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5785446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 579d6dc956fSAndreas Gohr */ 580d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 58127058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 582d6dc956fSAndreas Gohr global $auth; 583d6dc956fSAndreas Gohr if(!$auth) return false; 584d6dc956fSAndreas Gohr 585d6dc956fSAndreas Gohr // clean user and groups 5864f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 587d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 588d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 589d6dc956fSAndreas Gohr } 590d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 591d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 592d6dc956fSAndreas Gohr 593d6dc956fSAndreas Gohr // extract the memberlist 594d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 595d6dc956fSAndreas Gohr $members = array_map('trim', $members); 596d6dc956fSAndreas Gohr $members = array_unique($members); 597d6dc956fSAndreas Gohr $members = array_filter($members); 598d6dc956fSAndreas Gohr 599d6dc956fSAndreas Gohr // compare cleaned values 600d6dc956fSAndreas Gohr foreach($members as $member) { 601e5204a12SJurgen Hart if($member == '@ALL' ) return true; 6024f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 603d6dc956fSAndreas Gohr if($member[0] == '@') { 604d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 605d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 606d6dc956fSAndreas Gohr } else { 607d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 608d6dc956fSAndreas Gohr if($member == $user) return true; 609d6dc956fSAndreas Gohr } 610d6dc956fSAndreas Gohr } 611d6dc956fSAndreas Gohr 612d6dc956fSAndreas Gohr // still here? not a member! 613d6dc956fSAndreas Gohr return false; 614d6dc956fSAndreas Gohr} 615d6dc956fSAndreas Gohr 616f8cc712eSAndreas Gohr/** 61715fae107Sandi * Convinience function for auth_aclcheck() 61815fae107Sandi * 61915fae107Sandi * This checks the permissions for the current user 62015fae107Sandi * 62115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 62215fae107Sandi * 6231698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 62415fae107Sandi * @return int permission level 625f3f0262cSandi */ 626f3f0262cSandifunction auth_quickaclcheck($id) { 627f3f0262cSandi global $conf; 628f3f0262cSandi global $USERINFO; 629585bf44eSChristopher Smith /* @var Input $INPUT */ 630585bf44eSChristopher Smith global $INPUT; 631f3f0262cSandi # if no ACL is used always return upload rights 632f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 633585bf44eSChristopher Smith return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 634f3f0262cSandi} 635f3f0262cSandi 636f3f0262cSandi/** 637c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 63815fae107Sandi * 63915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 64042ea7f44SGerrit Uitslag * 641c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6421698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 64315fae107Sandi * @param string $user Username 6443272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 64515fae107Sandi * @return int permission level 646f3f0262cSandi */ 647f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 648c17acc9fSAndreas Gohr $data = array( 649c17acc9fSAndreas Gohr 'id' => $id, 650c17acc9fSAndreas Gohr 'user' => $user, 651c17acc9fSAndreas Gohr 'groups' => $groups 652c17acc9fSAndreas Gohr ); 653c17acc9fSAndreas Gohr 654c17acc9fSAndreas Gohr return trigger_event('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 655c17acc9fSAndreas Gohr} 656c17acc9fSAndreas Gohr 657c17acc9fSAndreas Gohr/** 658c17acc9fSAndreas Gohr * default ACL check method 659c17acc9fSAndreas Gohr * 660c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 661c17acc9fSAndreas Gohr * 662c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 66342ea7f44SGerrit Uitslag * 664c17acc9fSAndreas Gohr * @param array $data event data 665c17acc9fSAndreas Gohr * @return int permission level 666c17acc9fSAndreas Gohr */ 667c17acc9fSAndreas Gohrfunction auth_aclcheck_cb($data) { 668c17acc9fSAndreas Gohr $id =& $data['id']; 669c17acc9fSAndreas Gohr $user =& $data['user']; 670c17acc9fSAndreas Gohr $groups =& $data['groups']; 671c17acc9fSAndreas Gohr 672f3f0262cSandi global $conf; 673f3f0262cSandi global $AUTH_ACL; 67427058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 675d752aedeSAndreas Gohr global $auth; 676f3f0262cSandi 67785d03f68SAndreas Gohr // if no ACL is used always return upload rights 678f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 679beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 680f3f0262cSandi 681074cf26bSandi //make sure groups is an array 682074cf26bSandi if(!is_array($groups)) $groups = array(); 683074cf26bSandi 68485d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 685ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 686ab5d26daSAndreas Gohr return AUTH_ADMIN; 687ab5d26daSAndreas Gohr } 68885d03f68SAndreas Gohr 689eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 690eb3ce0d5SKazutaka Miyasaka $user = utf8_strtolower($user); 691eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 692eb3ce0d5SKazutaka Miyasaka } 69337ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 694d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 69585d03f68SAndreas Gohr 6966c2bb100SAndreas Gohr //prepend groups with @ and nameencode 69737ff2261SSascha Klopp foreach($groups as &$group) { 69837ff2261SSascha Klopp $group = '@'.auth_nameencode($group); 69910a76f6fSfrank } 70010a76f6fSfrank 701f3f0262cSandi $ns = getNS($id); 702f3f0262cSandi $perm = -1; 703f3f0262cSandi 704f3f0262cSandi //add ALL group 705f3f0262cSandi $groups[] = '@ALL'; 70637ff2261SSascha Klopp 707f3f0262cSandi //add User 70834aeb4afSAndreas Gohr if($user) $groups[] = $user; 709f3f0262cSandi 710f3f0262cSandi //check exact match first 71121c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 712f3f0262cSandi if(count($matches)) { 713f3f0262cSandi foreach($matches as $match) { 714f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 71521c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 716eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 717eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 718eb3ce0d5SKazutaka Miyasaka } 71948d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 72048d7b7a6SDominik Eckelmann continue; 72148d7b7a6SDominik Eckelmann } 7228ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 723f3f0262cSandi if($acl[2] > $perm) { 724f3f0262cSandi $perm = $acl[2]; 725f3f0262cSandi } 726f3f0262cSandi } 727f3f0262cSandi if($perm > -1) { 728f3f0262cSandi //we had a match - return it 729def492a2SGuillaume Turri return (int) $perm; 730f3f0262cSandi } 731f3f0262cSandi } 732f3f0262cSandi 733f3f0262cSandi //still here? do the namespace checks 734f3f0262cSandi if($ns) { 7353e304b55SMichael Hamann $path = $ns.':*'; 736f3f0262cSandi } else { 7373e304b55SMichael Hamann $path = '*'; //root document 738f3f0262cSandi } 739f3f0262cSandi 740f3f0262cSandi do { 74121c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 742f3f0262cSandi if(count($matches)) { 743f3f0262cSandi foreach($matches as $match) { 744f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 74521c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 746eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 747eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 748eb3ce0d5SKazutaka Miyasaka } 74948d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 75048d7b7a6SDominik Eckelmann continue; 75148d7b7a6SDominik Eckelmann } 7528ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 753f3f0262cSandi if($acl[2] > $perm) { 754f3f0262cSandi $perm = $acl[2]; 755f3f0262cSandi } 756f3f0262cSandi } 757f3f0262cSandi //we had a match - return it 75848d7b7a6SDominik Eckelmann if($perm != -1) { 759def492a2SGuillaume Turri return (int) $perm; 760f3f0262cSandi } 76148d7b7a6SDominik Eckelmann } 762f3f0262cSandi //get next higher namespace 763f3f0262cSandi $ns = getNS($ns); 764f3f0262cSandi 7653e304b55SMichael Hamann if($path != '*') { 7663e304b55SMichael Hamann $path = $ns.':*'; 7673e304b55SMichael Hamann if($path == ':*') $path = '*'; 768f3f0262cSandi } else { 769f3f0262cSandi //we did this already 770f3f0262cSandi //looks like there is something wrong with the ACL 771f3f0262cSandi //break here 772d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 773d5ce66f6SAndreas Gohr return AUTH_NONE; 774f3f0262cSandi } 775f3f0262cSandi } while(1); //this should never loop endless 776ab5d26daSAndreas Gohr return AUTH_NONE; 777f3f0262cSandi} 778f3f0262cSandi 779f3f0262cSandi/** 7806c2bb100SAndreas Gohr * Encode ASCII special chars 7816c2bb100SAndreas Gohr * 7826c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7836c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7846c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7856c2bb100SAndreas Gohr * urlencoding!). 7866c2bb100SAndreas Gohr * 7876c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7886c2bb100SAndreas Gohr * 7896c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7906c2bb100SAndreas Gohr * @see rawurldecode() 79142ea7f44SGerrit Uitslag * 79242ea7f44SGerrit Uitslag * @param string $name 79342ea7f44SGerrit Uitslag * @param bool $skip_group 79442ea7f44SGerrit Uitslag * @return string 7956c2bb100SAndreas Gohr */ 796e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 797a424cd8eSchris global $cache_authname; 798a424cd8eSchris $cache =& $cache_authname; 79931784267SAndreas Gohr $name = (string) $name; 800a424cd8eSchris 80180601d26SAndreas Gohr // never encode wildcard FS#1955 80280601d26SAndreas Gohr if($name == '%USER%') return $name; 803b78bf706Sromain if($name == '%GROUP%') return $name; 80480601d26SAndreas Gohr 805a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 806e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 80730f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 80830f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 80930f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 810ab5d26daSAndreas Gohr ); 811e838fc2eSAndreas Gohr } else { 81230f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 81330f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 81430f6faf0SChristopher Smith 'auth_nameencode_callback', $name 815ab5d26daSAndreas Gohr ); 816e838fc2eSAndreas Gohr } 8176c2bb100SAndreas Gohr } 8186c2bb100SAndreas Gohr 819a424cd8eSchris return $cache[$name][$skip_group]; 820a424cd8eSchris} 821a424cd8eSchris 82204d68ae4SGerrit Uitslag/** 82304d68ae4SGerrit Uitslag * callback encodes the matches 82404d68ae4SGerrit Uitslag * 82504d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 82604d68ae4SGerrit Uitslag * @return string 82704d68ae4SGerrit Uitslag */ 82830f6faf0SChristopher Smithfunction auth_nameencode_callback($matches) { 82930f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1],-1))); 83030f6faf0SChristopher Smith} 83130f6faf0SChristopher Smith 8326c2bb100SAndreas Gohr/** 833f3f0262cSandi * Create a pronouncable password 834f3f0262cSandi * 8358a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8368a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8378a285f7fSAndreas Gohr * 83815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 83915fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8408a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 84115fae107Sandi * 8428a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 84315fae107Sandi * @return string pronouncable password 844f3f0262cSandi */ 8458a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 8468a285f7fSAndreas Gohr $data = array( 847d628dcf3SAndreas Gohr 'password' => '', 848d628dcf3SAndreas Gohr 'foruser' => $foruser 8498a285f7fSAndreas Gohr ); 8508a285f7fSAndreas Gohr 8518a285f7fSAndreas Gohr $evt = new Doku_Event('AUTH_PASSWORD_GENERATE', $data); 8528a285f7fSAndreas Gohr if($evt->advise_before(true)) { 853f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 854f3f0262cSandi $v = 'aeiou'; //vowels 855f3f0262cSandi $a = $c.$v; //both 856987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 857f3f0262cSandi 858987c8d26SAndreas Gohr //use thre syllables... 859987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 860483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 861483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 862483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 863f3f0262cSandi } 864987c8d26SAndreas Gohr //... and add a nice number and special 865483b6238SMichael Hamann $data['password'] .= auth_random(10, 99).$s[auth_random(0, strlen($s) - 1)]; 8668a285f7fSAndreas Gohr } 8678a285f7fSAndreas Gohr $evt->advise_after(); 868f3f0262cSandi 8698a285f7fSAndreas Gohr return $data['password']; 870f3f0262cSandi} 871f3f0262cSandi 872f3f0262cSandi/** 873f3f0262cSandi * Sends a password to the given user 874f3f0262cSandi * 87515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 87642ea7f44SGerrit Uitslag * 877ab5d26daSAndreas Gohr * @param string $user Login name of the user 878ab5d26daSAndreas Gohr * @param string $password The new password in clear text 87915fae107Sandi * @return bool true on success 880f3f0262cSandi */ 881f3f0262cSandifunction auth_sendPassword($user, $password) { 882f3f0262cSandi global $lang; 88327058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 884cd52f92dSchris global $auth; 885beca106aSAdrian Lang if(!$auth) return false; 886cd52f92dSchris 887d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8882dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 889f3f0262cSandi 89087ddda95Sandi if(!$userinfo['mail']) return false; 891f3f0262cSandi 892f3f0262cSandi $text = rawLocale('password'); 893d7169d19SAndreas Gohr $trep = array( 894d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 895d7169d19SAndreas Gohr 'LOGIN' => $user, 896d7169d19SAndreas Gohr 'PASSWORD' => $password 897d7169d19SAndreas Gohr ); 898f3f0262cSandi 899d7169d19SAndreas Gohr $mail = new Mailer(); 900d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 901d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 902d7169d19SAndreas Gohr $mail->setBody($text, $trep); 903d7169d19SAndreas Gohr return $mail->send(); 904f3f0262cSandi} 905f3f0262cSandi 906f3f0262cSandi/** 90715fae107Sandi * Register a new user 908f3f0262cSandi * 90915fae107Sandi * This registers a new user - Data is read directly from $_POST 91015fae107Sandi * 91115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 91242ea7f44SGerrit Uitslag * 91315fae107Sandi * @return bool true on success, false on any error 914f3f0262cSandi */ 915f3f0262cSandifunction register() { 916f3f0262cSandi global $lang; 917eb5d07e4Sjan global $conf; 91827058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 919cd52f92dSchris global $auth; 92064273335SAndreas Gohr global $INPUT; 921f3f0262cSandi 92264273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9233a48618aSAnika Henke if(!actionOK('register')) return false; 924640145a5Sandi 92564273335SAndreas Gohr // gather input 92664273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 92764273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 92864273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 92964273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 93064273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 931d752aedeSAndreas Gohr 93264273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 933f3f0262cSandi msg($lang['regmissing'], -1); 934f3f0262cSandi return false; 935f3f0262cSandi } 936f3f0262cSandi 937cab2716aSmatthias.grimm if($conf['autopasswd']) { 9388a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 93964273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 940bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 941cab2716aSmatthias.grimm return false; 94264273335SAndreas Gohr } elseif($pass != $passchk) { 943bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 944cab2716aSmatthias.grimm return false; 945cab2716aSmatthias.grimm } 946cab2716aSmatthias.grimm 947f3f0262cSandi //check mail 94864273335SAndreas Gohr if(!mail_isvalid($email)) { 949f3f0262cSandi msg($lang['regbadmail'], -1); 950f3f0262cSandi return false; 951f3f0262cSandi } 952f3f0262cSandi 953f3f0262cSandi //okay try to create the user 95464273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 955db9faf02SPatrick Brown msg($lang['regfail'], -1); 956f3f0262cSandi return false; 957f3f0262cSandi } 958f3f0262cSandi 959790b7720SAndreas Gohr // send notification about the new user 960790b7720SAndreas Gohr $subscription = new Subscription(); 961790b7720SAndreas Gohr $subscription->send_register($login, $fullname, $email); 96202a498e7Schris 963790b7720SAndreas Gohr // are we done? 964cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 965cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 966cab2716aSmatthias.grimm return true; 967cab2716aSmatthias.grimm } 968cab2716aSmatthias.grimm 969790b7720SAndreas Gohr // autogenerated password? then send password to user 97064273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 971f3f0262cSandi msg($lang['regsuccess'], 1); 972f3f0262cSandi return true; 973f3f0262cSandi } else { 974f3f0262cSandi msg($lang['regmailfail'], -1); 975f3f0262cSandi return false; 976f3f0262cSandi } 977f3f0262cSandi} 978f3f0262cSandi 97910a76f6fSfrank/** 9808b06d178Schris * Update user profile 9818b06d178Schris * 9828b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9838b06d178Schris */ 9848b06d178Schrisfunction updateprofile() { 9858b06d178Schris global $conf; 9868b06d178Schris global $lang; 98727058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 988cd52f92dSchris global $auth; 989bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 990bcc94b2cSAndreas Gohr global $INPUT; 9918b06d178Schris 992bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9931b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9948b06d178Schris 9953a48618aSAnika Henke if(!actionOK('profile')) { 9968b06d178Schris msg($lang['profna'], -1); 9978b06d178Schris return false; 9988b06d178Schris } 9998b06d178Schris 1000bcc94b2cSAndreas Gohr $changes = array(); 1001bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 1002bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 1003bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 1004bcc94b2cSAndreas Gohr 1005bcc94b2cSAndreas Gohr // check misspelled passwords 1006bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 1007bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10088b06d178Schris return false; 10098b06d178Schris } 10108b06d178Schris 10118b06d178Schris // clean fullname and email 1012bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1013bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10148b06d178Schris 1015bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 1016bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 1017bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1018ab5d26daSAndreas Gohr ) { 10198b06d178Schris msg($lang['profnoempty'], -1); 10208b06d178Schris return false; 10218b06d178Schris } 1022bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10238b06d178Schris msg($lang['regbadmail'], -1); 10248b06d178Schris return false; 10258b06d178Schris } 10268b06d178Schris 1027bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10284c21b7eeSAndreas Gohr 1029bcc94b2cSAndreas Gohr // check for unavailable capabilities 1030bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 1031bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 1032bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 1033bcc94b2cSAndreas Gohr 1034bcc94b2cSAndreas Gohr // anything to do? 10358b06d178Schris if(!count($changes)) { 10368b06d178Schris msg($lang['profnochange'], -1); 10378b06d178Schris return false; 10388b06d178Schris } 10398b06d178Schris 10408b06d178Schris if($conf['profileconfirm']) { 1041585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 104271422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10438b06d178Schris return false; 10448b06d178Schris } 10458b06d178Schris } 10468b06d178Schris 1047e6c4392fSPatrick Brown if(!$auth->triggerUserMod('modify', array($INPUT->server->str('REMOTE_USER'), &$changes))) { 1048db9faf02SPatrick Brown msg($lang['proffail'], -1); 1049db9faf02SPatrick Brown return false; 1050db9faf02SPatrick Brown } 1051db9faf02SPatrick Brown 105232ed2b36SAndreas Gohr if($changes['pass']) { 1053*c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1054ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 105504369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1056585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1057*c276e9e8SMarcel Pennewiss } else { 1058*c276e9e8SMarcel Pennewiss // make sure the session is writable 1059*c276e9e8SMarcel Pennewiss @session_start(); 1060*c276e9e8SMarcel Pennewiss // invalidate session cache 1061*c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1062*c276e9e8SMarcel Pennewiss session_write_close(); 106332ed2b36SAndreas Gohr } 1064*c276e9e8SMarcel Pennewiss 106525b2a98cSMichael Klier return true; 1066a0b5b007SChris Smith} 1067ab5d26daSAndreas Gohr 106804d68ae4SGerrit Uitslag/** 106904d68ae4SGerrit Uitslag * Delete the current logged-in user 107004d68ae4SGerrit Uitslag * 107104d68ae4SGerrit Uitslag * @return bool true on success, false on any error 107204d68ae4SGerrit Uitslag */ 10732a7abf2dSChristopher Smithfunction auth_deleteprofile(){ 10742a7abf2dSChristopher Smith global $conf; 10752a7abf2dSChristopher Smith global $lang; 107673012efdSChristopher Smith /* @var DokuWiki_Auth_Plugin $auth */ 10772a7abf2dSChristopher Smith global $auth; 10782a7abf2dSChristopher Smith /* @var Input $INPUT */ 10792a7abf2dSChristopher Smith global $INPUT; 10802a7abf2dSChristopher Smith 10812a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 10822a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 10832a7abf2dSChristopher Smith 10842a7abf2dSChristopher Smith // action prevented or auth module disallows 10852a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10862a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10872a7abf2dSChristopher Smith return false; 10882a7abf2dSChristopher Smith } 10892a7abf2dSChristopher Smith 10902a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 10912a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10922a7abf2dSChristopher Smith return false; 10932a7abf2dSChristopher Smith } 10942a7abf2dSChristopher Smith 10952a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1096585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10972a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10982a7abf2dSChristopher Smith return false; 10992a7abf2dSChristopher Smith } 11002a7abf2dSChristopher Smith } 11012a7abf2dSChristopher Smith 110259bc3b48SGerrit Uitslag $deleted = array(); 1103585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 110473012efdSChristopher Smith if($auth->triggerUserMod('delete', array($deleted))) { 11052a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11062a7abf2dSChristopher Smith auth_logoff(); 11072a7abf2dSChristopher Smith return true; 11082a7abf2dSChristopher Smith } 11092a7abf2dSChristopher Smith 11102a7abf2dSChristopher Smith return false; 11112a7abf2dSChristopher Smith} 11122a7abf2dSChristopher Smith 11138b06d178Schris/** 11148b06d178Schris * Send a new password 11158b06d178Schris * 11161d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11171d5856cfSAndreas Gohr * 11181d5856cfSAndreas Gohr * - handling the first request of password reset 11191d5856cfSAndreas Gohr * - validating the password reset auth token 11201d5856cfSAndreas Gohr * 11218b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11228b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11231d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11248b06d178Schris * 11258b06d178Schris * @return bool true on success, false on any error 11268b06d178Schris */ 11278b06d178Schrisfunction act_resendpwd() { 11288b06d178Schris global $lang; 11298b06d178Schris global $conf; 113027058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1131cd52f92dSchris global $auth; 1132bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1133bcc94b2cSAndreas Gohr global $INPUT; 11348b06d178Schris 11353a48618aSAnika Henke if(!actionOK('resendpwd')) { 11368b06d178Schris msg($lang['resendna'], -1); 11378b06d178Schris return false; 11388b06d178Schris } 11398b06d178Schris 1140bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11418b06d178Schris 11421d5856cfSAndreas Gohr if($token) { 1143cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11441d5856cfSAndreas Gohr 11451d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 114679e79377SAndreas Gohr if(!file_exists($tfile)) { 11471d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1148bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11491d5856cfSAndreas Gohr return false; 11501d5856cfSAndreas Gohr } 11518a9735e3SAndreas Gohr // token is only valid for 3 days 11528a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11538a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1154bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11551d5856cfSAndreas Gohr @unlink($tfile); 11568a9735e3SAndreas Gohr return false; 11578a9735e3SAndreas Gohr } 11588a9735e3SAndreas Gohr 11598b06d178Schris $user = io_readfile($tfile); 11602dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11618b06d178Schris if(!$userinfo['mail']) { 11628b06d178Schris msg($lang['resendpwdnouser'], -1); 11638b06d178Schris return false; 11648b06d178Schris } 11658b06d178Schris 1166cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1167bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1168bcc94b2cSAndreas Gohr 1169cc204bbdSAndreas Gohr // password given correctly? 1170bcc94b2cSAndreas Gohr if(!$pass) return false; 1171bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1172451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1173cc204bbdSAndreas Gohr return false; 1174cc204bbdSAndreas Gohr } 1175cc204bbdSAndreas Gohr 1176bcc94b2cSAndreas Gohr // change it 1177cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1178db9faf02SPatrick Brown msg($lang['proffail'], -1); 1179cc204bbdSAndreas Gohr return false; 1180cc204bbdSAndreas Gohr } 1181cc204bbdSAndreas Gohr 1182cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1183cc204bbdSAndreas Gohr 11848a285f7fSAndreas Gohr $pass = auth_pwgen($user); 11857d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1186db9faf02SPatrick Brown msg($lang['proffail'], -1); 11878b06d178Schris return false; 11888b06d178Schris } 11898b06d178Schris 11908b06d178Schris if(auth_sendPassword($user, $pass)) { 11918b06d178Schris msg($lang['resendpwdsuccess'], 1); 11928b06d178Schris } else { 11938b06d178Schris msg($lang['regmailfail'], -1); 11948b06d178Schris } 1195cc204bbdSAndreas Gohr } 1196cc204bbdSAndreas Gohr 1197cc204bbdSAndreas Gohr @unlink($tfile); 11988b06d178Schris return true; 11991d5856cfSAndreas Gohr 12001d5856cfSAndreas Gohr } else { 12011d5856cfSAndreas Gohr // we're in request phase 12021d5856cfSAndreas Gohr 1203bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 12041d5856cfSAndreas Gohr 1205bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 12061d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12071d5856cfSAndreas Gohr return false; 12081d5856cfSAndreas Gohr } else { 1209bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12101d5856cfSAndreas Gohr } 12111d5856cfSAndreas Gohr 12122dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 12131d5856cfSAndreas Gohr if(!$userinfo['mail']) { 12141d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12151d5856cfSAndreas Gohr return false; 12161d5856cfSAndreas Gohr } 12171d5856cfSAndreas Gohr 12181d5856cfSAndreas Gohr // generate auth token 1219483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12201d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 12211d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 12221d5856cfSAndreas Gohr 12231d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12241d5856cfSAndreas Gohr 12251d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1226d7169d19SAndreas Gohr $trep = array( 1227d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1228d7169d19SAndreas Gohr 'LOGIN' => $user, 1229d7169d19SAndreas Gohr 'CONFIRM' => $url 1230d7169d19SAndreas Gohr ); 12311d5856cfSAndreas Gohr 1232d7169d19SAndreas Gohr $mail = new Mailer(); 1233d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1234d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1235d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1236d7169d19SAndreas Gohr if($mail->send()) { 12371d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12381d5856cfSAndreas Gohr } else { 12391d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12401d5856cfSAndreas Gohr } 12411d5856cfSAndreas Gohr return true; 12421d5856cfSAndreas Gohr } 1243ab5d26daSAndreas Gohr // never reached 12448b06d178Schris} 12458b06d178Schris 12468b06d178Schris/** 1247b0855b11Sandi * Encrypts a password using the given method and salt 1248b0855b11Sandi * 1249b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1250b0855b11Sandi * is chosen. 1251b0855b11Sandi * 1252b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 125342ea7f44SGerrit Uitslag * 1254ab5d26daSAndreas Gohr * @param string $clear The clear text password 1255ab5d26daSAndreas Gohr * @param string $method The hashing method 1256ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1257b0855b11Sandi * @return string The crypted password 1258b0855b11Sandi */ 1259577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1260b0855b11Sandi global $conf; 1261b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 126210a76f6fSfrank 12633a0a2d05SAndreas Gohr $pass = new PassHash(); 12643a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1265b0855b11Sandi 12663a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1267b0855b11Sandi msg("Unsupported crypt method $method", -1); 12683a0a2d05SAndreas Gohr return false; 1269b0855b11Sandi } 12703a0a2d05SAndreas Gohr 12713a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1272b0855b11Sandi} 1273b0855b11Sandi 1274b0855b11Sandi/** 1275b0855b11Sandi * Verifies a cleartext password against a crypted hash 1276b0855b11Sandi * 1277b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 127842ea7f44SGerrit Uitslag * 1279ab5d26daSAndreas Gohr * @param string $clear The clear text password 1280ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1281ab5d26daSAndreas Gohr * @return bool true if both match 1282b0855b11Sandi */ 1283b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 12843a0a2d05SAndreas Gohr $pass = new PassHash(); 12853a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1286b0855b11Sandi} 1287340756e4Sandi 1288a0b5b007SChris Smith/** 1289a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1290a0b5b007SChris Smith * 1291a0b5b007SChris Smith * @param string $user username 1292a0b5b007SChris Smith * @param string $pass encrypted password 1293a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1294ab5d26daSAndreas Gohr * @return bool 1295a0b5b007SChris Smith */ 1296a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1297a0b5b007SChris Smith global $conf; 129827058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1299a0b5b007SChris Smith global $auth; 130079d00841SOliver Geisen global $USERINFO; 1301a0b5b007SChris Smith 1302beca106aSAdrian Lang if(!$auth) return false; 1303a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1304a0b5b007SChris Smith 1305a0b5b007SChris Smith // set cookie 1306645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 130773ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1308c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 130973ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 131055a71a16SGerrit Uitslag 1311a0b5b007SChris Smith // set session 1312a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1313234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1314a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1315a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1316a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1317ab5d26daSAndreas Gohr 1318ab5d26daSAndreas Gohr return true; 1319a0b5b007SChris Smith} 1320a0b5b007SChris Smith 1321645c0a36SAndreas Gohr/** 1322645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1323645c0a36SAndreas Gohr * 1324645c0a36SAndreas Gohr * @returns array 1325645c0a36SAndreas Gohr */ 1326645c0a36SAndreas Gohrfunction auth_getCookie() { 1327c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1328c66972f2SAdrian Lang return array(null, null, null); 1329c66972f2SAdrian Lang } 1330645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1331645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1332645c0a36SAndreas Gohr $pass = base64_decode($pass); 1333645c0a36SAndreas Gohr $user = base64_decode($user); 1334645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1335645c0a36SAndreas Gohr} 1336645c0a36SAndreas Gohr 1337e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1338