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 12ed7b5f09Sandi if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 13ed7b5f09Sandi require_once(DOKU_INC.'inc/common.php'); 14ed7b5f09Sandi require_once(DOKU_INC.'inc/io.php'); 151c73890cSAndreas Gohr 16ebf97c8fSAndreas Gohr // some ACL level defines 17ebf97c8fSAndreas Gohr define('AUTH_NONE',0); 18ebf97c8fSAndreas Gohr define('AUTH_READ',1); 19ebf97c8fSAndreas Gohr define('AUTH_EDIT',2); 20ebf97c8fSAndreas Gohr define('AUTH_CREATE',4); 21ebf97c8fSAndreas Gohr define('AUTH_UPLOAD',8); 22ebf97c8fSAndreas Gohr define('AUTH_DELETE',16); 23ebf97c8fSAndreas Gohr define('AUTH_ADMIN',255); 24ebf97c8fSAndreas Gohr 25742c66f8Schris global $conf; 26742c66f8Schris 271c73890cSAndreas Gohr if($conf['useacl']){ 28ed7b5f09Sandi require_once(DOKU_INC.'inc/blowfish.php'); 29ed7b5f09Sandi require_once(DOKU_INC.'inc/mail.php'); 308b06d178Schris 3103c4aec3Schris global $auth; 3203c4aec3Schris 338b06d178Schris // load the the backend auth functions and instantiate the auth object 348b06d178Schris if (@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) { 358b06d178Schris require_once(DOKU_INC.'inc/auth/basic.class.php'); 368b06d178Schris require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php'); 378b06d178Schris 388b06d178Schris $auth_class = "auth_".$conf['authtype']; 39cd52f92dSchris if (class_exists($auth_class)) { 408b06d178Schris $auth = new $auth_class(); 41d2dde4ebSMatthias Grimm if ($auth->success == false) { 42d2dde4ebSMatthias Grimm unset($auth); 43cd52f92dSchris msg($lang['authtempfail'], -1); 44cd52f92dSchris 45cd52f92dSchris // turn acl config setting off for the rest of this page 46cd52f92dSchris $conf['useacl'] = 0; 47d2dde4ebSMatthias Grimm } 488b06d178Schris } else { 493816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 50cd52f92dSchris } 51cd52f92dSchris } else { 523816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 538b06d178Schris } 541c73890cSAndreas Gohr } 55f3f0262cSandi 56f5cb575dSAndreas Gohr // do the login either by cookie or provided credentials 57f3f0262cSandi if($conf['useacl']){ 58bbbd6568SAndreas Gohr if (!isset($_REQUEST['u'])) $_REQUEST['u'] = ''; 59bbbd6568SAndreas Gohr if (!isset($_REQUEST['p'])) $_REQUEST['p'] = ''; 60bbbd6568SAndreas Gohr if (!isset($_REQUEST['r'])) $_REQUEST['r'] = ''; 61bbbd6568SAndreas Gohr 621e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 634a26ad85Schris if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])){ 641e8c9c90SAndreas Gohr $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER']; 651e8c9c90SAndreas Gohr $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW']; 661e8c9c90SAndreas Gohr } 671e8c9c90SAndreas Gohr 68f5cb575dSAndreas Gohr // external trust mechanism in place? 6982fd59b6SAndreas Gohr if(!is_null($auth) && $auth->canDo('external')){ 70f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 71f5cb575dSAndreas Gohr }else{ 72132bdbfeSandi auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 73f5cb575dSAndreas Gohr } 74f5cb575dSAndreas Gohr 7515fae107Sandi //load ACL into a global array 76e7cb32dcSAndreas Gohr if(is_readable(DOKU_CONF.'acl.auth.php')){ 77e7cb32dcSAndreas Gohr $AUTH_ACL = file(DOKU_CONF.'acl.auth.php'); 7811799630Sandi }else{ 7911799630Sandi $AUTH_ACL = array(); 8011799630Sandi } 81f3f0262cSandi } 82f3f0262cSandi 83f3f0262cSandi/** 84f3f0262cSandi * This tries to login the user based on the sent auth credentials 85f3f0262cSandi * 86f3f0262cSandi * The authentication works like this: if a username was given 8715fae107Sandi * a new login is assumed and user/password are checked. If they 8815fae107Sandi * are correct the password is encrypted with blowfish and stored 8915fae107Sandi * together with the username in a cookie - the same info is stored 9015fae107Sandi * in the session, too. Additonally a browserID is stored in the 9115fae107Sandi * session. 9215fae107Sandi * 9315fae107Sandi * If no username was given the cookie is checked: if the username, 9415fae107Sandi * crypted password and browserID match between session and cookie 9515fae107Sandi * no further testing is done and the user is accepted 9615fae107Sandi * 9715fae107Sandi * If a cookie was found but no session info was availabe the 98136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 9915fae107Sandi * together with username rechecked by calling this function again. 100f3f0262cSandi * 101f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 102f3f0262cSandi * are set. 10315fae107Sandi * 10415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 10515fae107Sandi * 10615fae107Sandi * @param string $user Username 10715fae107Sandi * @param string $pass Cleartext Password 10815fae107Sandi * @param bool $sticky Cookie should not expire 10915fae107Sandi * @return bool true on successful auth 110f3f0262cSandi*/ 111132bdbfeSandifunction auth_login($user,$pass,$sticky=false){ 112f3f0262cSandi global $USERINFO; 113f3f0262cSandi global $conf; 114f3f0262cSandi global $lang; 115cd52f92dSchris global $auth; 116132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 117f3f0262cSandi 118bbbd6568SAndreas Gohr if(!empty($user)){ 119132bdbfeSandi //usual login 120cd52f92dSchris if ($auth->checkPass($user,$pass)){ 121132bdbfeSandi // make logininfo globally available 122f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 123cd52f92dSchris $USERINFO = $auth->getUserData($user); //FIXME move all references to session 124132bdbfeSandi 125132bdbfeSandi // set cookie 126132bdbfeSandi $pass = PMA_blowfish_encrypt($pass,auth_cookiesalt()); 127132bdbfeSandi $cookie = base64_encode("$user|$sticky|$pass"); 128132bdbfeSandi if($sticky) $time = time()+60*60*24*365; //one year 129e65afed4SSameer D. Sahasrabuddhe setcookie(DOKU_COOKIE,$cookie,$time,'/'); 130132bdbfeSandi 131132bdbfeSandi // set session 132e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 133e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass; 134e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 135e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 136132bdbfeSandi return true; 137f3f0262cSandi }else{ 138f3f0262cSandi //invalid credentials - log off 139f3f0262cSandi msg($lang['badlogin'],-1); 140f3f0262cSandi auth_logoff(); 141132bdbfeSandi return false; 142f3f0262cSandi } 143f3f0262cSandi }else{ 144132bdbfeSandi // read cookie information 145e65afed4SSameer D. Sahasrabuddhe $cookie = base64_decode($_COOKIE[DOKU_COOKIE]); 146132bdbfeSandi list($user,$sticky,$pass) = split('\|',$cookie,3); 147132bdbfeSandi // get session info 148e71ce681SAndreas Gohr $session = $_SESSION[DOKU_COOKIE]['auth']; 149132bdbfeSandi 150132bdbfeSandi if($user && $pass){ 151132bdbfeSandi // we got a cookie - see if we can trust it 152132bdbfeSandi if(isset($session) && 153132bdbfeSandi ($session['user'] == $user) && 154132bdbfeSandi ($session['pass'] == $pass) && //still crypted 155132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 156132bdbfeSandi // he has session, cookie and browser right - let him in 157132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 158132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 159132bdbfeSandi return true; 160132bdbfeSandi } 161132bdbfeSandi // no we don't trust it yet - recheck pass 162132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 163132bdbfeSandi return auth_login($user,$pass,$sticky); 164132bdbfeSandi } 165132bdbfeSandi } 166f3f0262cSandi //just to be sure 167f3f0262cSandi auth_logoff(); 168132bdbfeSandi return false; 169f3f0262cSandi} 170132bdbfeSandi 171132bdbfeSandi/** 172136ce040Sandi * Builds a pseudo UID from browser and IP data 173132bdbfeSandi * 174132bdbfeSandi * This is neither unique nor unfakable - still it adds some 175136ce040Sandi * security. Using the first part of the IP makes sure 176136ce040Sandi * proxy farms like AOLs are stil okay. 17715fae107Sandi * 17815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 17915fae107Sandi * 18015fae107Sandi * @return string a MD5 sum of various browser headers 181132bdbfeSandi */ 182132bdbfeSandifunction auth_browseruid(){ 183132bdbfeSandi $uid = ''; 184132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 185132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 186132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 187132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 188136ce040Sandi $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.')); 189132bdbfeSandi return md5($uid); 190132bdbfeSandi} 191132bdbfeSandi 192132bdbfeSandi/** 193132bdbfeSandi * Creates a random key to encrypt the password in cookies 19415fae107Sandi * 19515fae107Sandi * This function tries to read the password for encrypting 19698407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 19715fae107Sandi * if no such file is found a random key is created and 19815fae107Sandi * and stored in this file. 19915fae107Sandi * 20015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20115fae107Sandi * 20215fae107Sandi * @return string 203132bdbfeSandi */ 204132bdbfeSandifunction auth_cookiesalt(){ 205132bdbfeSandi global $conf; 20698407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 207132bdbfeSandi $salt = io_readFile($file); 208132bdbfeSandi if(empty($salt)){ 209132bdbfeSandi $salt = uniqid(rand(),true); 210132bdbfeSandi io_saveFile($file,$salt); 211132bdbfeSandi } 212132bdbfeSandi return $salt; 213f3f0262cSandi} 214f3f0262cSandi 215f3f0262cSandi/** 216f3f0262cSandi * This clears all authenticationdata and thus log the user 217f3f0262cSandi * off 21815fae107Sandi * 21915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 220f3f0262cSandi */ 221f3f0262cSandifunction auth_logoff(){ 222f3f0262cSandi global $conf; 223f3f0262cSandi global $USERINFO; 2248b06d178Schris global $INFO, $ID; 2255298a619SAndreas Gohr global $auth; 22637065e65Sandi 227e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 228e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 229e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 230e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 231e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 232e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 23337065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 234f3f0262cSandi unset($_SERVER['REMOTE_USER']); 235132bdbfeSandi $USERINFO=null; //FIXME 2361e866646Sandi setcookie(DOKU_COOKIE,'',time()-600000,'/'); 2375298a619SAndreas Gohr 2385298a619SAndreas Gohr if($auth && $auth->canDo('logoff')){ 2395298a619SAndreas Gohr $auth->logOff(); 2405298a619SAndreas Gohr } 241f3f0262cSandi} 242f3f0262cSandi 243f3f0262cSandi/** 24415fae107Sandi * Convinience function for auth_aclcheck() 24515fae107Sandi * 24615fae107Sandi * This checks the permissions for the current user 24715fae107Sandi * 24815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 24915fae107Sandi * 25015fae107Sandi * @param string $id page ID 25115fae107Sandi * @return int permission level 252f3f0262cSandi */ 253f3f0262cSandifunction auth_quickaclcheck($id){ 254f3f0262cSandi global $conf; 255f3f0262cSandi global $USERINFO; 256f3f0262cSandi # if no ACL is used always return upload rights 257f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 258f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 259f3f0262cSandi} 260f3f0262cSandi 261f3f0262cSandi/** 262f3f0262cSandi * Returns the maximum rights a user has for 263f3f0262cSandi * the given ID or its namespace 26415fae107Sandi * 26515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 26615fae107Sandi * 26715fae107Sandi * @param string $id page ID 26815fae107Sandi * @param string $user Username 26915fae107Sandi * @param array $groups Array of groups the user is in 27015fae107Sandi * @return int permission level 271f3f0262cSandi */ 272f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 273f3f0262cSandi global $conf; 274f3f0262cSandi global $AUTH_ACL; 275f3f0262cSandi 276f3f0262cSandi # if no ACL is used always return upload rights 277f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 278f3f0262cSandi 2796c2bb100SAndreas Gohr $user = auth_nameencode($user); 2806c2bb100SAndreas Gohr 28110a76f6fSfrank //if user is superuser return 255 (acl_admin) 282e838fc2eSAndreas Gohr if(auth_nameencode($conf['superuser']) == $user) { return AUTH_ADMIN; } 28310a76f6fSfrank 284074cf26bSandi //make sure groups is an array 285074cf26bSandi if(!is_array($groups)) $groups = array(); 286074cf26bSandi 2876c2bb100SAndreas Gohr //prepend groups with @ and nameencode 2882cd2db38Sandi $cnt = count($groups); 2892cd2db38Sandi for($i=0; $i<$cnt; $i++){ 2906c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 29110a76f6fSfrank } 29210a76f6fSfrank //if user is in superuser group return 255 (acl_admin) 293e838fc2eSAndreas Gohr if(in_array(auth_nameencode($conf['superuser'],true), $groups)) { return AUTH_ADMIN; } 29410a76f6fSfrank 295f3f0262cSandi $ns = getNS($id); 296f3f0262cSandi $perm = -1; 297f3f0262cSandi 298f3f0262cSandi if($user){ 299f3f0262cSandi //add ALL group 300f3f0262cSandi $groups[] = '@ALL'; 301f3f0262cSandi //add User 302f3f0262cSandi $groups[] = $user; 303f3f0262cSandi //build regexp 304f3f0262cSandi $regexp = join('|',$groups); 305f3f0262cSandi }else{ 306f3f0262cSandi $regexp = '@ALL'; 307f3f0262cSandi } 308f3f0262cSandi 309f3f0262cSandi //check exact match first 31042905504SAndreas Gohr $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL); 311f3f0262cSandi if(count($matches)){ 312f3f0262cSandi foreach($matches as $match){ 313f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 314f3f0262cSandi $acl = preg_split('/\s+/',$match); 3158ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 316f3f0262cSandi if($acl[2] > $perm){ 317f3f0262cSandi $perm = $acl[2]; 318f3f0262cSandi } 319f3f0262cSandi } 320f3f0262cSandi if($perm > -1){ 321f3f0262cSandi //we had a match - return it 322f3f0262cSandi return $perm; 323f3f0262cSandi } 324f3f0262cSandi } 325f3f0262cSandi 326f3f0262cSandi //still here? do the namespace checks 327f3f0262cSandi if($ns){ 328f3f0262cSandi $path = $ns.':\*'; 329f3f0262cSandi }else{ 330f3f0262cSandi $path = '\*'; //root document 331f3f0262cSandi } 332f3f0262cSandi 333f3f0262cSandi do{ 334f3f0262cSandi $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL); 335f3f0262cSandi if(count($matches)){ 336f3f0262cSandi foreach($matches as $match){ 337f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 338f3f0262cSandi $acl = preg_split('/\s+/',$match); 3398ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 340f3f0262cSandi if($acl[2] > $perm){ 341f3f0262cSandi $perm = $acl[2]; 342f3f0262cSandi } 343f3f0262cSandi } 344f3f0262cSandi //we had a match - return it 345f3f0262cSandi return $perm; 346f3f0262cSandi } 347f3f0262cSandi 348f3f0262cSandi //get next higher namespace 349f3f0262cSandi $ns = getNS($ns); 350f3f0262cSandi 351f3f0262cSandi if($path != '\*'){ 352f3f0262cSandi $path = $ns.':\*'; 353f3f0262cSandi if($path == ':\*') $path = '\*'; 354f3f0262cSandi }else{ 355f3f0262cSandi //we did this already 356f3f0262cSandi //looks like there is something wrong with the ACL 357f3f0262cSandi //break here 358d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 359d5ce66f6SAndreas Gohr return AUTH_NONE; 360f3f0262cSandi } 361f3f0262cSandi }while(1); //this should never loop endless 36252a5af8dSandi 36352a5af8dSandi //still here? return no permissions 36452a5af8dSandi return AUTH_NONE; 365f3f0262cSandi} 366f3f0262cSandi 367f3f0262cSandi/** 3686c2bb100SAndreas Gohr * Encode ASCII special chars 3696c2bb100SAndreas Gohr * 3706c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 3716c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 3726c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 3736c2bb100SAndreas Gohr * urlencoding!). 3746c2bb100SAndreas Gohr * 3756c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 3766c2bb100SAndreas Gohr * 3776c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 3786c2bb100SAndreas Gohr * @see rawurldecode() 3796c2bb100SAndreas Gohr */ 380e838fc2eSAndreas Gohrfunction auth_nameencode($name,$skip_group=false){ 381a424cd8eSchris global $cache_authname; 382a424cd8eSchris $cache =& $cache_authname; 383a424cd8eSchris 384a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 385e838fc2eSAndreas Gohr if($skip_group && $name{0} =='@'){ 386a424cd8eSchris $cache[$name][$skip_group] = '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 387e838fc2eSAndreas Gohr "'%'.dechex(ord('\\1'))",substr($name,1)); 388e838fc2eSAndreas Gohr }else{ 389a424cd8eSchris $cache[$name][$skip_group] = preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 390e838fc2eSAndreas Gohr "'%'.dechex(ord('\\1'))",$name); 391e838fc2eSAndreas Gohr } 3926c2bb100SAndreas Gohr } 3936c2bb100SAndreas Gohr 394a424cd8eSchris return $cache[$name][$skip_group]; 395a424cd8eSchris} 396a424cd8eSchris 3976c2bb100SAndreas Gohr/** 398f3f0262cSandi * Create a pronouncable password 399f3f0262cSandi * 40015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 40115fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 40215fae107Sandi * 40315fae107Sandi * @return string pronouncable password 404f3f0262cSandi */ 405f3f0262cSandifunction auth_pwgen(){ 406f3f0262cSandi $pw = ''; 407f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 408f3f0262cSandi $v = 'aeiou'; //vowels 409f3f0262cSandi $a = $c.$v; //both 410f3f0262cSandi 411f3f0262cSandi //use two syllables... 412f3f0262cSandi for($i=0;$i < 2; $i++){ 413f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 414f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 415f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 416f3f0262cSandi } 417f3f0262cSandi //... and add a nice number 418f3f0262cSandi $pw .= rand(10,99); 419f3f0262cSandi 420f3f0262cSandi return $pw; 421f3f0262cSandi} 422f3f0262cSandi 423f3f0262cSandi/** 424f3f0262cSandi * Sends a password to the given user 425f3f0262cSandi * 42615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 42715fae107Sandi * 42815fae107Sandi * @return bool true on success 429f3f0262cSandi */ 430f3f0262cSandifunction auth_sendPassword($user,$password){ 431f3f0262cSandi global $conf; 432f3f0262cSandi global $lang; 433cd52f92dSchris global $auth; 434cd52f92dSchris 435f3f0262cSandi $hdrs = ''; 436cd52f92dSchris $userinfo = $auth->getUserData($user); 437f3f0262cSandi 43887ddda95Sandi if(!$userinfo['mail']) return false; 439f3f0262cSandi 440f3f0262cSandi $text = rawLocale('password'); 441ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 44287ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 443f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 444f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 445f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 446f3f0262cSandi 44744f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 44844f669e9Sandi $lang['regpwmail'], 44944f669e9Sandi $text, 45044f669e9Sandi $conf['mailfrom']); 451f3f0262cSandi} 452f3f0262cSandi 453f3f0262cSandi/** 45415fae107Sandi * Register a new user 455f3f0262cSandi * 45615fae107Sandi * This registers a new user - Data is read directly from $_POST 45715fae107Sandi * 45815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 45915fae107Sandi * 46015fae107Sandi * @return bool true on success, false on any error 461f3f0262cSandi */ 462f3f0262cSandifunction register(){ 463f3f0262cSandi global $lang; 464eb5d07e4Sjan global $conf; 465cd52f92dSchris global $auth; 466f3f0262cSandi 467f3f0262cSandi if(!$_POST['save']) return false; 46882fd59b6SAndreas Gohr if(!$auth->canDo('addUser')) return false; 469640145a5Sandi 470f3f0262cSandi //clean username 471f3f0262cSandi $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 472f3f0262cSandi $_POST['login'] = cleanID($_POST['login']); 473f3f0262cSandi //clean fullname and email 47454f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 47554f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 476f3f0262cSandi 477f3f0262cSandi if( empty($_POST['login']) || 478f3f0262cSandi empty($_POST['fullname']) || 479f3f0262cSandi empty($_POST['email']) ){ 480f3f0262cSandi msg($lang['regmissing'],-1); 481f3f0262cSandi return false; 482f3f0262cSandi } 483f3f0262cSandi 484cab2716aSmatthias.grimm if ($conf['autopasswd']) { 485cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 486cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 487cab2716aSmatthias.grimm empty($_POST['passchk'])) { 488bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 489cab2716aSmatthias.grimm return false; 490cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 491bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 492cab2716aSmatthias.grimm return false; 493cab2716aSmatthias.grimm } else { 494cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 495cab2716aSmatthias.grimm } 496cab2716aSmatthias.grimm 497f3f0262cSandi //check mail 49844f669e9Sandi if(!mail_isvalid($_POST['email'])){ 499f3f0262cSandi msg($lang['regbadmail'],-1); 500f3f0262cSandi return false; 501f3f0262cSandi } 502f3f0262cSandi 503f3f0262cSandi //okay try to create the user 5041d096a10SAndreas Gohr if(!$auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email'])){ 505f3f0262cSandi msg($lang['reguexists'],-1); 506f3f0262cSandi return false; 507f3f0262cSandi } 508f3f0262cSandi 50902a498e7Schris // create substitutions for use in notification email 51002a498e7Schris $substitutions = array( 51102a498e7Schris 'NEWUSER' => $_POST['login'], 51202a498e7Schris 'NEWNAME' => $_POST['fullname'], 51302a498e7Schris 'NEWEMAIL' => $_POST['email'], 51402a498e7Schris ); 51502a498e7Schris 516cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 517cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 51802a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 519cab2716aSmatthias.grimm return true; 520cab2716aSmatthias.grimm } 521cab2716aSmatthias.grimm 522cab2716aSmatthias.grimm // autogenerated password? then send him the password 523f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 524f3f0262cSandi msg($lang['regsuccess'],1); 52502a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 526f3f0262cSandi return true; 527f3f0262cSandi }else{ 528f3f0262cSandi msg($lang['regmailfail'],-1); 529f3f0262cSandi return false; 530f3f0262cSandi } 531f3f0262cSandi} 532f3f0262cSandi 53310a76f6fSfrank/** 5348b06d178Schris * Update user profile 5358b06d178Schris * 5368b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 5378b06d178Schris */ 5388b06d178Schrisfunction updateprofile() { 5398b06d178Schris global $conf; 5408b06d178Schris global $INFO; 5418b06d178Schris global $lang; 542cd52f92dSchris global $auth; 5438b06d178Schris 544bb4866bdSchris if(empty($_POST['save'])) return false; 5458b06d178Schris 54682fd59b6SAndreas Gohr // should not be able to get here without Profile being possible... 54782fd59b6SAndreas Gohr if(!$auth->canDo('Profile')) { 5488b06d178Schris msg($lang['profna'],-1); 5498b06d178Schris return false; 5508b06d178Schris } 5518b06d178Schris 5528b06d178Schris if ($_POST['newpass'] != $_POST['passchk']) { 5538b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 5548b06d178Schris return false; 5558b06d178Schris } 5568b06d178Schris 5578b06d178Schris //clean fullname and email 55854f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 55954f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 5608b06d178Schris 5618b06d178Schris if (empty($_POST['fullname']) || empty($_POST['email'])) { 5628b06d178Schris msg($lang['profnoempty'],-1); 5638b06d178Schris return false; 5648b06d178Schris } 5658b06d178Schris 5668b06d178Schris if (!mail_isvalid($_POST['email'])){ 5678b06d178Schris msg($lang['regbadmail'],-1); 5688b06d178Schris return false; 5698b06d178Schris } 5708b06d178Schris 5718b06d178Schris if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname']; 5728b06d178Schris if ($_POST['email'] != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email']; 5738b06d178Schris if (!empty($_POST['newpass'])) $changes['pass'] = $_POST['newpass']; 5748b06d178Schris 5758b06d178Schris if (!count($changes)) { 5768b06d178Schris msg($lang['profnochange'], -1); 5778b06d178Schris return false; 5788b06d178Schris } 5798b06d178Schris 5808b06d178Schris if ($conf['profileconfirm']) { 5818b06d178Schris if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) { 5828b06d178Schris msg($lang['badlogin'],-1); 5838b06d178Schris return false; 5848b06d178Schris } 5858b06d178Schris } 5868b06d178Schris 587cd52f92dSchris return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes); 5888b06d178Schris} 5898b06d178Schris 5908b06d178Schris/** 5918b06d178Schris * Send a new password 5928b06d178Schris * 5931d5856cfSAndreas Gohr * This function handles both phases of the password reset: 5941d5856cfSAndreas Gohr * 5951d5856cfSAndreas Gohr * - handling the first request of password reset 5961d5856cfSAndreas Gohr * - validating the password reset auth token 5971d5856cfSAndreas Gohr * 5988b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 5998b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 6001d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6018b06d178Schris * 6028b06d178Schris * @return bool true on success, false on any error 6038b06d178Schris*/ 6048b06d178Schrisfunction act_resendpwd(){ 6058b06d178Schris global $lang; 6068b06d178Schris global $conf; 607cd52f92dSchris global $auth; 6088b06d178Schris 609409d7af7SAndreas Gohr if(!actionOK('resendpwd')) return false; 6108b06d178Schris 61182fd59b6SAndreas Gohr // should not be able to get here without modPass being possible... 61282fd59b6SAndreas Gohr if(!$auth->canDo('modPass')) { 6138b06d178Schris msg($lang['resendna'],-1); 6148b06d178Schris return false; 6158b06d178Schris } 6168b06d178Schris 6171d5856cfSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']); 6188b06d178Schris 6191d5856cfSAndreas Gohr if($token){ 6201d5856cfSAndreas Gohr // we're in token phase 6211d5856cfSAndreas Gohr 6221d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 6231d5856cfSAndreas Gohr if(!@file_exists($tfile)){ 6241d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'],-1); 6251d5856cfSAndreas Gohr return false; 6261d5856cfSAndreas Gohr } 6271d5856cfSAndreas Gohr $user = io_readfile($tfile); 6281d5856cfSAndreas Gohr @unlink($tfile); 629cd52f92dSchris $userinfo = $auth->getUserData($user); 6308b06d178Schris if(!$userinfo['mail']) { 6318b06d178Schris msg($lang['resendpwdnouser'], -1); 6328b06d178Schris return false; 6338b06d178Schris } 6348b06d178Schris 6358b06d178Schris $pass = auth_pwgen(); 636cd52f92dSchris if (!$auth->modifyUser($user,array('pass' => $pass))) { 6378b06d178Schris msg('error modifying user data',-1); 6388b06d178Schris return false; 6398b06d178Schris } 6408b06d178Schris 6418b06d178Schris if (auth_sendPassword($user,$pass)) { 6428b06d178Schris msg($lang['resendpwdsuccess'],1); 6438b06d178Schris } else { 6448b06d178Schris msg($lang['regmailfail'],-1); 6458b06d178Schris } 6468b06d178Schris return true; 6471d5856cfSAndreas Gohr 6481d5856cfSAndreas Gohr } else { 6491d5856cfSAndreas Gohr // we're in request phase 6501d5856cfSAndreas Gohr 6511d5856cfSAndreas Gohr if(!$_POST['save']) return false; 6521d5856cfSAndreas Gohr 6531d5856cfSAndreas Gohr if (empty($_POST['login'])) { 6541d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 6551d5856cfSAndreas Gohr return false; 6561d5856cfSAndreas Gohr } else { 657*16470b1dSchris $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 658*16470b1dSchris $user = cleanID($_POST['login']); 6591d5856cfSAndreas Gohr } 6601d5856cfSAndreas Gohr 6611d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 6621d5856cfSAndreas Gohr if(!$userinfo['mail']) { 6631d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 6641d5856cfSAndreas Gohr return false; 6651d5856cfSAndreas Gohr } 6661d5856cfSAndreas Gohr 6671d5856cfSAndreas Gohr // generate auth token 6681d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 6691d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 6701d5856cfSAndreas Gohr $url = wl('',array('do'=>'resendpwd','pwauth'=>$token),true,'&'); 6711d5856cfSAndreas Gohr 6721d5856cfSAndreas Gohr io_saveFile($tfile,$user); 6731d5856cfSAndreas Gohr 6741d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 6751d5856cfSAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 6761d5856cfSAndreas Gohr $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 6771d5856cfSAndreas Gohr $text = str_replace('@LOGIN@',$user,$text); 6781d5856cfSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 6791d5856cfSAndreas Gohr $text = str_replace('@CONFIRM@',$url,$text); 6801d5856cfSAndreas Gohr 6811d5856cfSAndreas Gohr if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 6821d5856cfSAndreas Gohr $lang['regpwmail'], 6831d5856cfSAndreas Gohr $text, 6841d5856cfSAndreas Gohr $conf['mailfrom'])){ 6851d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'],1); 6861d5856cfSAndreas Gohr }else{ 6871d5856cfSAndreas Gohr msg($lang['regmailfail'],-1); 6881d5856cfSAndreas Gohr } 6891d5856cfSAndreas Gohr return true; 6901d5856cfSAndreas Gohr } 6911d5856cfSAndreas Gohr 6921d5856cfSAndreas Gohr return false; // never reached 6938b06d178Schris} 6948b06d178Schris 6958b06d178Schris/** 69610a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid 69710a76f6fSfrank * 69810a76f6fSfrank * May not be completly RFC conform! 69910a76f6fSfrank * 70010a76f6fSfrank * @link http://www.webmasterworld.com/forum88/135.htm 70110a76f6fSfrank * 70210a76f6fSfrank * @param string $email the address to check 70310a76f6fSfrank * @return bool true if address is valid 70410a76f6fSfrank */ 70510a76f6fSfrankfunction isvalidemail($email){ 70610a76f6fSfrank return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 70710a76f6fSfrank} 70810a76f6fSfrank 709b0855b11Sandi/** 710b0855b11Sandi * Encrypts a password using the given method and salt 711b0855b11Sandi * 712b0855b11Sandi * If the selected method needs a salt and none was given, a random one 713b0855b11Sandi * is chosen. 714b0855b11Sandi * 715b0855b11Sandi * The following methods are understood: 716b0855b11Sandi * 717b0855b11Sandi * smd5 - Salted MD5 hashing 718b0855b11Sandi * md5 - Simple MD5 hashing 719b0855b11Sandi * sha1 - SHA1 hashing 720b0855b11Sandi * ssha - Salted SHA1 hashing 721d7be6245Sandi * crypt - Unix crypt 722d7be6245Sandi * mysql - MySQL password (old method) 723d7be6245Sandi * my411 - MySQL 4.1.1 password 724b0855b11Sandi * 725b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 726b0855b11Sandi * @return string The crypted password 727b0855b11Sandi */ 728b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){ 729b0855b11Sandi global $conf; 730b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 73110a76f6fSfrank 732b0855b11Sandi //prepare a salt 733b0855b11Sandi if(empty($salt)) $salt = md5(uniqid(rand(), true)); 734b0855b11Sandi 735b0855b11Sandi switch(strtolower($method)){ 736b0855b11Sandi case 'smd5': 737b0855b11Sandi return crypt($clear,'$1$'.substr($salt,0,8).'$'); 738b0855b11Sandi case 'md5': 739b0855b11Sandi return md5($clear); 740b0855b11Sandi case 'sha1': 741b0855b11Sandi return sha1($clear); 742b0855b11Sandi case 'ssha': 743b0855b11Sandi $salt=substr($salt,0,4); 744d6e54e02Smatthiasgrimm return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 745b0855b11Sandi case 'crypt': 746b0855b11Sandi return crypt($clear,substr($salt,0,2)); 747d7be6245Sandi case 'mysql': 748d7be6245Sandi //from http://www.php.net/mysql comment by <soren at byu dot edu> 749d7be6245Sandi $nr=0x50305735; 750d7be6245Sandi $nr2=0x12345671; 751d7be6245Sandi $add=7; 752d7be6245Sandi $charArr = preg_split("//", $clear); 753d7be6245Sandi foreach ($charArr as $char) { 754d7be6245Sandi if (($char == '') || ($char == ' ') || ($char == '\t')) continue; 755d7be6245Sandi $charVal = ord($char); 756d7be6245Sandi $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 757d7be6245Sandi $nr2 += ($nr2 << 8) ^ $nr; 758d7be6245Sandi $add += $charVal; 759d7be6245Sandi } 760d7be6245Sandi return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 761d7be6245Sandi case 'my411': 762d7be6245Sandi return '*'.sha1(pack("H*", sha1($clear))); 763b0855b11Sandi default: 764b0855b11Sandi msg("Unsupported crypt method $method",-1); 765b0855b11Sandi } 766b0855b11Sandi} 767b0855b11Sandi 768b0855b11Sandi/** 769b0855b11Sandi * Verifies a cleartext password against a crypted hash 770b0855b11Sandi * 771b0855b11Sandi * The method and salt used for the crypted hash is determined automatically 772b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs 773b0855b11Sandi * match true is is returned else false 774b0855b11Sandi * 775b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 776b0855b11Sandi * @return bool 777b0855b11Sandi */ 778b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 779b0855b11Sandi $method=''; 780b0855b11Sandi $salt=''; 781b0855b11Sandi 782b0855b11Sandi //determine the used method and salt 783d7be6245Sandi $len = strlen($crypt); 784b0855b11Sandi if(substr($crypt,0,3) == '$1$'){ 785b0855b11Sandi $method = 'smd5'; 786b0855b11Sandi $salt = substr($crypt,3,8); 787b0855b11Sandi }elseif(substr($crypt,0,6) == '{SSHA}'){ 788b0855b11Sandi $method = 'ssha'; 789b0855b11Sandi $salt = substr(base64_decode(substr($crypt, 6)),20); 790d7be6245Sandi }elseif($len == 32){ 791b0855b11Sandi $method = 'md5'; 792d7be6245Sandi }elseif($len == 40){ 793b0855b11Sandi $method = 'sha1'; 794d7be6245Sandi }elseif($len == 16){ 795d7be6245Sandi $method = 'mysql'; 796d7be6245Sandi }elseif($len == 41 && $crypt[0] == '*'){ 797d7be6245Sandi $method = 'my411'; 798b0855b11Sandi }else{ 799b0855b11Sandi $method = 'crypt'; 800b0855b11Sandi $salt = substr($crypt,0,2); 801b0855b11Sandi } 802b0855b11Sandi 803b0855b11Sandi //crypt and compare 804b0855b11Sandi if(auth_cryptPassword($clear,$method,$salt) === $crypt){ 805b0855b11Sandi return true; 806b0855b11Sandi } 807b0855b11Sandi return false; 808b0855b11Sandi} 809340756e4Sandi 810340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 811