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 32*ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 33*ab5d26daSAndreas Gohr * @return bool 3416905344SAndreas Gohr */ 3516905344SAndreas Gohrfunction auth_setup() { 36742c66f8Schris global $conf; 37*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 3803c4aec3Schris global $auth; 399a9714acSDominik Eckelmann global $AUTH_ACL; 409a9714acSDominik Eckelmann global $lang; 419a9714acSDominik Eckelmann $AUTH_ACL = array(); 4203c4aec3Schris 4316905344SAndreas Gohr if(!$conf['useacl']) return false; 4416905344SAndreas Gohr 4516905344SAndreas Gohr // load the the backend auth functions and instantiate the auth object XXX 468b06d178Schris if(@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) { 478b06d178Schris require_once(DOKU_INC.'inc/auth/basic.class.php'); 488b06d178Schris require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php'); 498b06d178Schris 508b06d178Schris $auth_class = "auth_".$conf['authtype']; 51cd52f92dSchris if(class_exists($auth_class)) { 528b06d178Schris $auth = new $auth_class(); 53d2dde4ebSMatthias Grimm if($auth->success == false) { 540f4f4adfSAndreas Gohr // degrade to unauthenticated user 55d2dde4ebSMatthias Grimm unset($auth); 560f4f4adfSAndreas Gohr auth_logoff(); 57cd52f92dSchris msg($lang['authtempfail'], -1); 58d2dde4ebSMatthias Grimm } 598b06d178Schris } else { 603816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 61cd52f92dSchris } 62cd52f92dSchris } else { 633816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 648b06d178Schris } 65f3f0262cSandi 66*ab5d26daSAndreas Gohr if(!$auth) return false; 6716905344SAndreas Gohr 6816905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 69bbbd6568SAndreas Gohr if(!isset($_REQUEST['u'])) $_REQUEST['u'] = ''; 70bbbd6568SAndreas Gohr if(!isset($_REQUEST['p'])) $_REQUEST['p'] = ''; 71bbbd6568SAndreas Gohr if(!isset($_REQUEST['r'])) $_REQUEST['r'] = ''; 72b2c0d874SGina Haeussge $_REQUEST['http_credentials'] = false; 7317f89d7eSMichael Klier if(!$conf['rememberme']) $_REQUEST['r'] = false; 74bbbd6568SAndreas Gohr 75b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 76b2665af7SMichael Hamann // the one presented at 77b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 78b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 79b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 80b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 81528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 8206156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 83528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 84528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 85528ddc7cSAndreas Gohr } 86528ddc7cSAndreas Gohr 871e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 884a26ad85Schris if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 891e8c9c90SAndreas Gohr $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER']; 901e8c9c90SAndreas Gohr $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW']; 91b2c0d874SGina Haeussge $_REQUEST['http_credentials'] = true; 921e8c9c90SAndreas Gohr } 931e8c9c90SAndreas Gohr 94191bb90aSAndreas Gohr // apply cleaning 95191bb90aSAndreas Gohr $_REQUEST['u'] = $auth->cleanUser($_REQUEST['u']); 96191bb90aSAndreas Gohr 97c66972f2SAdrian Lang if(isset($_REQUEST['authtok'])) { 98f13fa892SAndreas Gohr // when an authentication token is given, trust the session 99f13fa892SAndreas Gohr auth_validateToken($_REQUEST['authtok']); 100f13fa892SAndreas Gohr } elseif(!is_null($auth) && $auth->canDo('external')) { 101f13fa892SAndreas Gohr // external trust mechanism in place 102f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'], $_REQUEST['p'], $_REQUEST['r']); 103f5cb575dSAndreas Gohr } else { 1046080c584SRobin Gareus $evdata = array( 1056080c584SRobin Gareus 'user' => $_REQUEST['u'], 1066080c584SRobin Gareus 'password' => $_REQUEST['p'], 1076080c584SRobin Gareus 'sticky' => $_REQUEST['r'], 1086080c584SRobin Gareus 'silent' => $_REQUEST['http_credentials'], 1096080c584SRobin Gareus ); 110b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 111f5cb575dSAndreas Gohr } 112f5cb575dSAndreas Gohr 11316905344SAndreas Gohr //load ACL into a global array XXX 11475c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 115*ab5d26daSAndreas Gohr 116*ab5d26daSAndreas Gohr return true; 11775c93b77SAndreas Gohr} 11875c93b77SAndreas Gohr 11975c93b77SAndreas Gohr/** 12075c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12175c93b77SAndreas Gohr * 12275c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 123*ab5d26daSAndreas Gohr * @return array 12475c93b77SAndreas Gohr */ 12575c93b77SAndreas Gohrfunction auth_loadACL() { 12675c93b77SAndreas Gohr global $config_cascade; 12775c93b77SAndreas Gohr 12875c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 12975c93b77SAndreas Gohr 13075c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13175c93b77SAndreas Gohr 132191bb90aSAndreas Gohr //support user wildcard 133f8cc3354SGuy Brand if(isset($_SERVER['REMOTE_USER'])) { 13475c93b77SAndreas Gohr $len = count($acl); 13575c93b77SAndreas Gohr for($i = 0; $i < $len; $i++) { 13675c93b77SAndreas Gohr if($acl[$i]{0} == '#') continue; 13775c93b77SAndreas Gohr list($id, $rest) = preg_split('/\s+/', $acl[$i], 2); 13875c93b77SAndreas Gohr $id = str_replace('%USER%', cleanID($_SERVER['REMOTE_USER']), $id); 13975c93b77SAndreas Gohr $rest = str_replace('%USER%', auth_nameencode($_SERVER['REMOTE_USER']), $rest); 14075c93b77SAndreas Gohr $acl[$i] = "$id\t$rest"; 141a8fe108bSGuy Brand } 14211799630Sandi } 14375c93b77SAndreas Gohr return $acl; 144f3f0262cSandi} 145f3f0262cSandi 146*ab5d26daSAndreas Gohr/** 147*ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 148*ab5d26daSAndreas Gohr * 149*ab5d26daSAndreas Gohr * @param $evdata 150*ab5d26daSAndreas Gohr * @return bool 151*ab5d26daSAndreas Gohr */ 152b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 153*ab5d26daSAndreas Gohr return auth_login( 154*ab5d26daSAndreas Gohr $evdata['user'], 155b5ee21aaSAdrian Lang $evdata['password'], 156b5ee21aaSAdrian Lang $evdata['sticky'], 157*ab5d26daSAndreas Gohr $evdata['silent'] 158*ab5d26daSAndreas Gohr ); 159b5ee21aaSAdrian Lang} 160b5ee21aaSAdrian Lang 161f3f0262cSandi/** 162f3f0262cSandi * This tries to login the user based on the sent auth credentials 163f3f0262cSandi * 164f3f0262cSandi * The authentication works like this: if a username was given 16515fae107Sandi * a new login is assumed and user/password are checked. If they 16615fae107Sandi * are correct the password is encrypted with blowfish and stored 16715fae107Sandi * together with the username in a cookie - the same info is stored 16815fae107Sandi * in the session, too. Additonally a browserID is stored in the 16915fae107Sandi * session. 17015fae107Sandi * 17115fae107Sandi * If no username was given the cookie is checked: if the username, 17215fae107Sandi * crypted password and browserID match between session and cookie 17315fae107Sandi * no further testing is done and the user is accepted 17415fae107Sandi * 17515fae107Sandi * If a cookie was found but no session info was availabe the 176136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 17715fae107Sandi * together with username rechecked by calling this function again. 178f3f0262cSandi * 179f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 180f3f0262cSandi * are set. 18115fae107Sandi * 18215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 18315fae107Sandi * 18415fae107Sandi * @param string $user Username 18515fae107Sandi * @param string $pass Cleartext Password 18615fae107Sandi * @param bool $sticky Cookie should not expire 187f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 18815fae107Sandi * @return bool true on successful auth 189f3f0262cSandi */ 190f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 191f3f0262cSandi global $USERINFO; 192f3f0262cSandi global $conf; 193f3f0262cSandi global $lang; 194*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 195cd52f92dSchris global $auth; 196*ab5d26daSAndreas Gohr 197132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 198f3f0262cSandi 199beca106aSAdrian Lang if(!$auth) return false; 200beca106aSAdrian Lang 201bbbd6568SAndreas Gohr if(!empty($user)) { 202132bdbfeSandi //usual login 203cd52f92dSchris if($auth->checkPass($user, $pass)) { 204132bdbfeSandi // make logininfo globally available 205f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 20632ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 207e940aea4SAndreas Gohr auth_setCookie($user, PMA_blowfish_encrypt($pass, $secret), $sticky); 208132bdbfeSandi return true; 209f3f0262cSandi } else { 210f3f0262cSandi //invalid credentials - log off 211f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 212f3f0262cSandi auth_logoff(); 213132bdbfeSandi return false; 214f3f0262cSandi } 215f3f0262cSandi } else { 216132bdbfeSandi // read cookie information 217645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 218132bdbfeSandi if($user && $pass) { 219132bdbfeSandi // we got a cookie - see if we can trust it 220fa7c70ffSAdrian Lang 221fa7c70ffSAdrian Lang // get session info 222fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 223132bdbfeSandi if(isset($session) && 2247172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2254c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 226132bdbfeSandi ($session['user'] == $user) && 227234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 228*ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 229*ab5d26daSAndreas Gohr ) { 230234ce57eSAndreas Gohr 231132bdbfeSandi // he has session, cookie and browser right - let him in 232132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 233132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 234132bdbfeSandi return true; 235132bdbfeSandi } 236f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 23732ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 238e940aea4SAndreas Gohr $pass = PMA_blowfish_decrypt($pass, $secret); 239f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 240132bdbfeSandi } 241132bdbfeSandi } 242f3f0262cSandi //just to be sure 243883179a4SAndreas Gohr auth_logoff(true); 244132bdbfeSandi return false; 245f3f0262cSandi} 246132bdbfeSandi 247132bdbfeSandi/** 248f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 249f13fa892SAndreas Gohr * 250f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 251f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 252f13fa892SAndreas Gohr * 253f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 254f13fa892SAndreas Gohr * @param string $token The authentication token 255f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 256f13fa892SAndreas Gohr */ 257f13fa892SAndreas Gohrfunction auth_validateToken($token) { 258f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']) { 259f13fa892SAndreas Gohr // bad token 260f13fa892SAndreas Gohr header("HTTP/1.0 401 Unauthorized"); 261f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 262f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 263f13fa892SAndreas Gohr exit; 264f13fa892SAndreas Gohr } 265f13fa892SAndreas Gohr // still here? trust the session data 266f13fa892SAndreas Gohr global $USERINFO; 267f13fa892SAndreas Gohr $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user']; 268f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 269f13fa892SAndreas Gohr return true; 270f13fa892SAndreas Gohr} 271f13fa892SAndreas Gohr 272f13fa892SAndreas Gohr/** 273f13fa892SAndreas Gohr * Create an auth token and store it in the session 274f13fa892SAndreas Gohr * 275f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 276f13fa892SAndreas Gohr * 277f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 278f13fa892SAndreas Gohr * @return string The auth token 279f13fa892SAndreas Gohr */ 280f13fa892SAndreas Gohrfunction auth_createToken() { 281f13fa892SAndreas Gohr $token = md5(mt_rand()); 28209c2d803SAndreas Gohr @session_start(); // reopen the session if needed 283f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 28409c2d803SAndreas Gohr session_write_close(); 285f13fa892SAndreas Gohr return $token; 286f13fa892SAndreas Gohr} 287f13fa892SAndreas Gohr 288f13fa892SAndreas Gohr/** 289136ce040Sandi * Builds a pseudo UID from browser and IP data 290132bdbfeSandi * 291132bdbfeSandi * This is neither unique nor unfakable - still it adds some 292136ce040Sandi * security. Using the first part of the IP makes sure 293136ce040Sandi * proxy farms like AOLs are stil okay. 29415fae107Sandi * 29515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 29615fae107Sandi * 29715fae107Sandi * @return string a MD5 sum of various browser headers 298132bdbfeSandi */ 299132bdbfeSandifunction auth_browseruid() { 3002f9daf16SAndreas Gohr $ip = clientIP(true); 301132bdbfeSandi $uid = ''; 302132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 303132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 304132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 305132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 3062f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 307132bdbfeSandi return md5($uid); 308132bdbfeSandi} 309132bdbfeSandi 310132bdbfeSandi/** 311132bdbfeSandi * Creates a random key to encrypt the password in cookies 31215fae107Sandi * 31315fae107Sandi * This function tries to read the password for encrypting 31498407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 31515fae107Sandi * if no such file is found a random key is created and 31615fae107Sandi * and stored in this file. 31715fae107Sandi * 31815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 31932ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 32015fae107Sandi * @return string 321132bdbfeSandi */ 32232ed2b36SAndreas Gohrfunction auth_cookiesalt($addsession = false) { 323132bdbfeSandi global $conf; 32498407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 325132bdbfeSandi $salt = io_readFile($file); 326132bdbfeSandi if(empty($salt)) { 327132bdbfeSandi $salt = uniqid(rand(), true); 328132bdbfeSandi io_saveFile($file, $salt); 329132bdbfeSandi } 33032ed2b36SAndreas Gohr if($addsession) { 33132ed2b36SAndreas Gohr $salt .= session_id(); 33232ed2b36SAndreas Gohr } 333132bdbfeSandi return $salt; 334f3f0262cSandi} 335f3f0262cSandi 336f3f0262cSandi/** 337883179a4SAndreas Gohr * Log out the current user 338883179a4SAndreas Gohr * 339f3f0262cSandi * This clears all authentication data and thus log the user 340883179a4SAndreas Gohr * off. It also clears session data. 34115fae107Sandi * 34215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 343883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 344f3f0262cSandi */ 345883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 346f3f0262cSandi global $conf; 347f3f0262cSandi global $USERINFO; 348*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 3495298a619SAndreas Gohr global $auth; 35037065e65Sandi 351d4869846SAndreas Gohr // make sure the session is writable (it usually is) 352e9621d07SAndreas Gohr @session_start(); 353e9621d07SAndreas Gohr 354e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 355e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 356e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 357e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 358e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 359e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 360883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 361e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 36237065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 363f3f0262cSandi unset($_SERVER['REMOTE_USER']); 364132bdbfeSandi $USERINFO = null; //FIXME 365f5c6743cSAndreas Gohr 36673ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 367f5c6743cSAndreas Gohr if(version_compare(PHP_VERSION, '5.2.0', '>')) { 36873ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 369f5c6743cSAndreas Gohr } else { 37073ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 371f5c6743cSAndreas Gohr } 3725298a619SAndreas Gohr 373880f62faSAndreas Gohr if($auth) $auth->logOff(); 374f3f0262cSandi} 375f3f0262cSandi 376f3f0262cSandi/** 377f8cc712eSAndreas Gohr * Check if a user is a manager 378f8cc712eSAndreas Gohr * 379f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 380f8cc712eSAndreas Gohr * user. 381f8cc712eSAndreas Gohr * 382f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 383f8cc712eSAndreas Gohr * 384f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 385f8cc712eSAndreas Gohr * @see auth_isadmin 386*ab5d26daSAndreas Gohr * @param string $user Username 387*ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 388*ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 389*ab5d26daSAndreas Gohr * @return bool 390f8cc712eSAndreas Gohr */ 391f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 392f8cc712eSAndreas Gohr global $conf; 393f8cc712eSAndreas Gohr global $USERINFO; 394*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 395d752aedeSAndreas Gohr global $auth; 396f8cc712eSAndreas Gohr 397beca106aSAdrian Lang if(!$auth) return false; 398c66972f2SAdrian Lang if(is_null($user)) { 399c66972f2SAdrian Lang if(!isset($_SERVER['REMOTE_USER'])) { 400c66972f2SAdrian Lang return false; 401c66972f2SAdrian Lang } else { 402c66972f2SAdrian Lang $user = $_SERVER['REMOTE_USER']; 403c66972f2SAdrian Lang } 404c66972f2SAdrian Lang } 405d6dc956fSAndreas Gohr if(is_null($groups)) { 406d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 407e259aa79SAndreas Gohr } 408e259aa79SAndreas Gohr 409d6dc956fSAndreas Gohr // check superuser match 410d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 411d6dc956fSAndreas Gohr if($adminonly) return false; 412e259aa79SAndreas Gohr // check managers 413d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 41400ce12daSChris Smith 415f8cc712eSAndreas Gohr return false; 416f8cc712eSAndreas Gohr} 417f8cc712eSAndreas Gohr 418f8cc712eSAndreas Gohr/** 419f8cc712eSAndreas Gohr * Check if a user is admin 420f8cc712eSAndreas Gohr * 421f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 422f8cc712eSAndreas Gohr * 423f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 424f8cc712eSAndreas Gohr * 425f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 426*ab5d26daSAndreas Gohr * @see auth_ismanager() 427*ab5d26daSAndreas Gohr * @param string $user Username 428*ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 429*ab5d26daSAndreas Gohr * @return bool 430f8cc712eSAndreas Gohr */ 431f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 432f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 433f8cc712eSAndreas Gohr} 434f8cc712eSAndreas Gohr 435d6dc956fSAndreas Gohr/** 436d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 437d6dc956fSAndreas Gohr * users and groups to determine membership status 438d6dc956fSAndreas Gohr * 439d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 440d6dc956fSAndreas Gohr * 441d6dc956fSAndreas Gohr * @param $memberlist string commaseparated list of allowed users and groups 442d6dc956fSAndreas Gohr * @param $user string user to match against 443d6dc956fSAndreas Gohr * @param $groups array groups the user is member of 4445446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 445d6dc956fSAndreas Gohr */ 446d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 447*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 448d6dc956fSAndreas Gohr global $auth; 449d6dc956fSAndreas Gohr if(!$auth) return false; 450d6dc956fSAndreas Gohr 451d6dc956fSAndreas Gohr // clean user and groups 4524f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 453d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 454d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 455d6dc956fSAndreas Gohr } 456d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 457d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 458d6dc956fSAndreas Gohr 459d6dc956fSAndreas Gohr // extract the memberlist 460d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 461d6dc956fSAndreas Gohr $members = array_map('trim', $members); 462d6dc956fSAndreas Gohr $members = array_unique($members); 463d6dc956fSAndreas Gohr $members = array_filter($members); 464d6dc956fSAndreas Gohr 465d6dc956fSAndreas Gohr // compare cleaned values 466d6dc956fSAndreas Gohr foreach($members as $member) { 4674f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 468d6dc956fSAndreas Gohr if($member[0] == '@') { 469d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 470d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 471d6dc956fSAndreas Gohr } else { 472d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 473d6dc956fSAndreas Gohr if($member == $user) return true; 474d6dc956fSAndreas Gohr } 475d6dc956fSAndreas Gohr } 476d6dc956fSAndreas Gohr 477d6dc956fSAndreas Gohr // still here? not a member! 478d6dc956fSAndreas Gohr return false; 479d6dc956fSAndreas Gohr} 480d6dc956fSAndreas Gohr 481f8cc712eSAndreas Gohr/** 48215fae107Sandi * Convinience function for auth_aclcheck() 48315fae107Sandi * 48415fae107Sandi * This checks the permissions for the current user 48515fae107Sandi * 48615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 48715fae107Sandi * 4881698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 48915fae107Sandi * @return int permission level 490f3f0262cSandi */ 491f3f0262cSandifunction auth_quickaclcheck($id) { 492f3f0262cSandi global $conf; 493f3f0262cSandi global $USERINFO; 494f3f0262cSandi # if no ACL is used always return upload rights 495f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 496f3f0262cSandi return auth_aclcheck($id, $_SERVER['REMOTE_USER'], $USERINFO['grps']); 497f3f0262cSandi} 498f3f0262cSandi 499f3f0262cSandi/** 500f3f0262cSandi * Returns the maximum rights a user has for 501f3f0262cSandi * the given ID or its namespace 50215fae107Sandi * 50315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 50415fae107Sandi * 5051698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 50615fae107Sandi * @param string $user Username 50715fae107Sandi * @param array $groups Array of groups the user is in 50815fae107Sandi * @return int permission level 509f3f0262cSandi */ 510f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 511f3f0262cSandi global $conf; 512f3f0262cSandi global $AUTH_ACL; 513*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 514d752aedeSAndreas Gohr global $auth; 515f3f0262cSandi 51685d03f68SAndreas Gohr // if no ACL is used always return upload rights 517f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 518beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 519f3f0262cSandi 520074cf26bSandi //make sure groups is an array 521074cf26bSandi if(!is_array($groups)) $groups = array(); 522074cf26bSandi 52385d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 524*ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 525*ab5d26daSAndreas Gohr return AUTH_ADMIN; 526*ab5d26daSAndreas Gohr } 52785d03f68SAndreas Gohr 528e259aa79SAndreas Gohr $ci = ''; 529e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()) $ci = 'ui'; 530d752aedeSAndreas Gohr 531d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 532d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 53385d03f68SAndreas Gohr $user = auth_nameencode($user); 53485d03f68SAndreas Gohr 5356c2bb100SAndreas Gohr //prepend groups with @ and nameencode 5362cd2db38Sandi $cnt = count($groups); 5372cd2db38Sandi for($i = 0; $i < $cnt; $i++) { 5386c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 53910a76f6fSfrank } 54010a76f6fSfrank 541f3f0262cSandi $ns = getNS($id); 542f3f0262cSandi $perm = -1; 543f3f0262cSandi 54434aeb4afSAndreas Gohr if($user || count($groups)) { 545f3f0262cSandi //add ALL group 546f3f0262cSandi $groups[] = '@ALL'; 547f3f0262cSandi //add User 54834aeb4afSAndreas Gohr if($user) $groups[] = $user; 549f3f0262cSandi } else { 55048d7b7a6SDominik Eckelmann $groups[] = '@ALL'; 551f3f0262cSandi } 552f3f0262cSandi 553f3f0262cSandi //check exact match first 55448d7b7a6SDominik Eckelmann $matches = preg_grep('/^'.preg_quote($id, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL); 555f3f0262cSandi if(count($matches)) { 556f3f0262cSandi foreach($matches as $match) { 557f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 558f3f0262cSandi $acl = preg_split('/\s+/', $match); 55948d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 56048d7b7a6SDominik Eckelmann continue; 56148d7b7a6SDominik Eckelmann } 5628ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 563f3f0262cSandi if($acl[2] > $perm) { 564f3f0262cSandi $perm = $acl[2]; 565f3f0262cSandi } 566f3f0262cSandi } 567f3f0262cSandi if($perm > -1) { 568f3f0262cSandi //we had a match - return it 569f3f0262cSandi return $perm; 570f3f0262cSandi } 571f3f0262cSandi } 572f3f0262cSandi 573f3f0262cSandi //still here? do the namespace checks 574f3f0262cSandi if($ns) { 5753e304b55SMichael Hamann $path = $ns.':*'; 576f3f0262cSandi } else { 5773e304b55SMichael Hamann $path = '*'; //root document 578f3f0262cSandi } 579f3f0262cSandi 580f3f0262cSandi do { 58148d7b7a6SDominik Eckelmann $matches = preg_grep('/^'.preg_quote($path, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL); 582f3f0262cSandi if(count($matches)) { 583f3f0262cSandi foreach($matches as $match) { 584f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 585f3f0262cSandi $acl = preg_split('/\s+/', $match); 58648d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 58748d7b7a6SDominik Eckelmann continue; 58848d7b7a6SDominik Eckelmann } 5898ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 590f3f0262cSandi if($acl[2] > $perm) { 591f3f0262cSandi $perm = $acl[2]; 592f3f0262cSandi } 593f3f0262cSandi } 594f3f0262cSandi //we had a match - return it 59548d7b7a6SDominik Eckelmann if($perm != -1) { 596f3f0262cSandi return $perm; 597f3f0262cSandi } 59848d7b7a6SDominik Eckelmann } 599f3f0262cSandi //get next higher namespace 600f3f0262cSandi $ns = getNS($ns); 601f3f0262cSandi 6023e304b55SMichael Hamann if($path != '*') { 6033e304b55SMichael Hamann $path = $ns.':*'; 6043e304b55SMichael Hamann if($path == ':*') $path = '*'; 605f3f0262cSandi } else { 606f3f0262cSandi //we did this already 607f3f0262cSandi //looks like there is something wrong with the ACL 608f3f0262cSandi //break here 609d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 610d5ce66f6SAndreas Gohr return AUTH_NONE; 611f3f0262cSandi } 612f3f0262cSandi } while(1); //this should never loop endless 613*ab5d26daSAndreas Gohr return AUTH_NONE; 614f3f0262cSandi} 615f3f0262cSandi 616f3f0262cSandi/** 6176c2bb100SAndreas Gohr * Encode ASCII special chars 6186c2bb100SAndreas Gohr * 6196c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 6206c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 6216c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 6226c2bb100SAndreas Gohr * urlencoding!). 6236c2bb100SAndreas Gohr * 6246c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 6256c2bb100SAndreas Gohr * 6266c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 6276c2bb100SAndreas Gohr * @see rawurldecode() 6286c2bb100SAndreas Gohr */ 629e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 630a424cd8eSchris global $cache_authname; 631a424cd8eSchris $cache =& $cache_authname; 63231784267SAndreas Gohr $name = (string) $name; 633a424cd8eSchris 63480601d26SAndreas Gohr // never encode wildcard FS#1955 63580601d26SAndreas Gohr if($name == '%USER%') return $name; 63680601d26SAndreas Gohr 637a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 638e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 639*ab5d26daSAndreas Gohr $cache[$name][$skip_group] = '@'.preg_replace( 640*ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 641*ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", substr($name, 1) 642*ab5d26daSAndreas Gohr ); 643e838fc2eSAndreas Gohr } else { 644*ab5d26daSAndreas Gohr $cache[$name][$skip_group] = preg_replace( 645*ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 646*ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", $name 647*ab5d26daSAndreas Gohr ); 648e838fc2eSAndreas Gohr } 6496c2bb100SAndreas Gohr } 6506c2bb100SAndreas Gohr 651a424cd8eSchris return $cache[$name][$skip_group]; 652a424cd8eSchris} 653a424cd8eSchris 6546c2bb100SAndreas Gohr/** 655f3f0262cSandi * Create a pronouncable password 656f3f0262cSandi * 65715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 65815fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 65915fae107Sandi * 66015fae107Sandi * @return string pronouncable password 661f3f0262cSandi */ 662f3f0262cSandifunction auth_pwgen() { 663f3f0262cSandi $pw = ''; 664f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 665f3f0262cSandi $v = 'aeiou'; //vowels 666f3f0262cSandi $a = $c.$v; //both 667f3f0262cSandi 668f3f0262cSandi //use two syllables... 669f3f0262cSandi for($i = 0; $i < 2; $i++) { 670f3f0262cSandi $pw .= $c[rand(0, strlen($c) - 1)]; 671f3f0262cSandi $pw .= $v[rand(0, strlen($v) - 1)]; 672f3f0262cSandi $pw .= $a[rand(0, strlen($a) - 1)]; 673f3f0262cSandi } 674f3f0262cSandi //... and add a nice number 675f3f0262cSandi $pw .= rand(10, 99); 676f3f0262cSandi 677f3f0262cSandi return $pw; 678f3f0262cSandi} 679f3f0262cSandi 680f3f0262cSandi/** 681f3f0262cSandi * Sends a password to the given user 682f3f0262cSandi * 68315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 684*ab5d26daSAndreas Gohr * @param string $user Login name of the user 685*ab5d26daSAndreas Gohr * @param string $password The new password in clear text 68615fae107Sandi * @return bool true on success 687f3f0262cSandi */ 688f3f0262cSandifunction auth_sendPassword($user, $password) { 689f3f0262cSandi global $lang; 690*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 691cd52f92dSchris global $auth; 692beca106aSAdrian Lang if(!$auth) return false; 693cd52f92dSchris 694d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 695cd52f92dSchris $userinfo = $auth->getUserData($user); 696f3f0262cSandi 69787ddda95Sandi if(!$userinfo['mail']) return false; 698f3f0262cSandi 699f3f0262cSandi $text = rawLocale('password'); 700d7169d19SAndreas Gohr $trep = array( 701d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 702d7169d19SAndreas Gohr 'LOGIN' => $user, 703d7169d19SAndreas Gohr 'PASSWORD' => $password 704d7169d19SAndreas Gohr ); 705f3f0262cSandi 706d7169d19SAndreas Gohr $mail = new Mailer(); 707d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 708d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 709d7169d19SAndreas Gohr $mail->setBody($text, $trep); 710d7169d19SAndreas Gohr return $mail->send(); 711f3f0262cSandi} 712f3f0262cSandi 713f3f0262cSandi/** 71415fae107Sandi * Register a new user 715f3f0262cSandi * 71615fae107Sandi * This registers a new user - Data is read directly from $_POST 71715fae107Sandi * 71815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 71915fae107Sandi * @return bool true on success, false on any error 720f3f0262cSandi */ 721f3f0262cSandifunction register() { 722f3f0262cSandi global $lang; 723eb5d07e4Sjan global $conf; 724*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 725cd52f92dSchris global $auth; 726f3f0262cSandi 727f3f0262cSandi if(!$_POST['save']) return false; 7283a48618aSAnika Henke if(!actionOK('register')) return false; 729640145a5Sandi 730f3f0262cSandi //clean username 731d752aedeSAndreas Gohr $_POST['login'] = trim($auth->cleanUser($_POST['login'])); 732d752aedeSAndreas Gohr 733f3f0262cSandi //clean fullname and email 73454f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $_POST['fullname'])); 73554f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $_POST['email'])); 736f3f0262cSandi 737f3f0262cSandi if(empty($_POST['login']) || 738f3f0262cSandi empty($_POST['fullname']) || 739*ab5d26daSAndreas Gohr empty($_POST['email']) 740*ab5d26daSAndreas Gohr ) { 741f3f0262cSandi msg($lang['regmissing'], -1); 742f3f0262cSandi return false; 743f3f0262cSandi } 744f3f0262cSandi 745cab2716aSmatthias.grimm if($conf['autopasswd']) { 746cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 747cab2716aSmatthias.grimm } elseif(empty($_POST['pass']) || 748*ab5d26daSAndreas Gohr empty($_POST['passchk']) 749*ab5d26daSAndreas Gohr ) { 750bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 751cab2716aSmatthias.grimm return false; 752cab2716aSmatthias.grimm } elseif($_POST['pass'] != $_POST['passchk']) { 753bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 754cab2716aSmatthias.grimm return false; 755cab2716aSmatthias.grimm } else { 756cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 757cab2716aSmatthias.grimm } 758cab2716aSmatthias.grimm 759f3f0262cSandi //check mail 76044f669e9Sandi if(!mail_isvalid($_POST['email'])) { 761f3f0262cSandi msg($lang['regbadmail'], -1); 762f3f0262cSandi return false; 763f3f0262cSandi } 764f3f0262cSandi 765f3f0262cSandi //okay try to create the user 7667d3c8d42SGabriel Birke if(!$auth->triggerUserMod('create', array($_POST['login'], $pass, $_POST['fullname'], $_POST['email']))) { 767f3f0262cSandi msg($lang['reguexists'], -1); 768f3f0262cSandi return false; 769f3f0262cSandi } 770f3f0262cSandi 77102a498e7Schris // create substitutions for use in notification email 77202a498e7Schris $substitutions = array( 77302a498e7Schris 'NEWUSER' => $_POST['login'], 77402a498e7Schris 'NEWNAME' => $_POST['fullname'], 77502a498e7Schris 'NEWEMAIL' => $_POST['email'], 77602a498e7Schris ); 77702a498e7Schris 778cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 779cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 78002a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 781cab2716aSmatthias.grimm return true; 782cab2716aSmatthias.grimm } 783cab2716aSmatthias.grimm 784cab2716aSmatthias.grimm // autogenerated password? then send him the password 785f3f0262cSandi if(auth_sendPassword($_POST['login'], $pass)) { 786f3f0262cSandi msg($lang['regsuccess'], 1); 78702a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 788f3f0262cSandi return true; 789f3f0262cSandi } else { 790f3f0262cSandi msg($lang['regmailfail'], -1); 791f3f0262cSandi return false; 792f3f0262cSandi } 793f3f0262cSandi} 794f3f0262cSandi 79510a76f6fSfrank/** 7968b06d178Schris * Update user profile 7978b06d178Schris * 7988b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 7998b06d178Schris */ 8008b06d178Schrisfunction updateprofile() { 8018b06d178Schris global $conf; 8028b06d178Schris global $INFO; 8038b06d178Schris global $lang; 804*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 805cd52f92dSchris global $auth; 8068b06d178Schris 807bb4866bdSchris if(empty($_POST['save'])) return false; 8081b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 8098b06d178Schris 8103a48618aSAnika Henke if(!actionOK('profile')) { 8118b06d178Schris msg($lang['profna'], -1); 8128b06d178Schris return false; 8138b06d178Schris } 8148b06d178Schris 8158b06d178Schris if($_POST['newpass'] != $_POST['passchk']) { 8168b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 8178b06d178Schris return false; 8188b06d178Schris } 8198b06d178Schris 8208b06d178Schris //clean fullname and email 82154f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $_POST['fullname'])); 82254f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $_POST['email'])); 8238b06d178Schris 8244369edafSAndy Webber if((empty($_POST['fullname']) && $auth->canDo('modName')) || 825*ab5d26daSAndreas Gohr (empty($_POST['email']) && $auth->canDo('modMail')) 826*ab5d26daSAndreas Gohr ) { 8278b06d178Schris msg($lang['profnoempty'], -1); 8288b06d178Schris return false; 8298b06d178Schris } 8308b06d178Schris 8314369edafSAndy Webber if(!mail_isvalid($_POST['email']) && $auth->canDo('modMail')) { 8328b06d178Schris msg($lang['regbadmail'], -1); 8338b06d178Schris return false; 8348b06d178Schris } 8358b06d178Schris 836*ab5d26daSAndreas Gohr $changes = array(); 8374c21b7eeSAndreas Gohr if($_POST['fullname'] != $INFO['userinfo']['name'] && $auth->canDo('modName')) $changes['name'] = $_POST['fullname']; 8384c21b7eeSAndreas Gohr if($_POST['email'] != $INFO['userinfo']['mail'] && $auth->canDo('modMail')) $changes['mail'] = $_POST['email']; 839cf626a62SAndreas Gohr if(!empty($_POST['newpass']) && $auth->canDo('modPass')) $changes['pass'] = $_POST['newpass']; 8404c21b7eeSAndreas Gohr 8418b06d178Schris if(!count($changes)) { 8428b06d178Schris msg($lang['profnochange'], -1); 8438b06d178Schris return false; 8448b06d178Schris } 8458b06d178Schris 8468b06d178Schris if($conf['profileconfirm']) { 847df466c7aSAndreas Gohr if(!$auth->checkPass($_SERVER['REMOTE_USER'], $_POST['oldpass'])) { 8488b06d178Schris msg($lang['badlogin'], -1); 8498b06d178Schris return false; 8508b06d178Schris } 8518b06d178Schris } 8528b06d178Schris 853a0b5b007SChris Smith if($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) { 854a0b5b007SChris Smith // update cookie and session with the changed data 85532ed2b36SAndreas Gohr if($changes['pass']) { 856*ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 85732ed2b36SAndreas Gohr $pass = PMA_blowfish_encrypt($changes['pass'], auth_cookiesalt(!$sticky)); 858a0b5b007SChris Smith auth_setCookie($_SERVER['REMOTE_USER'], $pass, (bool) $sticky); 85932ed2b36SAndreas Gohr } 86025b2a98cSMichael Klier return true; 861a0b5b007SChris Smith } 862*ab5d26daSAndreas Gohr 863*ab5d26daSAndreas Gohr return false; 8648b06d178Schris} 8658b06d178Schris 8668b06d178Schris/** 8678b06d178Schris * Send a new password 8688b06d178Schris * 8691d5856cfSAndreas Gohr * This function handles both phases of the password reset: 8701d5856cfSAndreas Gohr * 8711d5856cfSAndreas Gohr * - handling the first request of password reset 8721d5856cfSAndreas Gohr * - validating the password reset auth token 8731d5856cfSAndreas Gohr * 8748b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 8758b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 8761d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8778b06d178Schris * 8788b06d178Schris * @return bool true on success, false on any error 8798b06d178Schris */ 8808b06d178Schrisfunction act_resendpwd() { 8818b06d178Schris global $lang; 8828b06d178Schris global $conf; 883*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 884cd52f92dSchris global $auth; 8858b06d178Schris 8863a48618aSAnika Henke if(!actionOK('resendpwd')) { 8878b06d178Schris msg($lang['resendna'], -1); 8888b06d178Schris return false; 8898b06d178Schris } 8908b06d178Schris 8911d5856cfSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $_REQUEST['pwauth']); 8928b06d178Schris 8931d5856cfSAndreas Gohr if($token) { 894cc204bbdSAndreas Gohr // we're in token phase - get user info from token 8951d5856cfSAndreas Gohr 8961d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 8971d5856cfSAndreas Gohr if(!@file_exists($tfile)) { 8981d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 899cc204bbdSAndreas Gohr unset($_REQUEST['pwauth']); 9001d5856cfSAndreas Gohr return false; 9011d5856cfSAndreas Gohr } 9028a9735e3SAndreas Gohr // token is only valid for 3 days 9038a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 9048a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 9058a9735e3SAndreas Gohr unset($_REQUEST['pwauth']); 9061d5856cfSAndreas Gohr @unlink($tfile); 9078a9735e3SAndreas Gohr return false; 9088a9735e3SAndreas Gohr } 9098a9735e3SAndreas Gohr 9108b06d178Schris $user = io_readfile($tfile); 911cd52f92dSchris $userinfo = $auth->getUserData($user); 9128b06d178Schris if(!$userinfo['mail']) { 9138b06d178Schris msg($lang['resendpwdnouser'], -1); 9148b06d178Schris return false; 9158b06d178Schris } 9168b06d178Schris 917cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 918cc204bbdSAndreas Gohr // password given correctly? 919cc204bbdSAndreas Gohr if(!isset($_REQUEST['pass']) || $_REQUEST['pass'] == '') return false; 920cc204bbdSAndreas Gohr if($_REQUEST['pass'] != $_REQUEST['passchk']) { 921451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 922cc204bbdSAndreas Gohr return false; 923cc204bbdSAndreas Gohr } 924cc204bbdSAndreas Gohr $pass = $_REQUEST['pass']; 925cc204bbdSAndreas Gohr 926cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 927cc204bbdSAndreas Gohr msg('error modifying user data', -1); 928cc204bbdSAndreas Gohr return false; 929cc204bbdSAndreas Gohr } 930cc204bbdSAndreas Gohr 931cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 932cc204bbdSAndreas Gohr 9338b06d178Schris $pass = auth_pwgen(); 9347d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 9358b06d178Schris msg('error modifying user data', -1); 9368b06d178Schris return false; 9378b06d178Schris } 9388b06d178Schris 9398b06d178Schris if(auth_sendPassword($user, $pass)) { 9408b06d178Schris msg($lang['resendpwdsuccess'], 1); 9418b06d178Schris } else { 9428b06d178Schris msg($lang['regmailfail'], -1); 9438b06d178Schris } 944cc204bbdSAndreas Gohr } 945cc204bbdSAndreas Gohr 946cc204bbdSAndreas Gohr @unlink($tfile); 9478b06d178Schris return true; 9481d5856cfSAndreas Gohr 9491d5856cfSAndreas Gohr } else { 9501d5856cfSAndreas Gohr // we're in request phase 9511d5856cfSAndreas Gohr 9521d5856cfSAndreas Gohr if(!$_POST['save']) return false; 9531d5856cfSAndreas Gohr 9541d5856cfSAndreas Gohr if(empty($_POST['login'])) { 9551d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 9561d5856cfSAndreas Gohr return false; 9571d5856cfSAndreas Gohr } else { 958d752aedeSAndreas Gohr $user = trim($auth->cleanUser($_POST['login'])); 9591d5856cfSAndreas Gohr } 9601d5856cfSAndreas Gohr 9611d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 9621d5856cfSAndreas Gohr if(!$userinfo['mail']) { 9631d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 9641d5856cfSAndreas Gohr return false; 9651d5856cfSAndreas Gohr } 9661d5856cfSAndreas Gohr 9671d5856cfSAndreas Gohr // generate auth token 9681d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 9691d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 9701d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 9711d5856cfSAndreas Gohr 9721d5856cfSAndreas Gohr io_saveFile($tfile, $user); 9731d5856cfSAndreas Gohr 9741d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 975d7169d19SAndreas Gohr $trep = array( 976d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 977d7169d19SAndreas Gohr 'LOGIN' => $user, 978d7169d19SAndreas Gohr 'CONFIRM' => $url 979d7169d19SAndreas Gohr ); 9801d5856cfSAndreas Gohr 981d7169d19SAndreas Gohr $mail = new Mailer(); 982d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 983d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 984d7169d19SAndreas Gohr $mail->setBody($text, $trep); 985d7169d19SAndreas Gohr if($mail->send()) { 9861d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 9871d5856cfSAndreas Gohr } else { 9881d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 9891d5856cfSAndreas Gohr } 9901d5856cfSAndreas Gohr return true; 9911d5856cfSAndreas Gohr } 992*ab5d26daSAndreas Gohr // never reached 9938b06d178Schris} 9948b06d178Schris 9958b06d178Schris/** 996b0855b11Sandi * Encrypts a password using the given method and salt 997b0855b11Sandi * 998b0855b11Sandi * If the selected method needs a salt and none was given, a random one 999b0855b11Sandi * is chosen. 1000b0855b11Sandi * 1001b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1002*ab5d26daSAndreas Gohr * @param string $clear The clear text password 1003*ab5d26daSAndreas Gohr * @param string $method The hashing method 1004*ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1005b0855b11Sandi * @return string The crypted password 1006b0855b11Sandi */ 1007577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1008b0855b11Sandi global $conf; 1009b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 101010a76f6fSfrank 10113a0a2d05SAndreas Gohr $pass = new PassHash(); 10123a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1013b0855b11Sandi 10143a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1015b0855b11Sandi msg("Unsupported crypt method $method", -1); 10163a0a2d05SAndreas Gohr return false; 1017b0855b11Sandi } 10183a0a2d05SAndreas Gohr 10193a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1020b0855b11Sandi} 1021b0855b11Sandi 1022b0855b11Sandi/** 1023b0855b11Sandi * Verifies a cleartext password against a crypted hash 1024b0855b11Sandi * 1025b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1026*ab5d26daSAndreas Gohr * @param string $clear The clear text password 1027*ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1028*ab5d26daSAndreas Gohr * @return bool true if both match 1029b0855b11Sandi */ 1030b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 10313a0a2d05SAndreas Gohr $pass = new PassHash(); 10323a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1033b0855b11Sandi} 1034340756e4Sandi 1035a0b5b007SChris Smith/** 1036a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1037a0b5b007SChris Smith * 1038a0b5b007SChris Smith * @param string $user username 1039a0b5b007SChris Smith * @param string $pass encrypted password 1040a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1041*ab5d26daSAndreas Gohr * @return bool 1042a0b5b007SChris Smith */ 1043a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1044a0b5b007SChris Smith global $conf; 1045*ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 1046a0b5b007SChris Smith global $auth; 104779d00841SOliver Geisen global $USERINFO; 1048a0b5b007SChris Smith 1049beca106aSAdrian Lang if(!$auth) return false; 1050a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1051a0b5b007SChris Smith 1052a0b5b007SChris Smith // set cookie 1053645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 105473ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1055c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1056a0b5b007SChris Smith if(version_compare(PHP_VERSION, '5.2.0', '>')) { 105773ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 1058a0b5b007SChris Smith } else { 105973ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 1060a0b5b007SChris Smith } 1061a0b5b007SChris Smith // set session 1062a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1063234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1064a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1065a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1066a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1067*ab5d26daSAndreas Gohr 1068*ab5d26daSAndreas Gohr return true; 1069a0b5b007SChris Smith} 1070a0b5b007SChris Smith 1071645c0a36SAndreas Gohr/** 1072645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1073645c0a36SAndreas Gohr * 1074645c0a36SAndreas Gohr * @returns array 1075645c0a36SAndreas Gohr */ 1076645c0a36SAndreas Gohrfunction auth_getCookie() { 1077c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1078c66972f2SAdrian Lang return array(null, null, null); 1079c66972f2SAdrian Lang } 1080645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1081645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1082645c0a36SAndreas Gohr $pass = base64_decode($pass); 1083645c0a36SAndreas Gohr $user = base64_decode($user); 1084645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1085645c0a36SAndreas Gohr} 1086645c0a36SAndreas Gohr 1087e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1088