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; 549c29eea5SJan Schumann } 559c29eea5SJan Schumann } 568b06d178Schris 576416b708SMichael Hamann if(!isset($auth) || !$auth){ 583094e817SAndreas Gohr msg($lang['authtempfail'], -1); 593094e817SAndreas Gohr return false; 603094e817SAndreas Gohr } 618b06d178Schris 626416b708SMichael Hamann if ($auth->success == false) { 630f4f4adfSAndreas Gohr // degrade to unauthenticated user 64d2dde4ebSMatthias Grimm unset($auth); 650f4f4adfSAndreas Gohr auth_logoff(); 66cd52f92dSchris msg($lang['authtempfail'], -1); 676416b708SMichael Hamann return false; 68d2dde4ebSMatthias Grimm } 6916905344SAndreas Gohr 7016905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 71bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 72bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 73bbbd6568SAndreas Gohr 74b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 75b2665af7SMichael Hamann // the one presented at 76b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 77b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 78b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 79b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 80528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 8106156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 82528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 83528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 84528ddc7cSAndreas Gohr } 85528ddc7cSAndreas Gohr 861e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 87bcc94b2cSAndreas Gohr if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 88bcc94b2cSAndreas Gohr $INPUT->set('u', $_SERVER['PHP_AUTH_USER']); 89bcc94b2cSAndreas Gohr $INPUT->set('p', $_SERVER['PHP_AUTH_PW']); 90bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 911e8c9c90SAndreas Gohr } 921e8c9c90SAndreas Gohr 93395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 9493a7873eSAndreas Gohr if (true === $auth->success) { 95395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 96395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 97f4476bd9SJan Schumann } 98191bb90aSAndreas Gohr 998eca974cSAndreas Gohr if(!is_null($auth) && $auth->canDo('external')) { 100f13fa892SAndreas Gohr // external trust mechanism in place 101bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 102f5cb575dSAndreas Gohr } else { 1036080c584SRobin Gareus $evdata = array( 104bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 105bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 106bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 107bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1086080c584SRobin Gareus ); 109b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 110f5cb575dSAndreas Gohr } 111f5cb575dSAndreas Gohr 11216905344SAndreas Gohr //load ACL into a global array XXX 11375c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 114ab5d26daSAndreas Gohr 115ab5d26daSAndreas Gohr return true; 11675c93b77SAndreas Gohr} 11775c93b77SAndreas Gohr 11875c93b77SAndreas Gohr/** 11975c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12075c93b77SAndreas Gohr * 12175c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12242ea7f44SGerrit Uitslag * 123ab5d26daSAndreas Gohr * @return array 12475c93b77SAndreas Gohr */ 12575c93b77SAndreas Gohrfunction auth_loadACL() { 12675c93b77SAndreas Gohr global $config_cascade; 127b78bf706Sromain global $USERINFO; 128585bf44eSChristopher Smith /* @var Input $INPUT */ 129585bf44eSChristopher Smith global $INPUT; 13075c93b77SAndreas Gohr 13175c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 13275c93b77SAndreas Gohr 13375c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13475c93b77SAndreas Gohr 13532e82180SAndreas Gohr $out = array(); 1369ce556d2SAndreas Gohr foreach($acl as $line) { 1379ce556d2SAndreas Gohr $line = trim($line); 138443e135dSChristopher Smith if(empty($line) || ($line{0} == '#')) continue; // skip blank lines & comments 13921c3090aSChristopher Smith list($id,$rest) = preg_split('/[ \t]+/',$line,2); 14032e82180SAndreas Gohr 141443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 142ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 143ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 144585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 145ad3d68d7SChristopher Smith 146585bf44eSChristopher Smith $id = str_replace('%USER%',cleanID($INPUT->server->str('REMOTE_USER')),$id); 147585bf44eSChristopher Smith $rest = str_replace('%USER%',auth_nameencode($INPUT->server->str('REMOTE_USER')),$rest); 148ad3d68d7SChristopher Smith } 149ad3d68d7SChristopher Smith 150ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1519ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 152ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 1539ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 154b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 15532e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 15632e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 157b78bf706Sromain } 15832e82180SAndreas Gohr } else { 15932e82180SAndreas Gohr $out[] = "$id\t$rest"; 160a8fe108bSGuy Brand } 16111799630Sandi } 1629ce556d2SAndreas Gohr 16332e82180SAndreas Gohr return $out; 164f3f0262cSandi} 165f3f0262cSandi 166ab5d26daSAndreas Gohr/** 167ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 168ab5d26daSAndreas Gohr * 16942ea7f44SGerrit Uitslag * @param array $evdata 170ab5d26daSAndreas Gohr * @return bool 171ab5d26daSAndreas Gohr */ 172b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 173ab5d26daSAndreas Gohr return auth_login( 174ab5d26daSAndreas Gohr $evdata['user'], 175b5ee21aaSAdrian Lang $evdata['password'], 176b5ee21aaSAdrian Lang $evdata['sticky'], 177ab5d26daSAndreas Gohr $evdata['silent'] 178ab5d26daSAndreas Gohr ); 179b5ee21aaSAdrian Lang} 180b5ee21aaSAdrian Lang 181f3f0262cSandi/** 182f3f0262cSandi * This tries to login the user based on the sent auth credentials 183f3f0262cSandi * 184f3f0262cSandi * The authentication works like this: if a username was given 18515fae107Sandi * a new login is assumed and user/password are checked. If they 18615fae107Sandi * are correct the password is encrypted with blowfish and stored 18715fae107Sandi * together with the username in a cookie - the same info is stored 18815fae107Sandi * in the session, too. Additonally a browserID is stored in the 18915fae107Sandi * session. 19015fae107Sandi * 19115fae107Sandi * If no username was given the cookie is checked: if the username, 19215fae107Sandi * crypted password and browserID match between session and cookie 19315fae107Sandi * no further testing is done and the user is accepted 19415fae107Sandi * 19515fae107Sandi * If a cookie was found but no session info was availabe the 196136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 19715fae107Sandi * together with username rechecked by calling this function again. 198f3f0262cSandi * 199f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 200f3f0262cSandi * are set. 20115fae107Sandi * 20215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20315fae107Sandi * 20415fae107Sandi * @param string $user Username 20515fae107Sandi * @param string $pass Cleartext Password 20615fae107Sandi * @param bool $sticky Cookie should not expire 207f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 20815fae107Sandi * @return bool true on successful auth 209f3f0262cSandi */ 210f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 211f3f0262cSandi global $USERINFO; 212f3f0262cSandi global $conf; 213f3f0262cSandi global $lang; 21427058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 215cd52f92dSchris global $auth; 216585bf44eSChristopher Smith /* @var Input $INPUT */ 217585bf44eSChristopher Smith global $INPUT; 218ab5d26daSAndreas Gohr 219132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 220f3f0262cSandi 221beca106aSAdrian Lang if(!$auth) return false; 222beca106aSAdrian Lang 223bbbd6568SAndreas Gohr if(!empty($user)) { 224132bdbfeSandi //usual login 2255e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 226132bdbfeSandi // make logininfo globally available 227585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 22830d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 22904369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 230132bdbfeSandi return true; 231f3f0262cSandi } else { 232f3f0262cSandi //invalid credentials - log off 233f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 234f3f0262cSandi auth_logoff(); 235132bdbfeSandi return false; 236f3f0262cSandi } 237f3f0262cSandi } else { 238132bdbfeSandi // read cookie information 239645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 240132bdbfeSandi if($user && $pass) { 241132bdbfeSandi // we got a cookie - see if we can trust it 242fa7c70ffSAdrian Lang 243fa7c70ffSAdrian Lang // get session info 244fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 245132bdbfeSandi if(isset($session) && 2467172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2474c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 248132bdbfeSandi ($session['user'] == $user) && 249234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 250ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 251ab5d26daSAndreas Gohr ) { 252234ce57eSAndreas Gohr 253132bdbfeSandi // he has session, cookie and browser right - let him in 254585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 255132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 256132bdbfeSandi return true; 257132bdbfeSandi } 258f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 25930d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26004369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 261f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 262132bdbfeSandi } 263132bdbfeSandi } 264f3f0262cSandi //just to be sure 265883179a4SAndreas Gohr auth_logoff(true); 266132bdbfeSandi return false; 267f3f0262cSandi} 268132bdbfeSandi 269132bdbfeSandi/** 270136ce040Sandi * Builds a pseudo UID from browser and IP data 271132bdbfeSandi * 272132bdbfeSandi * This is neither unique nor unfakable - still it adds some 273136ce040Sandi * security. Using the first part of the IP makes sure 27480b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 27515fae107Sandi * 27615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 27715fae107Sandi * 27815fae107Sandi * @return string a MD5 sum of various browser headers 279132bdbfeSandi */ 280132bdbfeSandifunction auth_browseruid() { 281585bf44eSChristopher Smith /* @var Input $INPUT */ 282585bf44eSChristopher Smith global $INPUT; 283585bf44eSChristopher Smith 2842f9daf16SAndreas Gohr $ip = clientIP(true); 285132bdbfeSandi $uid = ''; 286585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_USER_AGENT'); 287585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_CHARSET'); 2882f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 28980b4f376SAndreas Gohr $uid = strtolower($uid); 290132bdbfeSandi return md5($uid); 291132bdbfeSandi} 292132bdbfeSandi 293132bdbfeSandi/** 294132bdbfeSandi * Creates a random key to encrypt the password in cookies 29515fae107Sandi * 29615fae107Sandi * This function tries to read the password for encrypting 29798407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 29815fae107Sandi * if no such file is found a random key is created and 29915fae107Sandi * and stored in this file. 30015fae107Sandi * 30115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30242ea7f44SGerrit Uitslag * 30332ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 30430d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 30515fae107Sandi * @return string 306132bdbfeSandi */ 30730d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 308132bdbfeSandi global $conf; 30998407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 31030d544a4SMichael Hamann if ($secure || !file_exists($file)) { 31130d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 31230d544a4SMichael Hamann } 313132bdbfeSandi $salt = io_readFile($file); 314132bdbfeSandi if(empty($salt)) { 31530d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 316132bdbfeSandi io_saveFile($file, $salt); 317132bdbfeSandi } 31832ed2b36SAndreas Gohr if($addsession) { 31932ed2b36SAndreas Gohr $salt .= session_id(); 32032ed2b36SAndreas Gohr } 321132bdbfeSandi return $salt; 322f3f0262cSandi} 323f3f0262cSandi 324f3f0262cSandi/** 325483b6238SMichael Hamann * Return truly (pseudo) random bytes if available, otherwise fall back to mt_rand 326483b6238SMichael Hamann * 327483b6238SMichael Hamann * @author Mark Seecof 328483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 329*59752844SAnders Sandblad * @link http://php.net/manual/de/function.mt-rand.php#83655 33042ea7f44SGerrit Uitslag * 331483b6238SMichael Hamann * @param int $length number of bytes to get 332483b6238SMichael Hamann * @return string binary random strings 333483b6238SMichael Hamann */ 334483b6238SMichael Hamannfunction auth_randombytes($length) { 335483b6238SMichael Hamann $strong = false; 336483b6238SMichael Hamann $rbytes = false; 337483b6238SMichael Hamann 338483b6238SMichael Hamann if (function_exists('openssl_random_pseudo_bytes') 339483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.4') >= 0 340483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 341483b6238SMichael Hamann ) { 342483b6238SMichael Hamann $rbytes = openssl_random_pseudo_bytes($length, $strong); 343483b6238SMichael Hamann } 344483b6238SMichael Hamann 345483b6238SMichael Hamann if (!$strong && function_exists('mcrypt_create_iv') 346483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.7') >= 0 347483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 348483b6238SMichael Hamann ) { 349483b6238SMichael Hamann $rbytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); 350483b6238SMichael Hamann if ($rbytes !== false && strlen($rbytes) === $length) { 351483b6238SMichael Hamann $strong = true; 352483b6238SMichael Hamann } 353483b6238SMichael Hamann } 354483b6238SMichael Hamann 355483b6238SMichael Hamann // If no strong randoms available, try OS the specific ways 356483b6238SMichael Hamann if(!$strong) { 357483b6238SMichael Hamann // Unix/Linux platform 358483b6238SMichael Hamann $fp = @fopen('/dev/urandom', 'rb'); 359483b6238SMichael Hamann if($fp !== false) { 360483b6238SMichael Hamann $rbytes = fread($fp, $length); 361483b6238SMichael Hamann fclose($fp); 362483b6238SMichael Hamann } 363483b6238SMichael Hamann 364483b6238SMichael Hamann // MS-Windows platform 365483b6238SMichael Hamann if(class_exists('COM')) { 366483b6238SMichael Hamann // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx 367483b6238SMichael Hamann try { 368483b6238SMichael Hamann $CAPI_Util = new COM('CAPICOM.Utilities.1'); 369483b6238SMichael Hamann $rbytes = $CAPI_Util->GetRandom($length, 0); 370483b6238SMichael Hamann 371483b6238SMichael Hamann // if we ask for binary data PHP munges it, so we 372483b6238SMichael Hamann // request base64 return value. 373483b6238SMichael Hamann if($rbytes) $rbytes = base64_decode($rbytes); 374483b6238SMichael Hamann } catch(Exception $ex) { 375483b6238SMichael Hamann // fail 376483b6238SMichael Hamann } 377483b6238SMichael Hamann } 378483b6238SMichael Hamann } 379483b6238SMichael Hamann if(strlen($rbytes) < $length) $rbytes = false; 380483b6238SMichael Hamann 381483b6238SMichael Hamann // still no random bytes available - fall back to mt_rand() 382483b6238SMichael Hamann if($rbytes === false) { 383483b6238SMichael Hamann $rbytes = ''; 384483b6238SMichael Hamann for ($i = 0; $i < $length; ++$i) { 385483b6238SMichael Hamann $rbytes .= chr(mt_rand(0, 255)); 386483b6238SMichael Hamann } 387483b6238SMichael Hamann } 388483b6238SMichael Hamann 389483b6238SMichael Hamann return $rbytes; 390483b6238SMichael Hamann} 391483b6238SMichael Hamann 392483b6238SMichael Hamann/** 393483b6238SMichael Hamann * Random number generator using the best available source 394483b6238SMichael Hamann * 395483b6238SMichael Hamann * @author Michael Samuel 396483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 39742ea7f44SGerrit Uitslag * 398483b6238SMichael Hamann * @param int $min 399483b6238SMichael Hamann * @param int $max 400483b6238SMichael Hamann * @return int 401483b6238SMichael Hamann */ 402483b6238SMichael Hamannfunction auth_random($min, $max) { 403483b6238SMichael Hamann $abs_max = $max - $min; 404483b6238SMichael Hamann 405483b6238SMichael Hamann $nbits = 0; 406483b6238SMichael Hamann for ($n = $abs_max; $n > 0; $n >>= 1) { 407483b6238SMichael Hamann ++$nbits; 408483b6238SMichael Hamann } 409483b6238SMichael Hamann 410483b6238SMichael Hamann $mask = (1 << $nbits) - 1; 411483b6238SMichael Hamann do { 412483b6238SMichael Hamann $bytes = auth_randombytes(PHP_INT_SIZE); 413483b6238SMichael Hamann $integers = unpack('Inum', $bytes); 414483b6238SMichael Hamann $integer = $integers["num"] & $mask; 415483b6238SMichael Hamann } while ($integer > $abs_max); 416483b6238SMichael Hamann 417483b6238SMichael Hamann return $min + $integer; 418483b6238SMichael Hamann} 419483b6238SMichael Hamann 420483b6238SMichael Hamann/** 42104369c3eSMichael Hamann * Encrypt data using the given secret using AES 42204369c3eSMichael Hamann * 42304369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 42404369c3eSMichael Hamann * using pbkdf2. 42504369c3eSMichael Hamann * 42604369c3eSMichael Hamann * @param string $data The data that shall be encrypted 42704369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 42804369c3eSMichael Hamann * @return string The ciphertext 42904369c3eSMichael Hamann */ 43004369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 43104369c3eSMichael Hamann $iv = auth_randombytes(16); 43204369c3eSMichael Hamann $cipher = new Crypt_AES(); 43304369c3eSMichael Hamann $cipher->setPassword($secret); 43404369c3eSMichael Hamann 4357b650cefSMichael Hamann /* 4367b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 4377b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 4387b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 4397b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 4407b650cefSMichael Hamann */ 44104369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 44204369c3eSMichael Hamann} 44304369c3eSMichael Hamann 44404369c3eSMichael Hamann/** 44504369c3eSMichael Hamann * Decrypt the given AES ciphertext 44604369c3eSMichael Hamann * 44704369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 44804369c3eSMichael Hamann * 44904369c3eSMichael Hamann * @param string $ciphertext The encrypted data 45004369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 45104369c3eSMichael Hamann * @return string The decrypted data 45204369c3eSMichael Hamann */ 45304369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 4547b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 45504369c3eSMichael Hamann $cipher = new Crypt_AES(); 45604369c3eSMichael Hamann $cipher->setPassword($secret); 4577b650cefSMichael Hamann $cipher->setIV($iv); 45804369c3eSMichael Hamann 4597b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 46004369c3eSMichael Hamann} 46104369c3eSMichael Hamann 46204369c3eSMichael Hamann/** 463883179a4SAndreas Gohr * Log out the current user 464883179a4SAndreas Gohr * 465f3f0262cSandi * This clears all authentication data and thus log the user 466883179a4SAndreas Gohr * off. It also clears session data. 46715fae107Sandi * 46815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 46942ea7f44SGerrit Uitslag * 470883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 471f3f0262cSandi */ 472883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 473f3f0262cSandi global $conf; 474f3f0262cSandi global $USERINFO; 47527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 4765298a619SAndreas Gohr global $auth; 477585bf44eSChristopher Smith /* @var Input $INPUT */ 478585bf44eSChristopher Smith global $INPUT; 47937065e65Sandi 480d4869846SAndreas Gohr // make sure the session is writable (it usually is) 481e9621d07SAndreas Gohr @session_start(); 482e9621d07SAndreas Gohr 483e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 484e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 485e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 486e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 487e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 488e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 489883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 490e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 491585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 492132bdbfeSandi $USERINFO = null; //FIXME 493f5c6743cSAndreas Gohr 49473ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 49573ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 4965298a619SAndreas Gohr 497880f62faSAndreas Gohr if($auth) $auth->logOff(); 498f3f0262cSandi} 499f3f0262cSandi 500f3f0262cSandi/** 501f8cc712eSAndreas Gohr * Check if a user is a manager 502f8cc712eSAndreas Gohr * 503f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 504f8cc712eSAndreas Gohr * user. 505f8cc712eSAndreas Gohr * 506f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 507f8cc712eSAndreas Gohr * 508f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 509f8cc712eSAndreas Gohr * @see auth_isadmin 51042ea7f44SGerrit Uitslag * 511ab5d26daSAndreas Gohr * @param string $user Username 512ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 513ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 514ab5d26daSAndreas Gohr * @return bool 515f8cc712eSAndreas Gohr */ 516f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 517f8cc712eSAndreas Gohr global $conf; 518f8cc712eSAndreas Gohr global $USERINFO; 51927058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 520d752aedeSAndreas Gohr global $auth; 521585bf44eSChristopher Smith /* @var Input $INPUT */ 522585bf44eSChristopher Smith global $INPUT; 523585bf44eSChristopher Smith 524f8cc712eSAndreas Gohr 525beca106aSAdrian Lang if(!$auth) return false; 526c66972f2SAdrian Lang if(is_null($user)) { 527585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 528c66972f2SAdrian Lang return false; 529c66972f2SAdrian Lang } else { 530585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 531c66972f2SAdrian Lang } 532c66972f2SAdrian Lang } 533d6dc956fSAndreas Gohr if(is_null($groups)) { 534d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 535e259aa79SAndreas Gohr } 536e259aa79SAndreas Gohr 537d6dc956fSAndreas Gohr // check superuser match 538d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 539d6dc956fSAndreas Gohr if($adminonly) return false; 540e259aa79SAndreas Gohr // check managers 541d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 54200ce12daSChris Smith 543f8cc712eSAndreas Gohr return false; 544f8cc712eSAndreas Gohr} 545f8cc712eSAndreas Gohr 546f8cc712eSAndreas Gohr/** 547f8cc712eSAndreas Gohr * Check if a user is admin 548f8cc712eSAndreas Gohr * 549f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 550f8cc712eSAndreas Gohr * 551f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 552f8cc712eSAndreas Gohr * 553f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 554ab5d26daSAndreas Gohr * @see auth_ismanager() 55542ea7f44SGerrit Uitslag * 556ab5d26daSAndreas Gohr * @param string $user Username 557ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 558ab5d26daSAndreas Gohr * @return bool 559f8cc712eSAndreas Gohr */ 560f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 561f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 562f8cc712eSAndreas Gohr} 563f8cc712eSAndreas Gohr 564d6dc956fSAndreas Gohr/** 565d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 566d6dc956fSAndreas Gohr * users and groups to determine membership status 567d6dc956fSAndreas Gohr * 568d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 569d6dc956fSAndreas Gohr * 57042ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 57142ea7f44SGerrit Uitslag * @param string $user user to match against 57242ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5735446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 574d6dc956fSAndreas Gohr */ 575d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 57627058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 577d6dc956fSAndreas Gohr global $auth; 578d6dc956fSAndreas Gohr if(!$auth) return false; 579d6dc956fSAndreas Gohr 580d6dc956fSAndreas Gohr // clean user and groups 5814f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 582d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 583d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 584d6dc956fSAndreas Gohr } 585d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 586d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 587d6dc956fSAndreas Gohr 588d6dc956fSAndreas Gohr // extract the memberlist 589d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 590d6dc956fSAndreas Gohr $members = array_map('trim', $members); 591d6dc956fSAndreas Gohr $members = array_unique($members); 592d6dc956fSAndreas Gohr $members = array_filter($members); 593d6dc956fSAndreas Gohr 594d6dc956fSAndreas Gohr // compare cleaned values 595d6dc956fSAndreas Gohr foreach($members as $member) { 596e5204a12SJurgen Hart if($member == '@ALL' ) return true; 5974f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 598d6dc956fSAndreas Gohr if($member[0] == '@') { 599d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 600d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 601d6dc956fSAndreas Gohr } else { 602d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 603d6dc956fSAndreas Gohr if($member == $user) return true; 604d6dc956fSAndreas Gohr } 605d6dc956fSAndreas Gohr } 606d6dc956fSAndreas Gohr 607d6dc956fSAndreas Gohr // still here? not a member! 608d6dc956fSAndreas Gohr return false; 609d6dc956fSAndreas Gohr} 610d6dc956fSAndreas Gohr 611f8cc712eSAndreas Gohr/** 61215fae107Sandi * Convinience function for auth_aclcheck() 61315fae107Sandi * 61415fae107Sandi * This checks the permissions for the current user 61515fae107Sandi * 61615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 61715fae107Sandi * 6181698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 61915fae107Sandi * @return int permission level 620f3f0262cSandi */ 621f3f0262cSandifunction auth_quickaclcheck($id) { 622f3f0262cSandi global $conf; 623f3f0262cSandi global $USERINFO; 624585bf44eSChristopher Smith /* @var Input $INPUT */ 625585bf44eSChristopher Smith global $INPUT; 626f3f0262cSandi # if no ACL is used always return upload rights 627f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 628585bf44eSChristopher Smith return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 629f3f0262cSandi} 630f3f0262cSandi 631f3f0262cSandi/** 632c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 63315fae107Sandi * 63415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 63542ea7f44SGerrit Uitslag * 636c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6371698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 63815fae107Sandi * @param string $user Username 6393272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 64015fae107Sandi * @return int permission level 641f3f0262cSandi */ 642f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 643c17acc9fSAndreas Gohr $data = array( 644c17acc9fSAndreas Gohr 'id' => $id, 645c17acc9fSAndreas Gohr 'user' => $user, 646c17acc9fSAndreas Gohr 'groups' => $groups 647c17acc9fSAndreas Gohr ); 648c17acc9fSAndreas Gohr 649c17acc9fSAndreas Gohr return trigger_event('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 650c17acc9fSAndreas Gohr} 651c17acc9fSAndreas Gohr 652c17acc9fSAndreas Gohr/** 653c17acc9fSAndreas Gohr * default ACL check method 654c17acc9fSAndreas Gohr * 655c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 656c17acc9fSAndreas Gohr * 657c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 65842ea7f44SGerrit Uitslag * 659c17acc9fSAndreas Gohr * @param array $data event data 660c17acc9fSAndreas Gohr * @return int permission level 661c17acc9fSAndreas Gohr */ 662c17acc9fSAndreas Gohrfunction auth_aclcheck_cb($data) { 663c17acc9fSAndreas Gohr $id =& $data['id']; 664c17acc9fSAndreas Gohr $user =& $data['user']; 665c17acc9fSAndreas Gohr $groups =& $data['groups']; 666c17acc9fSAndreas Gohr 667f3f0262cSandi global $conf; 668f3f0262cSandi global $AUTH_ACL; 66927058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 670d752aedeSAndreas Gohr global $auth; 671f3f0262cSandi 67285d03f68SAndreas Gohr // if no ACL is used always return upload rights 673f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 674beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 675f3f0262cSandi 676074cf26bSandi //make sure groups is an array 677074cf26bSandi if(!is_array($groups)) $groups = array(); 678074cf26bSandi 67985d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 680ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 681ab5d26daSAndreas Gohr return AUTH_ADMIN; 682ab5d26daSAndreas Gohr } 68385d03f68SAndreas Gohr 684eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 685eb3ce0d5SKazutaka Miyasaka $user = utf8_strtolower($user); 686eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 687eb3ce0d5SKazutaka Miyasaka } 68837ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 689d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 69085d03f68SAndreas Gohr 6916c2bb100SAndreas Gohr //prepend groups with @ and nameencode 69237ff2261SSascha Klopp foreach($groups as &$group) { 69337ff2261SSascha Klopp $group = '@'.auth_nameencode($group); 69410a76f6fSfrank } 69510a76f6fSfrank 696f3f0262cSandi $ns = getNS($id); 697f3f0262cSandi $perm = -1; 698f3f0262cSandi 699f3f0262cSandi //add ALL group 700f3f0262cSandi $groups[] = '@ALL'; 70137ff2261SSascha Klopp 702f3f0262cSandi //add User 70334aeb4afSAndreas Gohr if($user) $groups[] = $user; 704f3f0262cSandi 705f3f0262cSandi //check exact match first 70621c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 707f3f0262cSandi if(count($matches)) { 708f3f0262cSandi foreach($matches as $match) { 709f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 71021c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 711eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 712eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 713eb3ce0d5SKazutaka Miyasaka } 71448d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 71548d7b7a6SDominik Eckelmann continue; 71648d7b7a6SDominik Eckelmann } 7178ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 718f3f0262cSandi if($acl[2] > $perm) { 719f3f0262cSandi $perm = $acl[2]; 720f3f0262cSandi } 721f3f0262cSandi } 722f3f0262cSandi if($perm > -1) { 723f3f0262cSandi //we had a match - return it 724def492a2SGuillaume Turri return (int) $perm; 725f3f0262cSandi } 726f3f0262cSandi } 727f3f0262cSandi 728f3f0262cSandi //still here? do the namespace checks 729f3f0262cSandi if($ns) { 7303e304b55SMichael Hamann $path = $ns.':*'; 731f3f0262cSandi } else { 7323e304b55SMichael Hamann $path = '*'; //root document 733f3f0262cSandi } 734f3f0262cSandi 735f3f0262cSandi do { 73621c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 737f3f0262cSandi if(count($matches)) { 738f3f0262cSandi foreach($matches as $match) { 739f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 74021c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 741eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 742eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 743eb3ce0d5SKazutaka Miyasaka } 74448d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 74548d7b7a6SDominik Eckelmann continue; 74648d7b7a6SDominik Eckelmann } 7478ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 748f3f0262cSandi if($acl[2] > $perm) { 749f3f0262cSandi $perm = $acl[2]; 750f3f0262cSandi } 751f3f0262cSandi } 752f3f0262cSandi //we had a match - return it 75348d7b7a6SDominik Eckelmann if($perm != -1) { 754def492a2SGuillaume Turri return (int) $perm; 755f3f0262cSandi } 75648d7b7a6SDominik Eckelmann } 757f3f0262cSandi //get next higher namespace 758f3f0262cSandi $ns = getNS($ns); 759f3f0262cSandi 7603e304b55SMichael Hamann if($path != '*') { 7613e304b55SMichael Hamann $path = $ns.':*'; 7623e304b55SMichael Hamann if($path == ':*') $path = '*'; 763f3f0262cSandi } else { 764f3f0262cSandi //we did this already 765f3f0262cSandi //looks like there is something wrong with the ACL 766f3f0262cSandi //break here 767d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 768d5ce66f6SAndreas Gohr return AUTH_NONE; 769f3f0262cSandi } 770f3f0262cSandi } while(1); //this should never loop endless 771ab5d26daSAndreas Gohr return AUTH_NONE; 772f3f0262cSandi} 773f3f0262cSandi 774f3f0262cSandi/** 7756c2bb100SAndreas Gohr * Encode ASCII special chars 7766c2bb100SAndreas Gohr * 7776c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7786c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7796c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7806c2bb100SAndreas Gohr * urlencoding!). 7816c2bb100SAndreas Gohr * 7826c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7836c2bb100SAndreas Gohr * 7846c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7856c2bb100SAndreas Gohr * @see rawurldecode() 78642ea7f44SGerrit Uitslag * 78742ea7f44SGerrit Uitslag * @param string $name 78842ea7f44SGerrit Uitslag * @param bool $skip_group 78942ea7f44SGerrit Uitslag * @return string 7906c2bb100SAndreas Gohr */ 791e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 792a424cd8eSchris global $cache_authname; 793a424cd8eSchris $cache =& $cache_authname; 79431784267SAndreas Gohr $name = (string) $name; 795a424cd8eSchris 79680601d26SAndreas Gohr // never encode wildcard FS#1955 79780601d26SAndreas Gohr if($name == '%USER%') return $name; 798b78bf706Sromain if($name == '%GROUP%') return $name; 79980601d26SAndreas Gohr 800a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 801e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 80230f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 80330f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 80430f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 805ab5d26daSAndreas Gohr ); 806e838fc2eSAndreas Gohr } else { 80730f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 80830f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 80930f6faf0SChristopher Smith 'auth_nameencode_callback', $name 810ab5d26daSAndreas Gohr ); 811e838fc2eSAndreas Gohr } 8126c2bb100SAndreas Gohr } 8136c2bb100SAndreas Gohr 814a424cd8eSchris return $cache[$name][$skip_group]; 815a424cd8eSchris} 816a424cd8eSchris 81704d68ae4SGerrit Uitslag/** 81804d68ae4SGerrit Uitslag * callback encodes the matches 81904d68ae4SGerrit Uitslag * 82004d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 82104d68ae4SGerrit Uitslag * @return string 82204d68ae4SGerrit Uitslag */ 82330f6faf0SChristopher Smithfunction auth_nameencode_callback($matches) { 82430f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1],-1))); 82530f6faf0SChristopher Smith} 82630f6faf0SChristopher Smith 8276c2bb100SAndreas Gohr/** 828f3f0262cSandi * Create a pronouncable password 829f3f0262cSandi * 8308a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8318a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8328a285f7fSAndreas Gohr * 83315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 83415fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8358a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 83615fae107Sandi * 8378a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 83815fae107Sandi * @return string pronouncable password 839f3f0262cSandi */ 8408a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 8418a285f7fSAndreas Gohr $data = array( 842d628dcf3SAndreas Gohr 'password' => '', 843d628dcf3SAndreas Gohr 'foruser' => $foruser 8448a285f7fSAndreas Gohr ); 8458a285f7fSAndreas Gohr 8468a285f7fSAndreas Gohr $evt = new Doku_Event('AUTH_PASSWORD_GENERATE', $data); 8478a285f7fSAndreas Gohr if($evt->advise_before(true)) { 848f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 849f3f0262cSandi $v = 'aeiou'; //vowels 850f3f0262cSandi $a = $c.$v; //both 851987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 852f3f0262cSandi 853987c8d26SAndreas Gohr //use thre syllables... 854987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 855483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 856483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 857483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 858f3f0262cSandi } 859987c8d26SAndreas Gohr //... and add a nice number and special 860483b6238SMichael Hamann $data['password'] .= auth_random(10, 99).$s[auth_random(0, strlen($s) - 1)]; 8618a285f7fSAndreas Gohr } 8628a285f7fSAndreas Gohr $evt->advise_after(); 863f3f0262cSandi 8648a285f7fSAndreas Gohr return $data['password']; 865f3f0262cSandi} 866f3f0262cSandi 867f3f0262cSandi/** 868f3f0262cSandi * Sends a password to the given user 869f3f0262cSandi * 87015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 87142ea7f44SGerrit Uitslag * 872ab5d26daSAndreas Gohr * @param string $user Login name of the user 873ab5d26daSAndreas Gohr * @param string $password The new password in clear text 87415fae107Sandi * @return bool true on success 875f3f0262cSandi */ 876f3f0262cSandifunction auth_sendPassword($user, $password) { 877f3f0262cSandi global $lang; 87827058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 879cd52f92dSchris global $auth; 880beca106aSAdrian Lang if(!$auth) return false; 881cd52f92dSchris 882d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8832dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 884f3f0262cSandi 88587ddda95Sandi if(!$userinfo['mail']) return false; 886f3f0262cSandi 887f3f0262cSandi $text = rawLocale('password'); 888d7169d19SAndreas Gohr $trep = array( 889d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 890d7169d19SAndreas Gohr 'LOGIN' => $user, 891d7169d19SAndreas Gohr 'PASSWORD' => $password 892d7169d19SAndreas Gohr ); 893f3f0262cSandi 894d7169d19SAndreas Gohr $mail = new Mailer(); 895d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 896d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 897d7169d19SAndreas Gohr $mail->setBody($text, $trep); 898d7169d19SAndreas Gohr return $mail->send(); 899f3f0262cSandi} 900f3f0262cSandi 901f3f0262cSandi/** 90215fae107Sandi * Register a new user 903f3f0262cSandi * 90415fae107Sandi * This registers a new user - Data is read directly from $_POST 90515fae107Sandi * 90615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 90742ea7f44SGerrit Uitslag * 90815fae107Sandi * @return bool true on success, false on any error 909f3f0262cSandi */ 910f3f0262cSandifunction register() { 911f3f0262cSandi global $lang; 912eb5d07e4Sjan global $conf; 91327058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 914cd52f92dSchris global $auth; 91564273335SAndreas Gohr global $INPUT; 916f3f0262cSandi 91764273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9183a48618aSAnika Henke if(!actionOK('register')) return false; 919640145a5Sandi 92064273335SAndreas Gohr // gather input 92164273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 92264273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 92364273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 92464273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 92564273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 926d752aedeSAndreas Gohr 92764273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 928f3f0262cSandi msg($lang['regmissing'], -1); 929f3f0262cSandi return false; 930f3f0262cSandi } 931f3f0262cSandi 932cab2716aSmatthias.grimm if($conf['autopasswd']) { 9338a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 93464273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 935bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 936cab2716aSmatthias.grimm return false; 93764273335SAndreas Gohr } elseif($pass != $passchk) { 938bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 939cab2716aSmatthias.grimm return false; 940cab2716aSmatthias.grimm } 941cab2716aSmatthias.grimm 942f3f0262cSandi //check mail 94364273335SAndreas Gohr if(!mail_isvalid($email)) { 944f3f0262cSandi msg($lang['regbadmail'], -1); 945f3f0262cSandi return false; 946f3f0262cSandi } 947f3f0262cSandi 948f3f0262cSandi //okay try to create the user 94964273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 950db9faf02SPatrick Brown msg($lang['regfail'], -1); 951f3f0262cSandi return false; 952f3f0262cSandi } 953f3f0262cSandi 954790b7720SAndreas Gohr // send notification about the new user 955790b7720SAndreas Gohr $subscription = new Subscription(); 956790b7720SAndreas Gohr $subscription->send_register($login, $fullname, $email); 95702a498e7Schris 958790b7720SAndreas Gohr // are we done? 959cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 960cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 961cab2716aSmatthias.grimm return true; 962cab2716aSmatthias.grimm } 963cab2716aSmatthias.grimm 964790b7720SAndreas Gohr // autogenerated password? then send password to user 96564273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 966f3f0262cSandi msg($lang['regsuccess'], 1); 967f3f0262cSandi return true; 968f3f0262cSandi } else { 969f3f0262cSandi msg($lang['regmailfail'], -1); 970f3f0262cSandi return false; 971f3f0262cSandi } 972f3f0262cSandi} 973f3f0262cSandi 97410a76f6fSfrank/** 9758b06d178Schris * Update user profile 9768b06d178Schris * 9778b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9788b06d178Schris */ 9798b06d178Schrisfunction updateprofile() { 9808b06d178Schris global $conf; 9818b06d178Schris global $lang; 98227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 983cd52f92dSchris global $auth; 984bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 985bcc94b2cSAndreas Gohr global $INPUT; 9868b06d178Schris 987bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9881b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 9898b06d178Schris 9903a48618aSAnika Henke if(!actionOK('profile')) { 9918b06d178Schris msg($lang['profna'], -1); 9928b06d178Schris return false; 9938b06d178Schris } 9948b06d178Schris 995bcc94b2cSAndreas Gohr $changes = array(); 996bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 997bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 998bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 999bcc94b2cSAndreas Gohr 1000bcc94b2cSAndreas Gohr // check misspelled passwords 1001bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 1002bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10038b06d178Schris return false; 10048b06d178Schris } 10058b06d178Schris 10068b06d178Schris // clean fullname and email 1007bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1008bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10098b06d178Schris 1010bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 1011bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 1012bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1013ab5d26daSAndreas Gohr ) { 10148b06d178Schris msg($lang['profnoempty'], -1); 10158b06d178Schris return false; 10168b06d178Schris } 1017bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10188b06d178Schris msg($lang['regbadmail'], -1); 10198b06d178Schris return false; 10208b06d178Schris } 10218b06d178Schris 1022bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10234c21b7eeSAndreas Gohr 1024bcc94b2cSAndreas Gohr // check for unavailable capabilities 1025bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 1026bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 1027bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 1028bcc94b2cSAndreas Gohr 1029bcc94b2cSAndreas Gohr // anything to do? 10308b06d178Schris if(!count($changes)) { 10318b06d178Schris msg($lang['profnochange'], -1); 10328b06d178Schris return false; 10338b06d178Schris } 10348b06d178Schris 10358b06d178Schris if($conf['profileconfirm']) { 1036585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 103771422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10388b06d178Schris return false; 10398b06d178Schris } 10408b06d178Schris } 10418b06d178Schris 1042e6c4392fSPatrick Brown if(!$auth->triggerUserMod('modify', array($INPUT->server->str('REMOTE_USER'), &$changes))) { 1043db9faf02SPatrick Brown msg($lang['proffail'], -1); 1044db9faf02SPatrick Brown return false; 1045db9faf02SPatrick Brown } 1046db9faf02SPatrick Brown 104732ed2b36SAndreas Gohr if($changes['pass']) { 1048c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1049ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 105004369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1051585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1052c276e9e8SMarcel Pennewiss } else { 1053c276e9e8SMarcel Pennewiss // make sure the session is writable 1054c276e9e8SMarcel Pennewiss @session_start(); 1055c276e9e8SMarcel Pennewiss // invalidate session cache 1056c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1057c276e9e8SMarcel Pennewiss session_write_close(); 105832ed2b36SAndreas Gohr } 1059c276e9e8SMarcel Pennewiss 106025b2a98cSMichael Klier return true; 1061a0b5b007SChris Smith} 1062ab5d26daSAndreas Gohr 106304d68ae4SGerrit Uitslag/** 106404d68ae4SGerrit Uitslag * Delete the current logged-in user 106504d68ae4SGerrit Uitslag * 106604d68ae4SGerrit Uitslag * @return bool true on success, false on any error 106704d68ae4SGerrit Uitslag */ 10682a7abf2dSChristopher Smithfunction auth_deleteprofile(){ 10692a7abf2dSChristopher Smith global $conf; 10702a7abf2dSChristopher Smith global $lang; 107173012efdSChristopher Smith /* @var DokuWiki_Auth_Plugin $auth */ 10722a7abf2dSChristopher Smith global $auth; 10732a7abf2dSChristopher Smith /* @var Input $INPUT */ 10742a7abf2dSChristopher Smith global $INPUT; 10752a7abf2dSChristopher Smith 10762a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 10772a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 10782a7abf2dSChristopher Smith 10792a7abf2dSChristopher Smith // action prevented or auth module disallows 10802a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10812a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10822a7abf2dSChristopher Smith return false; 10832a7abf2dSChristopher Smith } 10842a7abf2dSChristopher Smith 10852a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 10862a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10872a7abf2dSChristopher Smith return false; 10882a7abf2dSChristopher Smith } 10892a7abf2dSChristopher Smith 10902a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1091585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10922a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10932a7abf2dSChristopher Smith return false; 10942a7abf2dSChristopher Smith } 10952a7abf2dSChristopher Smith } 10962a7abf2dSChristopher Smith 109759bc3b48SGerrit Uitslag $deleted = array(); 1098585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 109973012efdSChristopher Smith if($auth->triggerUserMod('delete', array($deleted))) { 11002a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11012a7abf2dSChristopher Smith auth_logoff(); 11022a7abf2dSChristopher Smith return true; 11032a7abf2dSChristopher Smith } 11042a7abf2dSChristopher Smith 11052a7abf2dSChristopher Smith return false; 11062a7abf2dSChristopher Smith} 11072a7abf2dSChristopher Smith 11088b06d178Schris/** 11098b06d178Schris * Send a new password 11108b06d178Schris * 11111d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11121d5856cfSAndreas Gohr * 11131d5856cfSAndreas Gohr * - handling the first request of password reset 11141d5856cfSAndreas Gohr * - validating the password reset auth token 11151d5856cfSAndreas Gohr * 11168b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11178b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11181d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11198b06d178Schris * 11208b06d178Schris * @return bool true on success, false on any error 11218b06d178Schris */ 11228b06d178Schrisfunction act_resendpwd() { 11238b06d178Schris global $lang; 11248b06d178Schris global $conf; 112527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1126cd52f92dSchris global $auth; 1127bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1128bcc94b2cSAndreas Gohr global $INPUT; 11298b06d178Schris 11303a48618aSAnika Henke if(!actionOK('resendpwd')) { 11318b06d178Schris msg($lang['resendna'], -1); 11328b06d178Schris return false; 11338b06d178Schris } 11348b06d178Schris 1135bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11368b06d178Schris 11371d5856cfSAndreas Gohr if($token) { 1138cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11391d5856cfSAndreas Gohr 11401d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 114179e79377SAndreas Gohr if(!file_exists($tfile)) { 11421d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1143bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11441d5856cfSAndreas Gohr return false; 11451d5856cfSAndreas Gohr } 11468a9735e3SAndreas Gohr // token is only valid for 3 days 11478a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11488a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1149bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11501d5856cfSAndreas Gohr @unlink($tfile); 11518a9735e3SAndreas Gohr return false; 11528a9735e3SAndreas Gohr } 11538a9735e3SAndreas Gohr 11548b06d178Schris $user = io_readfile($tfile); 11552dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11568b06d178Schris if(!$userinfo['mail']) { 11578b06d178Schris msg($lang['resendpwdnouser'], -1); 11588b06d178Schris return false; 11598b06d178Schris } 11608b06d178Schris 1161cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1162bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1163bcc94b2cSAndreas Gohr 1164cc204bbdSAndreas Gohr // password given correctly? 1165bcc94b2cSAndreas Gohr if(!$pass) return false; 1166bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1167451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1168cc204bbdSAndreas Gohr return false; 1169cc204bbdSAndreas Gohr } 1170cc204bbdSAndreas Gohr 1171bcc94b2cSAndreas Gohr // change it 1172cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1173db9faf02SPatrick Brown msg($lang['proffail'], -1); 1174cc204bbdSAndreas Gohr return false; 1175cc204bbdSAndreas Gohr } 1176cc204bbdSAndreas Gohr 1177cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1178cc204bbdSAndreas Gohr 11798a285f7fSAndreas Gohr $pass = auth_pwgen($user); 11807d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1181db9faf02SPatrick Brown msg($lang['proffail'], -1); 11828b06d178Schris return false; 11838b06d178Schris } 11848b06d178Schris 11858b06d178Schris if(auth_sendPassword($user, $pass)) { 11868b06d178Schris msg($lang['resendpwdsuccess'], 1); 11878b06d178Schris } else { 11888b06d178Schris msg($lang['regmailfail'], -1); 11898b06d178Schris } 1190cc204bbdSAndreas Gohr } 1191cc204bbdSAndreas Gohr 1192cc204bbdSAndreas Gohr @unlink($tfile); 11938b06d178Schris return true; 11941d5856cfSAndreas Gohr 11951d5856cfSAndreas Gohr } else { 11961d5856cfSAndreas Gohr // we're in request phase 11971d5856cfSAndreas Gohr 1198bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 11991d5856cfSAndreas Gohr 1200bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 12011d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12021d5856cfSAndreas Gohr return false; 12031d5856cfSAndreas Gohr } else { 1204bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12051d5856cfSAndreas Gohr } 12061d5856cfSAndreas Gohr 12072dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 12081d5856cfSAndreas Gohr if(!$userinfo['mail']) { 12091d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12101d5856cfSAndreas Gohr return false; 12111d5856cfSAndreas Gohr } 12121d5856cfSAndreas Gohr 12131d5856cfSAndreas Gohr // generate auth token 1214483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12151d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 12161d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 12171d5856cfSAndreas Gohr 12181d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12191d5856cfSAndreas Gohr 12201d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1221d7169d19SAndreas Gohr $trep = array( 1222d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1223d7169d19SAndreas Gohr 'LOGIN' => $user, 1224d7169d19SAndreas Gohr 'CONFIRM' => $url 1225d7169d19SAndreas Gohr ); 12261d5856cfSAndreas Gohr 1227d7169d19SAndreas Gohr $mail = new Mailer(); 1228d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1229d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1230d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1231d7169d19SAndreas Gohr if($mail->send()) { 12321d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12331d5856cfSAndreas Gohr } else { 12341d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12351d5856cfSAndreas Gohr } 12361d5856cfSAndreas Gohr return true; 12371d5856cfSAndreas Gohr } 1238ab5d26daSAndreas Gohr // never reached 12398b06d178Schris} 12408b06d178Schris 12418b06d178Schris/** 1242b0855b11Sandi * Encrypts a password using the given method and salt 1243b0855b11Sandi * 1244b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1245b0855b11Sandi * is chosen. 1246b0855b11Sandi * 1247b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 124842ea7f44SGerrit Uitslag * 1249ab5d26daSAndreas Gohr * @param string $clear The clear text password 1250ab5d26daSAndreas Gohr * @param string $method The hashing method 1251ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1252b0855b11Sandi * @return string The crypted password 1253b0855b11Sandi */ 1254577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1255b0855b11Sandi global $conf; 1256b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 125710a76f6fSfrank 12583a0a2d05SAndreas Gohr $pass = new PassHash(); 12593a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1260b0855b11Sandi 12613a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1262b0855b11Sandi msg("Unsupported crypt method $method", -1); 12633a0a2d05SAndreas Gohr return false; 1264b0855b11Sandi } 12653a0a2d05SAndreas Gohr 12663a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1267b0855b11Sandi} 1268b0855b11Sandi 1269b0855b11Sandi/** 1270b0855b11Sandi * Verifies a cleartext password against a crypted hash 1271b0855b11Sandi * 1272b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 127342ea7f44SGerrit Uitslag * 1274ab5d26daSAndreas Gohr * @param string $clear The clear text password 1275ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1276ab5d26daSAndreas Gohr * @return bool true if both match 1277b0855b11Sandi */ 1278b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 12793a0a2d05SAndreas Gohr $pass = new PassHash(); 12803a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1281b0855b11Sandi} 1282340756e4Sandi 1283a0b5b007SChris Smith/** 1284a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1285a0b5b007SChris Smith * 1286a0b5b007SChris Smith * @param string $user username 1287a0b5b007SChris Smith * @param string $pass encrypted password 1288a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1289ab5d26daSAndreas Gohr * @return bool 1290a0b5b007SChris Smith */ 1291a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1292a0b5b007SChris Smith global $conf; 129327058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1294a0b5b007SChris Smith global $auth; 129579d00841SOliver Geisen global $USERINFO; 1296a0b5b007SChris Smith 1297beca106aSAdrian Lang if(!$auth) return false; 1298a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1299a0b5b007SChris Smith 1300a0b5b007SChris Smith // set cookie 1301645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 130273ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1303c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 130473ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 130555a71a16SGerrit Uitslag 1306a0b5b007SChris Smith // set session 1307a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1308234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1309a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1310a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1311a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1312ab5d26daSAndreas Gohr 1313ab5d26daSAndreas Gohr return true; 1314a0b5b007SChris Smith} 1315a0b5b007SChris Smith 1316645c0a36SAndreas Gohr/** 1317645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1318645c0a36SAndreas Gohr * 1319645c0a36SAndreas Gohr * @returns array 1320645c0a36SAndreas Gohr */ 1321645c0a36SAndreas Gohrfunction auth_getCookie() { 1322c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1323c66972f2SAdrian Lang return array(null, null, null); 1324c66972f2SAdrian Lang } 1325645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1326645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1327645c0a36SAndreas Gohr $pass = base64_decode($pass); 1328645c0a36SAndreas Gohr $user = base64_decode($user); 1329645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1330645c0a36SAndreas Gohr} 1331645c0a36SAndreas Gohr 1332e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1333