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 104bcc94b2cSAndreas Gohr if($INPUT->str('authtok')) { 105f13fa892SAndreas Gohr // when an authentication token is given, trust the session 106bcc94b2cSAndreas Gohr auth_validateToken($INPUT->str('authtok')); 107f13fa892SAndreas Gohr } elseif(!is_null($auth) && $auth->canDo('external')) { 108f13fa892SAndreas Gohr // external trust mechanism in place 109bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 110f5cb575dSAndreas Gohr } else { 1116080c584SRobin Gareus $evdata = array( 112bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 113bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 114bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 115bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1166080c584SRobin Gareus ); 117b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 118f5cb575dSAndreas Gohr } 119f5cb575dSAndreas Gohr 12016905344SAndreas Gohr //load ACL into a global array XXX 12175c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 122ab5d26daSAndreas Gohr 123ab5d26daSAndreas Gohr return true; 12475c93b77SAndreas Gohr} 12575c93b77SAndreas Gohr 12675c93b77SAndreas Gohr/** 12775c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12875c93b77SAndreas Gohr * 12975c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 130ab5d26daSAndreas Gohr * @return array 13175c93b77SAndreas Gohr */ 13275c93b77SAndreas Gohrfunction auth_loadACL() { 13375c93b77SAndreas Gohr global $config_cascade; 134b78bf706Sromain global $USERINFO; 135585bf44eSChristopher Smith /* @var Input $INPUT */ 136585bf44eSChristopher Smith global $INPUT; 13775c93b77SAndreas Gohr 13875c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 13975c93b77SAndreas Gohr 14075c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 14175c93b77SAndreas Gohr 14232e82180SAndreas Gohr $out = array(); 1439ce556d2SAndreas Gohr foreach($acl as $line) { 1449ce556d2SAndreas Gohr $line = trim($line); 145443e135dSChristopher Smith if(empty($line) || ($line{0} == '#')) continue; // skip blank lines & comments 14621c3090aSChristopher Smith list($id,$rest) = preg_split('/[ \t]+/',$line,2); 14732e82180SAndreas Gohr 148443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 149ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 150ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 151585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 152ad3d68d7SChristopher Smith 153585bf44eSChristopher Smith $id = str_replace('%USER%',cleanID($INPUT->server->str('REMOTE_USER')),$id); 154585bf44eSChristopher Smith $rest = str_replace('%USER%',auth_nameencode($INPUT->server->str('REMOTE_USER')),$rest); 155ad3d68d7SChristopher Smith } 156ad3d68d7SChristopher Smith 157ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1589ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 159ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 1609ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 161b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 16232e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 16332e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 164b78bf706Sromain } 16532e82180SAndreas Gohr } else { 16632e82180SAndreas Gohr $out[] = "$id\t$rest"; 167a8fe108bSGuy Brand } 16811799630Sandi } 1699ce556d2SAndreas Gohr 17032e82180SAndreas Gohr return $out; 171f3f0262cSandi} 172f3f0262cSandi 173ab5d26daSAndreas Gohr/** 174ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 175ab5d26daSAndreas Gohr * 176ab5d26daSAndreas Gohr * @param $evdata 177ab5d26daSAndreas Gohr * @return bool 178ab5d26daSAndreas Gohr */ 179b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 180ab5d26daSAndreas Gohr return auth_login( 181ab5d26daSAndreas Gohr $evdata['user'], 182b5ee21aaSAdrian Lang $evdata['password'], 183b5ee21aaSAdrian Lang $evdata['sticky'], 184ab5d26daSAndreas Gohr $evdata['silent'] 185ab5d26daSAndreas Gohr ); 186b5ee21aaSAdrian Lang} 187b5ee21aaSAdrian Lang 188f3f0262cSandi/** 189f3f0262cSandi * This tries to login the user based on the sent auth credentials 190f3f0262cSandi * 191f3f0262cSandi * The authentication works like this: if a username was given 19215fae107Sandi * a new login is assumed and user/password are checked. If they 19315fae107Sandi * are correct the password is encrypted with blowfish and stored 19415fae107Sandi * together with the username in a cookie - the same info is stored 19515fae107Sandi * in the session, too. Additonally a browserID is stored in the 19615fae107Sandi * session. 19715fae107Sandi * 19815fae107Sandi * If no username was given the cookie is checked: if the username, 19915fae107Sandi * crypted password and browserID match between session and cookie 20015fae107Sandi * no further testing is done and the user is accepted 20115fae107Sandi * 20215fae107Sandi * If a cookie was found but no session info was availabe the 203136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 20415fae107Sandi * together with username rechecked by calling this function again. 205f3f0262cSandi * 206f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 207f3f0262cSandi * are set. 20815fae107Sandi * 20915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 21015fae107Sandi * 21115fae107Sandi * @param string $user Username 21215fae107Sandi * @param string $pass Cleartext Password 21315fae107Sandi * @param bool $sticky Cookie should not expire 214f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 21515fae107Sandi * @return bool true on successful auth 216f3f0262cSandi */ 217f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 218f3f0262cSandi global $USERINFO; 219f3f0262cSandi global $conf; 220f3f0262cSandi global $lang; 22127058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 222cd52f92dSchris global $auth; 223585bf44eSChristopher Smith /* @var Input $INPUT */ 224585bf44eSChristopher Smith global $INPUT; 225ab5d26daSAndreas Gohr 226132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 227f3f0262cSandi 228beca106aSAdrian Lang if(!$auth) return false; 229beca106aSAdrian Lang 230bbbd6568SAndreas Gohr if(!empty($user)) { 231132bdbfeSandi //usual login 2325e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 233132bdbfeSandi // make logininfo globally available 234585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 23530d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 23604369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 237132bdbfeSandi return true; 238f3f0262cSandi } else { 239f3f0262cSandi //invalid credentials - log off 240f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 241f3f0262cSandi auth_logoff(); 242132bdbfeSandi return false; 243f3f0262cSandi } 244f3f0262cSandi } else { 245132bdbfeSandi // read cookie information 246645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 247132bdbfeSandi if($user && $pass) { 248132bdbfeSandi // we got a cookie - see if we can trust it 249fa7c70ffSAdrian Lang 250fa7c70ffSAdrian Lang // get session info 251fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 252132bdbfeSandi if(isset($session) && 2537172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2544c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 255132bdbfeSandi ($session['user'] == $user) && 256234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 257ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 258ab5d26daSAndreas Gohr ) { 259234ce57eSAndreas Gohr 260132bdbfeSandi // he has session, cookie and browser right - let him in 261585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 262132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 263132bdbfeSandi return true; 264132bdbfeSandi } 265f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 26630d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26704369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 268f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 269132bdbfeSandi } 270132bdbfeSandi } 271f3f0262cSandi //just to be sure 272883179a4SAndreas Gohr auth_logoff(true); 273132bdbfeSandi return false; 274f3f0262cSandi} 275132bdbfeSandi 276132bdbfeSandi/** 277f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 278f13fa892SAndreas Gohr * 279f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 280f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 281f13fa892SAndreas Gohr * 282f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 283f13fa892SAndreas Gohr * @param string $token The authentication token 284f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 285f13fa892SAndreas Gohr */ 286f13fa892SAndreas Gohrfunction auth_validateToken($token) { 287f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']) { 288f13fa892SAndreas Gohr // bad token 2899d2e1be6SAndreas Gohr http_status(401); 290f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 291f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 292f13fa892SAndreas Gohr exit; 293f13fa892SAndreas Gohr } 294f13fa892SAndreas Gohr // still here? trust the session data 295f13fa892SAndreas Gohr global $USERINFO; 296585bf44eSChristopher Smith /* @var Input $INPUT */ 297585bf44eSChristopher Smith global $INPUT; 298585bf44eSChristopher Smith 299585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER',$_SESSION[DOKU_COOKIE]['auth']['user']); 300f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 301f13fa892SAndreas Gohr return true; 302f13fa892SAndreas Gohr} 303f13fa892SAndreas Gohr 304f13fa892SAndreas Gohr/** 305f13fa892SAndreas Gohr * Create an auth token and store it in the session 306f13fa892SAndreas Gohr * 307f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 308f13fa892SAndreas Gohr * 309f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 310f13fa892SAndreas Gohr * @return string The auth token 311f13fa892SAndreas Gohr */ 312f13fa892SAndreas Gohrfunction auth_createToken() { 313483b6238SMichael Hamann $token = md5(auth_randombytes(16)); 31409c2d803SAndreas Gohr @session_start(); // reopen the session if needed 315f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 31609c2d803SAndreas Gohr session_write_close(); 317f13fa892SAndreas Gohr return $token; 318f13fa892SAndreas Gohr} 319f13fa892SAndreas Gohr 320f13fa892SAndreas Gohr/** 321136ce040Sandi * Builds a pseudo UID from browser and IP data 322132bdbfeSandi * 323132bdbfeSandi * This is neither unique nor unfakable - still it adds some 324136ce040Sandi * security. Using the first part of the IP makes sure 32580b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 32615fae107Sandi * 32715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 32815fae107Sandi * 32915fae107Sandi * @return string a MD5 sum of various browser headers 330132bdbfeSandi */ 331132bdbfeSandifunction auth_browseruid() { 332585bf44eSChristopher Smith /* @var Input $INPUT */ 333585bf44eSChristopher Smith global $INPUT; 334585bf44eSChristopher Smith 3352f9daf16SAndreas Gohr $ip = clientIP(true); 336132bdbfeSandi $uid = ''; 337585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_USER_AGENT'); 338585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_ENCODING'); 339585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_CHARSET'); 3402f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 34180b4f376SAndreas Gohr $uid = strtolower($uid); 342132bdbfeSandi return md5($uid); 343132bdbfeSandi} 344132bdbfeSandi 345132bdbfeSandi/** 346132bdbfeSandi * Creates a random key to encrypt the password in cookies 34715fae107Sandi * 34815fae107Sandi * This function tries to read the password for encrypting 34998407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 35015fae107Sandi * if no such file is found a random key is created and 35115fae107Sandi * and stored in this file. 35215fae107Sandi * 35315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 35432ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 35530d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 35615fae107Sandi * @return string 357132bdbfeSandi */ 35830d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 359132bdbfeSandi global $conf; 36098407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 36130d544a4SMichael Hamann if ($secure || !file_exists($file)) { 36230d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 36330d544a4SMichael Hamann } 364132bdbfeSandi $salt = io_readFile($file); 365132bdbfeSandi if(empty($salt)) { 36630d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 367132bdbfeSandi io_saveFile($file, $salt); 368132bdbfeSandi } 36932ed2b36SAndreas Gohr if($addsession) { 37032ed2b36SAndreas Gohr $salt .= session_id(); 37132ed2b36SAndreas Gohr } 372132bdbfeSandi return $salt; 373f3f0262cSandi} 374f3f0262cSandi 375f3f0262cSandi/** 376483b6238SMichael Hamann * Return truly (pseudo) random bytes if available, otherwise fall back to mt_rand 377483b6238SMichael Hamann * 378483b6238SMichael Hamann * @author Mark Seecof 379483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 380483b6238SMichael Hamann * @link http://www.php.net/manual/de/function.mt-rand.php#83655 381483b6238SMichael Hamann * @param int $length number of bytes to get 382483b6238SMichael Hamann * @return string binary random strings 383483b6238SMichael Hamann */ 384483b6238SMichael Hamannfunction auth_randombytes($length) { 385483b6238SMichael Hamann $strong = false; 386483b6238SMichael Hamann $rbytes = false; 387483b6238SMichael Hamann 388483b6238SMichael Hamann if (function_exists('openssl_random_pseudo_bytes') 389483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.4') >= 0 390483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 391483b6238SMichael Hamann ) { 392483b6238SMichael Hamann $rbytes = openssl_random_pseudo_bytes($length, $strong); 393483b6238SMichael Hamann } 394483b6238SMichael Hamann 395483b6238SMichael Hamann if (!$strong && function_exists('mcrypt_create_iv') 396483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.7') >= 0 397483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 398483b6238SMichael Hamann ) { 399483b6238SMichael Hamann $rbytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); 400483b6238SMichael Hamann if ($rbytes !== false && strlen($rbytes) === $length) { 401483b6238SMichael Hamann $strong = true; 402483b6238SMichael Hamann } 403483b6238SMichael Hamann } 404483b6238SMichael Hamann 405483b6238SMichael Hamann // If no strong randoms available, try OS the specific ways 406483b6238SMichael Hamann if(!$strong) { 407483b6238SMichael Hamann // Unix/Linux platform 408483b6238SMichael Hamann $fp = @fopen('/dev/urandom', 'rb'); 409483b6238SMichael Hamann if($fp !== false) { 410483b6238SMichael Hamann $rbytes = fread($fp, $length); 411483b6238SMichael Hamann fclose($fp); 412483b6238SMichael Hamann } 413483b6238SMichael Hamann 414483b6238SMichael Hamann // MS-Windows platform 415483b6238SMichael Hamann if(class_exists('COM')) { 416483b6238SMichael Hamann // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx 417483b6238SMichael Hamann try { 418483b6238SMichael Hamann $CAPI_Util = new COM('CAPICOM.Utilities.1'); 419483b6238SMichael Hamann $rbytes = $CAPI_Util->GetRandom($length, 0); 420483b6238SMichael Hamann 421483b6238SMichael Hamann // if we ask for binary data PHP munges it, so we 422483b6238SMichael Hamann // request base64 return value. 423483b6238SMichael Hamann if($rbytes) $rbytes = base64_decode($rbytes); 424483b6238SMichael Hamann } catch(Exception $ex) { 425483b6238SMichael Hamann // fail 426483b6238SMichael Hamann } 427483b6238SMichael Hamann } 428483b6238SMichael Hamann } 429483b6238SMichael Hamann if(strlen($rbytes) < $length) $rbytes = false; 430483b6238SMichael Hamann 431483b6238SMichael Hamann // still no random bytes available - fall back to mt_rand() 432483b6238SMichael Hamann if($rbytes === false) { 433483b6238SMichael Hamann $rbytes = ''; 434483b6238SMichael Hamann for ($i = 0; $i < $length; ++$i) { 435483b6238SMichael Hamann $rbytes .= chr(mt_rand(0, 255)); 436483b6238SMichael Hamann } 437483b6238SMichael Hamann } 438483b6238SMichael Hamann 439483b6238SMichael Hamann return $rbytes; 440483b6238SMichael Hamann} 441483b6238SMichael Hamann 442483b6238SMichael Hamann/** 443483b6238SMichael Hamann * Random number generator using the best available source 444483b6238SMichael Hamann * 445483b6238SMichael Hamann * @author Michael Samuel 446483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 447483b6238SMichael Hamann * @param int $min 448483b6238SMichael Hamann * @param int $max 449483b6238SMichael Hamann * @return int 450483b6238SMichael Hamann */ 451483b6238SMichael Hamannfunction auth_random($min, $max) { 452483b6238SMichael Hamann $abs_max = $max - $min; 453483b6238SMichael Hamann 454483b6238SMichael Hamann $nbits = 0; 455483b6238SMichael Hamann for ($n = $abs_max; $n > 0; $n >>= 1) { 456483b6238SMichael Hamann ++$nbits; 457483b6238SMichael Hamann } 458483b6238SMichael Hamann 459483b6238SMichael Hamann $mask = (1 << $nbits) - 1; 460483b6238SMichael Hamann do { 461483b6238SMichael Hamann $bytes = auth_randombytes(PHP_INT_SIZE); 462483b6238SMichael Hamann $integers = unpack('Inum', $bytes); 463483b6238SMichael Hamann $integer = $integers["num"] & $mask; 464483b6238SMichael Hamann } while ($integer > $abs_max); 465483b6238SMichael Hamann 466483b6238SMichael Hamann return $min + $integer; 467483b6238SMichael Hamann} 468483b6238SMichael Hamann 469483b6238SMichael Hamann/** 47004369c3eSMichael Hamann * Encrypt data using the given secret using AES 47104369c3eSMichael Hamann * 47204369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 47304369c3eSMichael Hamann * using pbkdf2. 47404369c3eSMichael Hamann * 47504369c3eSMichael Hamann * @param string $data The data that shall be encrypted 47604369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 47704369c3eSMichael Hamann * @return string The ciphertext 47804369c3eSMichael Hamann */ 47904369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 48004369c3eSMichael Hamann $iv = auth_randombytes(16); 48104369c3eSMichael Hamann $cipher = new Crypt_AES(); 48204369c3eSMichael Hamann $cipher->setPassword($secret); 48304369c3eSMichael Hamann 4847b650cefSMichael Hamann /* 4857b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 4867b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 4877b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 4887b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 4897b650cefSMichael Hamann */ 49004369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 49104369c3eSMichael Hamann} 49204369c3eSMichael Hamann 49304369c3eSMichael Hamann/** 49404369c3eSMichael Hamann * Decrypt the given AES ciphertext 49504369c3eSMichael Hamann * 49604369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 49704369c3eSMichael Hamann * 49804369c3eSMichael Hamann * @param string $ciphertext The encrypted data 49904369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 50004369c3eSMichael Hamann * @return string The decrypted data 50104369c3eSMichael Hamann */ 50204369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 5037b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 50404369c3eSMichael Hamann $cipher = new Crypt_AES(); 50504369c3eSMichael Hamann $cipher->setPassword($secret); 5067b650cefSMichael Hamann $cipher->setIV($iv); 50704369c3eSMichael Hamann 5087b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 50904369c3eSMichael Hamann} 51004369c3eSMichael Hamann 51104369c3eSMichael Hamann/** 512883179a4SAndreas Gohr * Log out the current user 513883179a4SAndreas Gohr * 514f3f0262cSandi * This clears all authentication data and thus log the user 515883179a4SAndreas Gohr * off. It also clears session data. 51615fae107Sandi * 51715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 518883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 519f3f0262cSandi */ 520883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 521f3f0262cSandi global $conf; 522f3f0262cSandi global $USERINFO; 52327058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 5245298a619SAndreas Gohr global $auth; 525585bf44eSChristopher Smith /* @var Input $INPUT */ 526585bf44eSChristopher Smith global $INPUT; 52737065e65Sandi 528d4869846SAndreas Gohr // make sure the session is writable (it usually is) 529e9621d07SAndreas Gohr @session_start(); 530e9621d07SAndreas Gohr 531e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 532e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 533e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 534e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 535e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 536e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 537883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 538e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 539585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 540132bdbfeSandi $USERINFO = null; //FIXME 541f5c6743cSAndreas Gohr 54273ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 54373ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 5445298a619SAndreas Gohr 545880f62faSAndreas Gohr if($auth) $auth->logOff(); 546f3f0262cSandi} 547f3f0262cSandi 548f3f0262cSandi/** 549f8cc712eSAndreas Gohr * Check if a user is a manager 550f8cc712eSAndreas Gohr * 551f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 552f8cc712eSAndreas Gohr * user. 553f8cc712eSAndreas Gohr * 554f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 555f8cc712eSAndreas Gohr * 556f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 557f8cc712eSAndreas Gohr * @see auth_isadmin 558ab5d26daSAndreas Gohr * @param string $user Username 559ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 560ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 561ab5d26daSAndreas Gohr * @return bool 562f8cc712eSAndreas Gohr */ 563f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 564f8cc712eSAndreas Gohr global $conf; 565f8cc712eSAndreas Gohr global $USERINFO; 56627058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 567d752aedeSAndreas Gohr global $auth; 568585bf44eSChristopher Smith /* @var Input $INPUT */ 569585bf44eSChristopher Smith global $INPUT; 570585bf44eSChristopher Smith 571f8cc712eSAndreas Gohr 572beca106aSAdrian Lang if(!$auth) return false; 573c66972f2SAdrian Lang if(is_null($user)) { 574585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 575c66972f2SAdrian Lang return false; 576c66972f2SAdrian Lang } else { 577585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 578c66972f2SAdrian Lang } 579c66972f2SAdrian Lang } 580d6dc956fSAndreas Gohr if(is_null($groups)) { 581d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 582e259aa79SAndreas Gohr } 583e259aa79SAndreas Gohr 584d6dc956fSAndreas Gohr // check superuser match 585d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 586d6dc956fSAndreas Gohr if($adminonly) return false; 587e259aa79SAndreas Gohr // check managers 588d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 58900ce12daSChris Smith 590f8cc712eSAndreas Gohr return false; 591f8cc712eSAndreas Gohr} 592f8cc712eSAndreas Gohr 593f8cc712eSAndreas Gohr/** 594f8cc712eSAndreas Gohr * Check if a user is admin 595f8cc712eSAndreas Gohr * 596f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 597f8cc712eSAndreas Gohr * 598f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 599f8cc712eSAndreas Gohr * 600f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 601ab5d26daSAndreas Gohr * @see auth_ismanager() 602ab5d26daSAndreas Gohr * @param string $user Username 603ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 604ab5d26daSAndreas Gohr * @return bool 605f8cc712eSAndreas Gohr */ 606f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 607f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 608f8cc712eSAndreas Gohr} 609f8cc712eSAndreas Gohr 610d6dc956fSAndreas Gohr/** 611d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 612d6dc956fSAndreas Gohr * users and groups to determine membership status 613d6dc956fSAndreas Gohr * 614d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 615d6dc956fSAndreas Gohr * 616d6dc956fSAndreas Gohr * @param $memberlist string commaseparated list of allowed users and groups 617d6dc956fSAndreas Gohr * @param $user string user to match against 618d6dc956fSAndreas Gohr * @param $groups array groups the user is member of 6195446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 620d6dc956fSAndreas Gohr */ 621d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 62227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 623d6dc956fSAndreas Gohr global $auth; 624d6dc956fSAndreas Gohr if(!$auth) return false; 625d6dc956fSAndreas Gohr 626d6dc956fSAndreas Gohr // clean user and groups 6274f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 628d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 629d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 630d6dc956fSAndreas Gohr } 631d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 632d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 633d6dc956fSAndreas Gohr 634d6dc956fSAndreas Gohr // extract the memberlist 635d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 636d6dc956fSAndreas Gohr $members = array_map('trim', $members); 637d6dc956fSAndreas Gohr $members = array_unique($members); 638d6dc956fSAndreas Gohr $members = array_filter($members); 639d6dc956fSAndreas Gohr 640d6dc956fSAndreas Gohr // compare cleaned values 641d6dc956fSAndreas Gohr foreach($members as $member) { 642e5204a12SJurgen Hart if($member == '@ALL' ) return true; 6434f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 644d6dc956fSAndreas Gohr if($member[0] == '@') { 645d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 646d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 647d6dc956fSAndreas Gohr } else { 648d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 649d6dc956fSAndreas Gohr if($member == $user) return true; 650d6dc956fSAndreas Gohr } 651d6dc956fSAndreas Gohr } 652d6dc956fSAndreas Gohr 653d6dc956fSAndreas Gohr // still here? not a member! 654d6dc956fSAndreas Gohr return false; 655d6dc956fSAndreas Gohr} 656d6dc956fSAndreas Gohr 657f8cc712eSAndreas Gohr/** 65815fae107Sandi * Convinience function for auth_aclcheck() 65915fae107Sandi * 66015fae107Sandi * This checks the permissions for the current user 66115fae107Sandi * 66215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 66315fae107Sandi * 6641698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 66515fae107Sandi * @return int permission level 666f3f0262cSandi */ 667f3f0262cSandifunction auth_quickaclcheck($id) { 668f3f0262cSandi global $conf; 669f3f0262cSandi global $USERINFO; 670585bf44eSChristopher Smith /* @var Input $INPUT */ 671585bf44eSChristopher Smith global $INPUT; 672f3f0262cSandi # if no ACL is used always return upload rights 673f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 674585bf44eSChristopher Smith return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 675f3f0262cSandi} 676f3f0262cSandi 677f3f0262cSandi/** 678c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 67915fae107Sandi * 68015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 681c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6821698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 68315fae107Sandi * @param string $user Username 6843272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 68515fae107Sandi * @return int permission level 686f3f0262cSandi */ 687f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 688c17acc9fSAndreas Gohr $data = array( 689c17acc9fSAndreas Gohr 'id' => $id, 690c17acc9fSAndreas Gohr 'user' => $user, 691c17acc9fSAndreas Gohr 'groups' => $groups 692c17acc9fSAndreas Gohr ); 693c17acc9fSAndreas Gohr 694c17acc9fSAndreas Gohr return trigger_event('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 695c17acc9fSAndreas Gohr} 696c17acc9fSAndreas Gohr 697c17acc9fSAndreas Gohr/** 698c17acc9fSAndreas Gohr * default ACL check method 699c17acc9fSAndreas Gohr * 700c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 701c17acc9fSAndreas Gohr * 702c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 703c17acc9fSAndreas Gohr * @param array $data event data 704c17acc9fSAndreas Gohr * @return int permission level 705c17acc9fSAndreas Gohr */ 706c17acc9fSAndreas Gohrfunction auth_aclcheck_cb($data) { 707c17acc9fSAndreas Gohr $id =& $data['id']; 708c17acc9fSAndreas Gohr $user =& $data['user']; 709c17acc9fSAndreas Gohr $groups =& $data['groups']; 710c17acc9fSAndreas Gohr 711f3f0262cSandi global $conf; 712f3f0262cSandi global $AUTH_ACL; 71327058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 714d752aedeSAndreas Gohr global $auth; 715f3f0262cSandi 71685d03f68SAndreas Gohr // if no ACL is used always return upload rights 717f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 718beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 719f3f0262cSandi 720074cf26bSandi //make sure groups is an array 721074cf26bSandi if(!is_array($groups)) $groups = array(); 722074cf26bSandi 72385d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 724ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 725ab5d26daSAndreas Gohr return AUTH_ADMIN; 726ab5d26daSAndreas Gohr } 72785d03f68SAndreas Gohr 728eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 729eb3ce0d5SKazutaka Miyasaka $user = utf8_strtolower($user); 730eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 731eb3ce0d5SKazutaka Miyasaka } 732d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 733d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 73485d03f68SAndreas Gohr $user = auth_nameencode($user); 73585d03f68SAndreas Gohr 7366c2bb100SAndreas Gohr //prepend groups with @ and nameencode 7372cd2db38Sandi $cnt = count($groups); 7382cd2db38Sandi for($i = 0; $i < $cnt; $i++) { 7396c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 74010a76f6fSfrank } 74110a76f6fSfrank 742f3f0262cSandi $ns = getNS($id); 743f3f0262cSandi $perm = -1; 744f3f0262cSandi 74534aeb4afSAndreas Gohr if($user || count($groups)) { 746f3f0262cSandi //add ALL group 747f3f0262cSandi $groups[] = '@ALL'; 748f3f0262cSandi //add User 74934aeb4afSAndreas Gohr if($user) $groups[] = $user; 750f3f0262cSandi } else { 75148d7b7a6SDominik Eckelmann $groups[] = '@ALL'; 752f3f0262cSandi } 753f3f0262cSandi 754f3f0262cSandi //check exact match first 75521c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 756f3f0262cSandi if(count($matches)) { 757f3f0262cSandi foreach($matches as $match) { 758f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 75921c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 760eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 761eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 762eb3ce0d5SKazutaka Miyasaka } 76348d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 76448d7b7a6SDominik Eckelmann continue; 76548d7b7a6SDominik Eckelmann } 7668ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 767f3f0262cSandi if($acl[2] > $perm) { 768f3f0262cSandi $perm = $acl[2]; 769f3f0262cSandi } 770f3f0262cSandi } 771f3f0262cSandi if($perm > -1) { 772f3f0262cSandi //we had a match - return it 773def492a2SGuillaume Turri return (int) $perm; 774f3f0262cSandi } 775f3f0262cSandi } 776f3f0262cSandi 777f3f0262cSandi //still here? do the namespace checks 778f3f0262cSandi if($ns) { 7793e304b55SMichael Hamann $path = $ns.':*'; 780f3f0262cSandi } else { 7813e304b55SMichael Hamann $path = '*'; //root document 782f3f0262cSandi } 783f3f0262cSandi 784f3f0262cSandi do { 78521c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 786f3f0262cSandi if(count($matches)) { 787f3f0262cSandi foreach($matches as $match) { 788f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 78921c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 790eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 791eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 792eb3ce0d5SKazutaka Miyasaka } 79348d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 79448d7b7a6SDominik Eckelmann continue; 79548d7b7a6SDominik Eckelmann } 7968ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 797f3f0262cSandi if($acl[2] > $perm) { 798f3f0262cSandi $perm = $acl[2]; 799f3f0262cSandi } 800f3f0262cSandi } 801f3f0262cSandi //we had a match - return it 80248d7b7a6SDominik Eckelmann if($perm != -1) { 803def492a2SGuillaume Turri return (int) $perm; 804f3f0262cSandi } 80548d7b7a6SDominik Eckelmann } 806f3f0262cSandi //get next higher namespace 807f3f0262cSandi $ns = getNS($ns); 808f3f0262cSandi 8093e304b55SMichael Hamann if($path != '*') { 8103e304b55SMichael Hamann $path = $ns.':*'; 8113e304b55SMichael Hamann if($path == ':*') $path = '*'; 812f3f0262cSandi } else { 813f3f0262cSandi //we did this already 814f3f0262cSandi //looks like there is something wrong with the ACL 815f3f0262cSandi //break here 816d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 817d5ce66f6SAndreas Gohr return AUTH_NONE; 818f3f0262cSandi } 819f3f0262cSandi } while(1); //this should never loop endless 820ab5d26daSAndreas Gohr return AUTH_NONE; 821f3f0262cSandi} 822f3f0262cSandi 823f3f0262cSandi/** 8246c2bb100SAndreas Gohr * Encode ASCII special chars 8256c2bb100SAndreas Gohr * 8266c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 8276c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 8286c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 8296c2bb100SAndreas Gohr * urlencoding!). 8306c2bb100SAndreas Gohr * 8316c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 8326c2bb100SAndreas Gohr * 8336c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 8346c2bb100SAndreas Gohr * @see rawurldecode() 8356c2bb100SAndreas Gohr */ 836e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 837a424cd8eSchris global $cache_authname; 838a424cd8eSchris $cache =& $cache_authname; 83931784267SAndreas Gohr $name = (string) $name; 840a424cd8eSchris 84180601d26SAndreas Gohr // never encode wildcard FS#1955 84280601d26SAndreas Gohr if($name == '%USER%') return $name; 843b78bf706Sromain if($name == '%GROUP%') return $name; 84480601d26SAndreas Gohr 845a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 846e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 84730f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 84830f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 84930f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 850ab5d26daSAndreas Gohr ); 851e838fc2eSAndreas Gohr } else { 85230f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 85330f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 85430f6faf0SChristopher Smith 'auth_nameencode_callback', $name 855ab5d26daSAndreas Gohr ); 856e838fc2eSAndreas Gohr } 8576c2bb100SAndreas Gohr } 8586c2bb100SAndreas Gohr 859a424cd8eSchris return $cache[$name][$skip_group]; 860a424cd8eSchris} 861a424cd8eSchris 86204d68ae4SGerrit Uitslag/** 86304d68ae4SGerrit Uitslag * callback encodes the matches 86404d68ae4SGerrit Uitslag * 86504d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 86604d68ae4SGerrit Uitslag * @return string 86704d68ae4SGerrit Uitslag */ 86830f6faf0SChristopher Smithfunction auth_nameencode_callback($matches) { 86930f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1],-1))); 87030f6faf0SChristopher Smith} 87130f6faf0SChristopher Smith 8726c2bb100SAndreas Gohr/** 873f3f0262cSandi * Create a pronouncable password 874f3f0262cSandi * 8758a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8768a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8778a285f7fSAndreas Gohr * 87815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 87915fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8808a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 88115fae107Sandi * 8828a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 88315fae107Sandi * @return string pronouncable password 884f3f0262cSandi */ 8858a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 8868a285f7fSAndreas Gohr $data = array( 887d628dcf3SAndreas Gohr 'password' => '', 888d628dcf3SAndreas Gohr 'foruser' => $foruser 8898a285f7fSAndreas Gohr ); 8908a285f7fSAndreas Gohr 8918a285f7fSAndreas Gohr $evt = new Doku_Event('AUTH_PASSWORD_GENERATE', $data); 8928a285f7fSAndreas Gohr if($evt->advise_before(true)) { 893f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 894f3f0262cSandi $v = 'aeiou'; //vowels 895f3f0262cSandi $a = $c.$v; //both 896987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 897f3f0262cSandi 898987c8d26SAndreas Gohr //use thre syllables... 899987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 900483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 901483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 902483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 903f3f0262cSandi } 904987c8d26SAndreas Gohr //... and add a nice number and special 905483b6238SMichael Hamann $data['password'] .= auth_random(10, 99).$s[auth_random(0, strlen($s) - 1)]; 9068a285f7fSAndreas Gohr } 9078a285f7fSAndreas Gohr $evt->advise_after(); 908f3f0262cSandi 9098a285f7fSAndreas Gohr return $data['password']; 910f3f0262cSandi} 911f3f0262cSandi 912f3f0262cSandi/** 913f3f0262cSandi * Sends a password to the given user 914f3f0262cSandi * 91515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 916ab5d26daSAndreas Gohr * @param string $user Login name of the user 917ab5d26daSAndreas Gohr * @param string $password The new password in clear text 91815fae107Sandi * @return bool true on success 919f3f0262cSandi */ 920f3f0262cSandifunction auth_sendPassword($user, $password) { 921f3f0262cSandi global $lang; 92227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 923cd52f92dSchris global $auth; 924beca106aSAdrian Lang if(!$auth) return false; 925cd52f92dSchris 926d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 9272dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 928f3f0262cSandi 92987ddda95Sandi if(!$userinfo['mail']) return false; 930f3f0262cSandi 931f3f0262cSandi $text = rawLocale('password'); 932d7169d19SAndreas Gohr $trep = array( 933d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 934d7169d19SAndreas Gohr 'LOGIN' => $user, 935d7169d19SAndreas Gohr 'PASSWORD' => $password 936d7169d19SAndreas Gohr ); 937f3f0262cSandi 938d7169d19SAndreas Gohr $mail = new Mailer(); 939d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 940d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 941d7169d19SAndreas Gohr $mail->setBody($text, $trep); 942d7169d19SAndreas Gohr return $mail->send(); 943f3f0262cSandi} 944f3f0262cSandi 945f3f0262cSandi/** 94615fae107Sandi * Register a new user 947f3f0262cSandi * 94815fae107Sandi * This registers a new user - Data is read directly from $_POST 94915fae107Sandi * 95015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 95115fae107Sandi * @return bool true on success, false on any error 952f3f0262cSandi */ 953f3f0262cSandifunction register() { 954f3f0262cSandi global $lang; 955eb5d07e4Sjan global $conf; 95627058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 957cd52f92dSchris global $auth; 95864273335SAndreas Gohr global $INPUT; 959f3f0262cSandi 96064273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9613a48618aSAnika Henke if(!actionOK('register')) return false; 962640145a5Sandi 96364273335SAndreas Gohr // gather input 96464273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 96564273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 96664273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 96764273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 96864273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 969d752aedeSAndreas Gohr 97064273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 971f3f0262cSandi msg($lang['regmissing'], -1); 972f3f0262cSandi return false; 973f3f0262cSandi } 974f3f0262cSandi 975cab2716aSmatthias.grimm if($conf['autopasswd']) { 9768a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 97764273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 978bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 979cab2716aSmatthias.grimm return false; 98064273335SAndreas Gohr } elseif($pass != $passchk) { 981bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 982cab2716aSmatthias.grimm return false; 983cab2716aSmatthias.grimm } 984cab2716aSmatthias.grimm 985f3f0262cSandi //check mail 98664273335SAndreas Gohr if(!mail_isvalid($email)) { 987f3f0262cSandi msg($lang['regbadmail'], -1); 988f3f0262cSandi return false; 989f3f0262cSandi } 990f3f0262cSandi 991f3f0262cSandi //okay try to create the user 99264273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 993f3f0262cSandi msg($lang['reguexists'], -1); 994f3f0262cSandi return false; 995f3f0262cSandi } 996f3f0262cSandi 997790b7720SAndreas Gohr // send notification about the new user 998790b7720SAndreas Gohr $subscription = new Subscription(); 999790b7720SAndreas Gohr $subscription->send_register($login, $fullname, $email); 100002a498e7Schris 1001790b7720SAndreas Gohr // are we done? 1002cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 1003cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 1004cab2716aSmatthias.grimm return true; 1005cab2716aSmatthias.grimm } 1006cab2716aSmatthias.grimm 1007790b7720SAndreas Gohr // autogenerated password? then send password to user 100864273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 1009f3f0262cSandi msg($lang['regsuccess'], 1); 1010f3f0262cSandi return true; 1011f3f0262cSandi } else { 1012f3f0262cSandi msg($lang['regmailfail'], -1); 1013f3f0262cSandi return false; 1014f3f0262cSandi } 1015f3f0262cSandi} 1016f3f0262cSandi 101710a76f6fSfrank/** 10188b06d178Schris * Update user profile 10198b06d178Schris * 10208b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 10218b06d178Schris */ 10228b06d178Schrisfunction updateprofile() { 10238b06d178Schris global $conf; 10248b06d178Schris global $lang; 102527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1026cd52f92dSchris global $auth; 1027bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1028bcc94b2cSAndreas Gohr global $INPUT; 10298b06d178Schris 1030bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 10311b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 10328b06d178Schris 10333a48618aSAnika Henke if(!actionOK('profile')) { 10348b06d178Schris msg($lang['profna'], -1); 10358b06d178Schris return false; 10368b06d178Schris } 10378b06d178Schris 1038bcc94b2cSAndreas Gohr $changes = array(); 1039bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 1040bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 1041bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 1042bcc94b2cSAndreas Gohr 1043bcc94b2cSAndreas Gohr // check misspelled passwords 1044bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 1045bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10468b06d178Schris return false; 10478b06d178Schris } 10488b06d178Schris 10498b06d178Schris // clean fullname and email 1050bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1051bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10528b06d178Schris 1053bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 1054bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 1055bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1056ab5d26daSAndreas Gohr ) { 10578b06d178Schris msg($lang['profnoempty'], -1); 10588b06d178Schris return false; 10598b06d178Schris } 1060bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10618b06d178Schris msg($lang['regbadmail'], -1); 10628b06d178Schris return false; 10638b06d178Schris } 10648b06d178Schris 1065bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10664c21b7eeSAndreas Gohr 1067bcc94b2cSAndreas Gohr // check for unavailable capabilities 1068bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 1069bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 1070bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 1071bcc94b2cSAndreas Gohr 1072bcc94b2cSAndreas Gohr // anything to do? 10738b06d178Schris if(!count($changes)) { 10748b06d178Schris msg($lang['profnochange'], -1); 10758b06d178Schris return false; 10768b06d178Schris } 10778b06d178Schris 10788b06d178Schris if($conf['profileconfirm']) { 1079585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 108071422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10818b06d178Schris return false; 10828b06d178Schris } 10838b06d178Schris } 10848b06d178Schris 108549cd1ed0SAndreas Gohr if($result = $auth->triggerUserMod('modify', array($INPUT->server->str('REMOTE_USER'), &$changes))) { 1086a0b5b007SChris Smith // update cookie and session with the changed data 108732ed2b36SAndreas Gohr if($changes['pass']) { 1088ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 108904369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1090585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 109132ed2b36SAndreas Gohr } 109225b2a98cSMichael Klier return true; 1093a0b5b007SChris Smith } 1094ab5d26daSAndreas Gohr 1095ab5d26daSAndreas Gohr return false; 10968b06d178Schris} 10978b06d178Schris 109804d68ae4SGerrit Uitslag/** 109904d68ae4SGerrit Uitslag * Delete the current logged-in user 110004d68ae4SGerrit Uitslag * 110104d68ae4SGerrit Uitslag * @return bool true on success, false on any error 110204d68ae4SGerrit Uitslag */ 11032a7abf2dSChristopher Smithfunction auth_deleteprofile(){ 11042a7abf2dSChristopher Smith global $conf; 11052a7abf2dSChristopher Smith global $lang; 110673012efdSChristopher Smith /* @var DokuWiki_Auth_Plugin $auth */ 11072a7abf2dSChristopher Smith global $auth; 11082a7abf2dSChristopher Smith /* @var Input $INPUT */ 11092a7abf2dSChristopher Smith global $INPUT; 11102a7abf2dSChristopher Smith 11112a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 11122a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 11132a7abf2dSChristopher Smith 11142a7abf2dSChristopher Smith // action prevented or auth module disallows 11152a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 11162a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 11172a7abf2dSChristopher Smith return false; 11182a7abf2dSChristopher Smith } 11192a7abf2dSChristopher Smith 11202a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 11212a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 11222a7abf2dSChristopher Smith return false; 11232a7abf2dSChristopher Smith } 11242a7abf2dSChristopher Smith 11252a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1126585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 11272a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 11282a7abf2dSChristopher Smith return false; 11292a7abf2dSChristopher Smith } 11302a7abf2dSChristopher Smith } 11312a7abf2dSChristopher Smith 1132*59bc3b48SGerrit Uitslag $deleted = array(); 1133585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 113473012efdSChristopher Smith if($auth->triggerUserMod('delete', array($deleted))) { 11352a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11362a7abf2dSChristopher Smith auth_logoff(); 11372a7abf2dSChristopher Smith return true; 11382a7abf2dSChristopher Smith } 11392a7abf2dSChristopher Smith 11402a7abf2dSChristopher Smith return false; 11412a7abf2dSChristopher Smith} 11422a7abf2dSChristopher Smith 11438b06d178Schris/** 11448b06d178Schris * Send a new password 11458b06d178Schris * 11461d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11471d5856cfSAndreas Gohr * 11481d5856cfSAndreas Gohr * - handling the first request of password reset 11491d5856cfSAndreas Gohr * - validating the password reset auth token 11501d5856cfSAndreas Gohr * 11518b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11528b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11531d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11548b06d178Schris * 11558b06d178Schris * @return bool true on success, false on any error 11568b06d178Schris */ 11578b06d178Schrisfunction act_resendpwd() { 11588b06d178Schris global $lang; 11598b06d178Schris global $conf; 116027058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1161cd52f92dSchris global $auth; 1162bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1163bcc94b2cSAndreas Gohr global $INPUT; 11648b06d178Schris 11653a48618aSAnika Henke if(!actionOK('resendpwd')) { 11668b06d178Schris msg($lang['resendna'], -1); 11678b06d178Schris return false; 11688b06d178Schris } 11698b06d178Schris 1170bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11718b06d178Schris 11721d5856cfSAndreas Gohr if($token) { 1173cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11741d5856cfSAndreas Gohr 11751d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 11761d5856cfSAndreas Gohr if(!@file_exists($tfile)) { 11771d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1178bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11791d5856cfSAndreas Gohr return false; 11801d5856cfSAndreas Gohr } 11818a9735e3SAndreas Gohr // token is only valid for 3 days 11828a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11838a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1184bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11851d5856cfSAndreas Gohr @unlink($tfile); 11868a9735e3SAndreas Gohr return false; 11878a9735e3SAndreas Gohr } 11888a9735e3SAndreas Gohr 11898b06d178Schris $user = io_readfile($tfile); 11902dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 11918b06d178Schris if(!$userinfo['mail']) { 11928b06d178Schris msg($lang['resendpwdnouser'], -1); 11938b06d178Schris return false; 11948b06d178Schris } 11958b06d178Schris 1196cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1197bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1198bcc94b2cSAndreas Gohr 1199cc204bbdSAndreas Gohr // password given correctly? 1200bcc94b2cSAndreas Gohr if(!$pass) return false; 1201bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1202451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1203cc204bbdSAndreas Gohr return false; 1204cc204bbdSAndreas Gohr } 1205cc204bbdSAndreas Gohr 1206bcc94b2cSAndreas Gohr // change it 1207cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1208cc204bbdSAndreas Gohr msg('error modifying user data', -1); 1209cc204bbdSAndreas Gohr return false; 1210cc204bbdSAndreas Gohr } 1211cc204bbdSAndreas Gohr 1212cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1213cc204bbdSAndreas Gohr 12148a285f7fSAndreas Gohr $pass = auth_pwgen($user); 12157d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 12168b06d178Schris msg('error modifying user data', -1); 12178b06d178Schris return false; 12188b06d178Schris } 12198b06d178Schris 12208b06d178Schris if(auth_sendPassword($user, $pass)) { 12218b06d178Schris msg($lang['resendpwdsuccess'], 1); 12228b06d178Schris } else { 12238b06d178Schris msg($lang['regmailfail'], -1); 12248b06d178Schris } 1225cc204bbdSAndreas Gohr } 1226cc204bbdSAndreas Gohr 1227cc204bbdSAndreas Gohr @unlink($tfile); 12288b06d178Schris return true; 12291d5856cfSAndreas Gohr 12301d5856cfSAndreas Gohr } else { 12311d5856cfSAndreas Gohr // we're in request phase 12321d5856cfSAndreas Gohr 1233bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 12341d5856cfSAndreas Gohr 1235bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 12361d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12371d5856cfSAndreas Gohr return false; 12381d5856cfSAndreas Gohr } else { 1239bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12401d5856cfSAndreas Gohr } 12411d5856cfSAndreas Gohr 12422dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 12431d5856cfSAndreas Gohr if(!$userinfo['mail']) { 12441d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12451d5856cfSAndreas Gohr return false; 12461d5856cfSAndreas Gohr } 12471d5856cfSAndreas Gohr 12481d5856cfSAndreas Gohr // generate auth token 1249483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12501d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 12511d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 12521d5856cfSAndreas Gohr 12531d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12541d5856cfSAndreas Gohr 12551d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1256d7169d19SAndreas Gohr $trep = array( 1257d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1258d7169d19SAndreas Gohr 'LOGIN' => $user, 1259d7169d19SAndreas Gohr 'CONFIRM' => $url 1260d7169d19SAndreas Gohr ); 12611d5856cfSAndreas Gohr 1262d7169d19SAndreas Gohr $mail = new Mailer(); 1263d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1264d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1265d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1266d7169d19SAndreas Gohr if($mail->send()) { 12671d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12681d5856cfSAndreas Gohr } else { 12691d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12701d5856cfSAndreas Gohr } 12711d5856cfSAndreas Gohr return true; 12721d5856cfSAndreas Gohr } 1273ab5d26daSAndreas Gohr // never reached 12748b06d178Schris} 12758b06d178Schris 12768b06d178Schris/** 1277b0855b11Sandi * Encrypts a password using the given method and salt 1278b0855b11Sandi * 1279b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1280b0855b11Sandi * is chosen. 1281b0855b11Sandi * 1282b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1283ab5d26daSAndreas Gohr * @param string $clear The clear text password 1284ab5d26daSAndreas Gohr * @param string $method The hashing method 1285ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1286b0855b11Sandi * @return string The crypted password 1287b0855b11Sandi */ 1288577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1289b0855b11Sandi global $conf; 1290b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 129110a76f6fSfrank 12923a0a2d05SAndreas Gohr $pass = new PassHash(); 12933a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1294b0855b11Sandi 12953a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1296b0855b11Sandi msg("Unsupported crypt method $method", -1); 12973a0a2d05SAndreas Gohr return false; 1298b0855b11Sandi } 12993a0a2d05SAndreas Gohr 13003a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1301b0855b11Sandi} 1302b0855b11Sandi 1303b0855b11Sandi/** 1304b0855b11Sandi * Verifies a cleartext password against a crypted hash 1305b0855b11Sandi * 1306b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1307ab5d26daSAndreas Gohr * @param string $clear The clear text password 1308ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1309ab5d26daSAndreas Gohr * @return bool true if both match 1310b0855b11Sandi */ 1311b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 13123a0a2d05SAndreas Gohr $pass = new PassHash(); 13133a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1314b0855b11Sandi} 1315340756e4Sandi 1316a0b5b007SChris Smith/** 1317a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1318a0b5b007SChris Smith * 1319a0b5b007SChris Smith * @param string $user username 1320a0b5b007SChris Smith * @param string $pass encrypted password 1321a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1322ab5d26daSAndreas Gohr * @return bool 1323a0b5b007SChris Smith */ 1324a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1325a0b5b007SChris Smith global $conf; 132627058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1327a0b5b007SChris Smith global $auth; 132879d00841SOliver Geisen global $USERINFO; 1329a0b5b007SChris Smith 1330beca106aSAdrian Lang if(!$auth) return false; 1331a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1332a0b5b007SChris Smith 1333a0b5b007SChris Smith // set cookie 1334645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 133573ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1336c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 133773ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 133855a71a16SGerrit Uitslag 1339a0b5b007SChris Smith // set session 1340a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1341234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1342a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1343a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1344a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1345ab5d26daSAndreas Gohr 1346ab5d26daSAndreas Gohr return true; 1347a0b5b007SChris Smith} 1348a0b5b007SChris Smith 1349645c0a36SAndreas Gohr/** 1350645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1351645c0a36SAndreas Gohr * 1352645c0a36SAndreas Gohr * @returns array 1353645c0a36SAndreas Gohr */ 1354645c0a36SAndreas Gohrfunction auth_getCookie() { 1355c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1356c66972f2SAdrian Lang return array(null, null, null); 1357c66972f2SAdrian Lang } 1358645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1359645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1360645c0a36SAndreas Gohr $pass = base64_decode($pass); 1361645c0a36SAndreas Gohr $user = base64_decode($user); 1362645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1363645c0a36SAndreas Gohr} 1364645c0a36SAndreas Gohr 1365e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1366