1f3f0262cSandi<? 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 12f3f0262cSandi require_once("inc/common.php"); 13f3f0262cSandi require_once("inc/io.php"); 14132bdbfeSandi require_once("inc/blowfish.php"); 1515fae107Sandi // load the the auth functions 16f3f0262cSandi require_once('inc/auth_'.$conf['authtype'].'.php'); 17f3f0262cSandi 1815fae107Sandi // some ACL level defines 19f3f0262cSandi define('AUTH_NONE',0); 20f3f0262cSandi define('AUTH_READ',1); 21f3f0262cSandi define('AUTH_EDIT',2); 22f3f0262cSandi define('AUTH_CREATE',4); 23f3f0262cSandi define('AUTH_UPLOAD',8); 24f3f0262cSandi define('AUTH_GRANT',255); 25f3f0262cSandi 26f3f0262cSandi if($conf['useacl']){ 27132bdbfeSandi auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 2815fae107Sandi // load ACL into a global array 29f3f0262cSandi $AUTH_ACL = file('conf/acl.auth'); 30f3f0262cSandi } 31f3f0262cSandi 32f3f0262cSandi/** 33f3f0262cSandi * This tries to login the user based on the sent auth credentials 34f3f0262cSandi * 35f3f0262cSandi * The authentication works like this: if a username was given 3615fae107Sandi * a new login is assumed and user/password are checked. If they 3715fae107Sandi * are correct the password is encrypted with blowfish and stored 3815fae107Sandi * together with the username in a cookie - the same info is stored 3915fae107Sandi * in the session, too. Additonally a browserID is stored in the 4015fae107Sandi * session. 4115fae107Sandi * 4215fae107Sandi * If no username was given the cookie is checked: if the username, 4315fae107Sandi * crypted password and browserID match between session and cookie 4415fae107Sandi * no further testing is done and the user is accepted 4515fae107Sandi * 4615fae107Sandi * If a cookie was found but no session info was availabe the 4715fae107Sandi * blowish encrypted password from the cookie is decrypted and 4815fae107Sandi * together with username rechecked by calling this function again. 49f3f0262cSandi * 50f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 51f3f0262cSandi * are set. 5215fae107Sandi * 5315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 5415fae107Sandi * 5515fae107Sandi * @param string $user Username 5615fae107Sandi * @param string $pass Cleartext Password 5715fae107Sandi * @param bool $sticky Cookie should not expire 5815fae107Sandi * @return bool true on successful auth 59f3f0262cSandi*/ 60132bdbfeSandifunction auth_login($user,$pass,$sticky=false){ 61f3f0262cSandi global $USERINFO; 62f3f0262cSandi global $conf; 63f3f0262cSandi global $lang; 64132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 65f3f0262cSandi 66f3f0262cSandi if(isset($user)){ 67132bdbfeSandi //usual login 68f3f0262cSandi if (auth_checkPass($user,$pass)){ 69132bdbfeSandi // make logininfo globally available 70f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 71132bdbfeSandi $USERINFO = auth_getUserData($user); //FIXME move all references to session 72132bdbfeSandi 73132bdbfeSandi // set cookie 74132bdbfeSandi $pass = PMA_blowfish_encrypt($pass,auth_cookiesalt()); 75132bdbfeSandi $cookie = base64_encode("$user|$sticky|$pass"); 76132bdbfeSandi if($sticky) $time = time()+60*60*24*365; //one year 77132bdbfeSandi setcookie('DokuWikiAUTH',$cookie,$time); 78132bdbfeSandi 79132bdbfeSandi // set session 80132bdbfeSandi $_SESSION[$conf['title']]['auth']['user'] = $user; 81132bdbfeSandi $_SESSION[$conf['title']]['auth']['pass'] = $pass; 82132bdbfeSandi $_SESSION[$conf['title']]['auth']['buid'] = auth_browseruid(); 83132bdbfeSandi $_SESSION[$conf['title']]['auth']['info'] = $USERINFO; 84132bdbfeSandi return true; 85f3f0262cSandi }else{ 86f3f0262cSandi //invalid credentials - log off 87f3f0262cSandi msg($lang['badlogin'],-1); 88f3f0262cSandi auth_logoff(); 89132bdbfeSandi return false; 90f3f0262cSandi } 91f3f0262cSandi }else{ 92132bdbfeSandi // read cookie information 93132bdbfeSandi $cookie = base64_decode($_COOKIE['DokuWikiAUTH']); 94132bdbfeSandi list($user,$sticky,$pass) = split('\|',$cookie,3); 95132bdbfeSandi // get session info 96132bdbfeSandi $session = $_SESSION[$conf['title']]['auth']; 97132bdbfeSandi 98132bdbfeSandi if($user && $pass){ 99132bdbfeSandi // we got a cookie - see if we can trust it 100132bdbfeSandi if(isset($session) && 101132bdbfeSandi ($session['user'] == $user) && 102132bdbfeSandi ($session['pass'] == $pass) && //still crypted 103132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 104132bdbfeSandi // he has session, cookie and browser right - let him in 105132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 106132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 107132bdbfeSandi return true; 108132bdbfeSandi } 109132bdbfeSandi // no we don't trust it yet - recheck pass 110132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 111132bdbfeSandi return auth_login($user,$pass,$sticky); 112132bdbfeSandi } 113132bdbfeSandi } 114f3f0262cSandi //just to be sure 115f3f0262cSandi auth_logoff(); 116132bdbfeSandi return false; 117f3f0262cSandi} 118132bdbfeSandi 119132bdbfeSandi/** 120132bdbfeSandi * Builds a pseudo UID from browserdata 121132bdbfeSandi * 122132bdbfeSandi * This is neither unique nor unfakable - still it adds some 123132bdbfeSandi * security 12415fae107Sandi * 12515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 12615fae107Sandi * 12715fae107Sandi * @return string a MD5 sum of various browser headers 128132bdbfeSandi */ 129132bdbfeSandifunction auth_browseruid(){ 130132bdbfeSandi $uid = ''; 131132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 132132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 133132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 134132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 135132bdbfeSandi return md5($uid); 136132bdbfeSandi} 137132bdbfeSandi 138132bdbfeSandi/** 139132bdbfeSandi * Creates a random key to encrypt the password in cookies 14015fae107Sandi * 14115fae107Sandi * This function tries to read the password for encrypting 14215fae107Sandi * cookies from $conf['datadir'].'/.cache/cookiesalt' 14315fae107Sandi * if no such file is found a random key is created and 14415fae107Sandi * and stored in this file. 14515fae107Sandi * 14615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 14715fae107Sandi * 14815fae107Sandi * @return string 149132bdbfeSandi */ 150132bdbfeSandifunction auth_cookiesalt(){ 151132bdbfeSandi global $conf; 152132bdbfeSandi $file = $conf['datadir'].'/.cache/cookiesalt'; 153132bdbfeSandi $salt = io_readFile($file); 154132bdbfeSandi if(empty($salt)){ 155132bdbfeSandi $salt = uniqid(rand(),true); 156132bdbfeSandi io_saveFile($file,$salt); 157132bdbfeSandi } 158132bdbfeSandi return $salt; 159f3f0262cSandi} 160f3f0262cSandi 161f3f0262cSandi/** 162f3f0262cSandi * This clears all authenticationdata and thus log the user 163f3f0262cSandi * off 16415fae107Sandi * 16515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 166f3f0262cSandi */ 167f3f0262cSandifunction auth_logoff(){ 168f3f0262cSandi global $conf; 169f3f0262cSandi global $USERINFO; 170132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['user']); 171132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['pass']); 172132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['info']); 173f3f0262cSandi unset($_SERVER['REMOTE_USER']); 174132bdbfeSandi $USERINFO=null; //FIXME 175132bdbfeSandi setcookie('DokuWikiAUTH','',time()-3600); 176f3f0262cSandi} 177f3f0262cSandi 178f3f0262cSandi/** 17915fae107Sandi * Convinience function for auth_aclcheck() 18015fae107Sandi * 18115fae107Sandi * This checks the permissions for the current user 18215fae107Sandi * 18315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 18415fae107Sandi * 18515fae107Sandi * @param string $id page ID 18615fae107Sandi * @return int permission level 187f3f0262cSandi */ 188f3f0262cSandifunction auth_quickaclcheck($id){ 189f3f0262cSandi global $conf; 190f3f0262cSandi global $USERINFO; 191f3f0262cSandi # if no ACL is used always return upload rights 192f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 193f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 194f3f0262cSandi} 195f3f0262cSandi 196f3f0262cSandi/** 197f3f0262cSandi * Returns the maximum rights a user has for 198f3f0262cSandi * the given ID or its namespace 19915fae107Sandi * 20015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20115fae107Sandi * 20215fae107Sandi * @param string $id page ID 20315fae107Sandi * @param string $user Username 20415fae107Sandi * @param array $groups Array of groups the user is in 20515fae107Sandi * @return int permission level 206f3f0262cSandi */ 207f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 208f3f0262cSandi global $conf; 209f3f0262cSandi global $AUTH_ACL; 210f3f0262cSandi 211f3f0262cSandi # if no ACL is used always return upload rights 212f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 213f3f0262cSandi 214f3f0262cSandi $ns = getNS($id); 215f3f0262cSandi $perm = -1; 216f3f0262cSandi 217f3f0262cSandi if($user){ 218f3f0262cSandi //prepend groups with @ 219f3f0262cSandi for($i=0; $i<count($groups); $i++){ 220f3f0262cSandi $groups[$i] = '@'.$groups[$i]; 221f3f0262cSandi } 222f3f0262cSandi //add ALL group 223f3f0262cSandi $groups[] = '@ALL'; 224f3f0262cSandi //add User 225f3f0262cSandi $groups[] = $user; 226f3f0262cSandi //build regexp 227f3f0262cSandi $regexp = join('|',$groups); 228f3f0262cSandi }else{ 229f3f0262cSandi $regexp = '@ALL'; 230f3f0262cSandi } 231f3f0262cSandi 232f3f0262cSandi //check exact match first 233f3f0262cSandi $matches = preg_grep('/^'.$id.'\s+('.$regexp.')\s+/',$AUTH_ACL); 234f3f0262cSandi if(count($matches)){ 235f3f0262cSandi foreach($matches as $match){ 236f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 237f3f0262cSandi $acl = preg_split('/\s+/',$match); 238f3f0262cSandi if($acl[2] > $perm){ 239f3f0262cSandi $perm = $acl[2]; 240f3f0262cSandi } 241f3f0262cSandi } 242f3f0262cSandi if($perm > -1){ 243f3f0262cSandi //we had a match - return it 244f3f0262cSandi return $perm; 245f3f0262cSandi } 246f3f0262cSandi } 247f3f0262cSandi 248f3f0262cSandi //still here? do the namespace checks 249f3f0262cSandi if($ns){ 250f3f0262cSandi $path = $ns.':\*'; 251f3f0262cSandi }else{ 252f3f0262cSandi $path = '\*'; //root document 253f3f0262cSandi } 254f3f0262cSandi 255f3f0262cSandi do{ 256f3f0262cSandi $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL); 257f3f0262cSandi if(count($matches)){ 258f3f0262cSandi foreach($matches as $match){ 259f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 260f3f0262cSandi $acl = preg_split('/\s+/',$match); 261f3f0262cSandi if($acl[2] > $perm){ 262f3f0262cSandi $perm = $acl[2]; 263f3f0262cSandi } 264f3f0262cSandi } 265f3f0262cSandi //we had a match - return it 266f3f0262cSandi return $perm; 267f3f0262cSandi } 268f3f0262cSandi 269f3f0262cSandi //get next higher namespace 270f3f0262cSandi $ns = getNS($ns); 271f3f0262cSandi 272f3f0262cSandi if($path != '\*'){ 273f3f0262cSandi $path = $ns.':\*'; 274f3f0262cSandi if($path == ':\*') $path = '\*'; 275f3f0262cSandi }else{ 276f3f0262cSandi //we did this already 277f3f0262cSandi //looks like there is something wrong with the ACL 278f3f0262cSandi //break here 279f3f0262cSandi return $perm; 280f3f0262cSandi } 281f3f0262cSandi }while(1); //this should never loop endless 282f3f0262cSandi} 283f3f0262cSandi 284f3f0262cSandi/** 285f3f0262cSandi * Create a pronouncable password 286f3f0262cSandi * 28715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 28815fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 28915fae107Sandi * 29015fae107Sandi * @return string pronouncable password 291f3f0262cSandi */ 292f3f0262cSandifunction auth_pwgen(){ 293f3f0262cSandi $pw = ''; 294f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 295f3f0262cSandi $v = 'aeiou'; //vowels 296f3f0262cSandi $a = $c.$v; //both 297f3f0262cSandi 298f3f0262cSandi //use two syllables... 299f3f0262cSandi for($i=0;$i < 2; $i++){ 300f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 301f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 302f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 303f3f0262cSandi } 304f3f0262cSandi //... and add a nice number 305f3f0262cSandi $pw .= rand(10,99); 306f3f0262cSandi 307f3f0262cSandi return $pw; 308f3f0262cSandi} 309f3f0262cSandi 310f3f0262cSandi/** 311f3f0262cSandi * Sends a password to the given user 312f3f0262cSandi * 31315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 31415fae107Sandi * 31515fae107Sandi * @return bool true on success 316f3f0262cSandi */ 317f3f0262cSandifunction auth_sendPassword($user,$password){ 318f3f0262cSandi global $conf; 319f3f0262cSandi global $lang; 320f3f0262cSandi $hdrs = ''; 321*87ddda95Sandi $userinfo = auth_getUserData($user); 322f3f0262cSandi 323*87ddda95Sandi if(!$userinfo['mail']) return false; 324f3f0262cSandi 325f3f0262cSandi $text = rawLocale('password'); 326f3f0262cSandi $text = str_replace('@DOKUWIKIURL@',getBaseURL(true),$text); 327*87ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 328f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 329f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 330f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 331f3f0262cSandi 332f3f0262cSandi if (!empty($conf['mailfrom'])) { 333f3f0262cSandi $hdrs = 'From: '.$conf['mailfrom']."\n"; 334f3f0262cSandi } 335*87ddda95Sandi return @mail($userinfo['mail'],$lang['regpwmail'],$text,$hdrs); 336f3f0262cSandi} 337f3f0262cSandi 338f3f0262cSandi/** 33915fae107Sandi * Register a new user 340f3f0262cSandi * 34115fae107Sandi * This registers a new user - Data is read directly from $_POST 34215fae107Sandi * 34315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 34415fae107Sandi * 34515fae107Sandi * @return bool true on success, false on any error 346f3f0262cSandi */ 347f3f0262cSandifunction register(){ 348f3f0262cSandi global $lang; 349f3f0262cSandi global $conf; 350f3f0262cSandi 351f3f0262cSandi if(!$_POST['save']) return false; 352f3f0262cSandi if(!$conf['openregister']) return false; 353f3f0262cSandi 354f3f0262cSandi //clean username 355f3f0262cSandi $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 356f3f0262cSandi $_POST['login'] = cleanID($_POST['login']); 357f3f0262cSandi //clean fullname and email 358f3f0262cSandi $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname'])); 359f3f0262cSandi $_POST['email'] = trim(str_replace(':','',$_POST['email'])); 360f3f0262cSandi 361f3f0262cSandi if( empty($_POST['login']) || 362f3f0262cSandi empty($_POST['fullname']) || 363f3f0262cSandi empty($_POST['email']) ){ 364f3f0262cSandi msg($lang['regmissing'],-1); 365f3f0262cSandi return false; 366f3f0262cSandi } 367f3f0262cSandi 368f3f0262cSandi //check mail 369f3f0262cSandi if(!isvalidemail($_POST['email'])){ 370f3f0262cSandi msg($lang['regbadmail'],-1); 371f3f0262cSandi return false; 372f3f0262cSandi } 373f3f0262cSandi 374f3f0262cSandi //okay try to create the user 375f3f0262cSandi $pass = auth_createUser($_POST['login'],$_POST['fullname'],$_POST['email']); 376f3f0262cSandi if(empty($pass)){ 377f3f0262cSandi msg($lang['reguexists'],-1); 378f3f0262cSandi return false; 379f3f0262cSandi } 380f3f0262cSandi 381f3f0262cSandi //send him the password 382f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 383f3f0262cSandi msg($lang['regsuccess'],1); 384f3f0262cSandi return true; 385f3f0262cSandi }else{ 386f3f0262cSandi msg($lang['regmailfail'],-1); 387f3f0262cSandi return false; 388f3f0262cSandi } 389f3f0262cSandi} 390f3f0262cSandi 391f3f0262cSandi/** 392f3f0262cSandi * Uses a regular expresion to check if a given mail address is valid 393f3f0262cSandi * 394f3f0262cSandi * May not be completly RFC conform! 39515fae107Sandi * 39615fae107Sandi * @link http://www.webmasterworld.com/forum88/135.htm 39715fae107Sandi * 39815fae107Sandi * @param string $email the address to check 39915fae107Sandi * @return bool true if address is valid 400f3f0262cSandi */ 401f3f0262cSandifunction isvalidemail($email){ 402f3f0262cSandi return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 403f3f0262cSandi} 404f3f0262cSandi 405f3f0262cSandi?> 406