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 16742c66f8Schris global $conf; 17742c66f8Schris 181c73890cSAndreas Gohr if($conf['useacl']){ 19ed7b5f09Sandi require_once(DOKU_INC.'inc/blowfish.php'); 20ed7b5f09Sandi require_once(DOKU_INC.'inc/mail.php'); 218b06d178Schris 2203c4aec3Schris global $auth; 2303c4aec3Schris 248b06d178Schris // load the the backend auth functions and instantiate the auth object 258b06d178Schris if (@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) { 268b06d178Schris require_once(DOKU_INC.'inc/auth/basic.class.php'); 278b06d178Schris require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php'); 288b06d178Schris 298b06d178Schris $auth_class = "auth_".$conf['authtype']; 30cd52f92dSchris if (class_exists($auth_class)) { 318b06d178Schris $auth = new $auth_class(); 32d2dde4ebSMatthias Grimm if ($auth->success == false) { 33d2dde4ebSMatthias Grimm unset($auth); 34cd52f92dSchris msg($lang['authtempfail'], -1); 35cd52f92dSchris 36cd52f92dSchris // turn acl config setting off for the rest of this page 37cd52f92dSchris $conf['useacl'] = 0; 38d2dde4ebSMatthias Grimm } 398b06d178Schris } else { 403816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 41cd52f92dSchris } 42cd52f92dSchris } else { 433816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 448b06d178Schris } 451c73890cSAndreas Gohr } 46f3f0262cSandi 471e866646Sandi if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5($conf['title'])); 48e65afed4SSameer D. Sahasrabuddhe 4915fae107Sandi // some ACL level defines 50f3f0262cSandi define('AUTH_NONE',0); 51f3f0262cSandi define('AUTH_READ',1); 52f3f0262cSandi define('AUTH_EDIT',2); 53f3f0262cSandi define('AUTH_CREATE',4); 54f3f0262cSandi define('AUTH_UPLOAD',8); 558ef6b7caSandi define('AUTH_DELETE',16); 5610a76f6fSfrank define('AUTH_ADMIN',255); 57f3f0262cSandi 58f5cb575dSAndreas Gohr // do the login either by cookie or provided credentials 59f3f0262cSandi if($conf['useacl']){ 60bbbd6568SAndreas Gohr if (!isset($_REQUEST['u'])) $_REQUEST['u'] = ''; 61bbbd6568SAndreas Gohr if (!isset($_REQUEST['p'])) $_REQUEST['p'] = ''; 62bbbd6568SAndreas Gohr if (!isset($_REQUEST['r'])) $_REQUEST['r'] = ''; 63bbbd6568SAndreas Gohr 641e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 654a26ad85Schris if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])){ 661e8c9c90SAndreas Gohr $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER']; 671e8c9c90SAndreas Gohr $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW']; 681e8c9c90SAndreas Gohr } 691e8c9c90SAndreas Gohr 70f5cb575dSAndreas Gohr // external trust mechanism in place? 7182fd59b6SAndreas Gohr if(!is_null($auth) && $auth->canDo('external')){ 72f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 73f5cb575dSAndreas Gohr }else{ 74132bdbfeSandi auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 75f5cb575dSAndreas Gohr } 76f5cb575dSAndreas Gohr 7715fae107Sandi //load ACL into a global array 78e7cb32dcSAndreas Gohr if(is_readable(DOKU_CONF.'acl.auth.php')){ 79e7cb32dcSAndreas Gohr $AUTH_ACL = file(DOKU_CONF.'acl.auth.php'); 8011799630Sandi }else{ 8111799630Sandi $AUTH_ACL = array(); 8211799630Sandi } 83f3f0262cSandi } 84f3f0262cSandi 85f3f0262cSandi/** 86f3f0262cSandi * This tries to login the user based on the sent auth credentials 87f3f0262cSandi * 88f3f0262cSandi * The authentication works like this: if a username was given 8915fae107Sandi * a new login is assumed and user/password are checked. If they 9015fae107Sandi * are correct the password is encrypted with blowfish and stored 9115fae107Sandi * together with the username in a cookie - the same info is stored 9215fae107Sandi * in the session, too. Additonally a browserID is stored in the 9315fae107Sandi * session. 9415fae107Sandi * 9515fae107Sandi * If no username was given the cookie is checked: if the username, 9615fae107Sandi * crypted password and browserID match between session and cookie 9715fae107Sandi * no further testing is done and the user is accepted 9815fae107Sandi * 9915fae107Sandi * If a cookie was found but no session info was availabe the 100136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 10115fae107Sandi * together with username rechecked by calling this function again. 102f3f0262cSandi * 103f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 104f3f0262cSandi * are set. 10515fae107Sandi * 10615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 10715fae107Sandi * 10815fae107Sandi * @param string $user Username 10915fae107Sandi * @param string $pass Cleartext Password 11015fae107Sandi * @param bool $sticky Cookie should not expire 11115fae107Sandi * @return bool true on successful auth 112f3f0262cSandi*/ 113132bdbfeSandifunction auth_login($user,$pass,$sticky=false){ 114f3f0262cSandi global $USERINFO; 115f3f0262cSandi global $conf; 116f3f0262cSandi global $lang; 117cd52f92dSchris global $auth; 118132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 119f3f0262cSandi 120bbbd6568SAndreas Gohr if(!empty($user)){ 121132bdbfeSandi //usual login 122cd52f92dSchris if ($auth->checkPass($user,$pass)){ 123132bdbfeSandi // make logininfo globally available 124f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 125cd52f92dSchris $USERINFO = $auth->getUserData($user); //FIXME move all references to session 126132bdbfeSandi 127132bdbfeSandi // set cookie 128132bdbfeSandi $pass = PMA_blowfish_encrypt($pass,auth_cookiesalt()); 129132bdbfeSandi $cookie = base64_encode("$user|$sticky|$pass"); 130132bdbfeSandi if($sticky) $time = time()+60*60*24*365; //one year 131e65afed4SSameer D. Sahasrabuddhe setcookie(DOKU_COOKIE,$cookie,$time,'/'); 132132bdbfeSandi 133132bdbfeSandi // set session 134132bdbfeSandi $_SESSION[$conf['title']]['auth']['user'] = $user; 135132bdbfeSandi $_SESSION[$conf['title']]['auth']['pass'] = $pass; 136132bdbfeSandi $_SESSION[$conf['title']]['auth']['buid'] = auth_browseruid(); 137132bdbfeSandi $_SESSION[$conf['title']]['auth']['info'] = $USERINFO; 138132bdbfeSandi return true; 139f3f0262cSandi }else{ 140f3f0262cSandi //invalid credentials - log off 141f3f0262cSandi msg($lang['badlogin'],-1); 142f3f0262cSandi auth_logoff(); 143132bdbfeSandi return false; 144f3f0262cSandi } 145f3f0262cSandi }else{ 146132bdbfeSandi // read cookie information 147e65afed4SSameer D. Sahasrabuddhe $cookie = base64_decode($_COOKIE[DOKU_COOKIE]); 148132bdbfeSandi list($user,$sticky,$pass) = split('\|',$cookie,3); 149132bdbfeSandi // get session info 150132bdbfeSandi $session = $_SESSION[$conf['title']]['auth']; 151132bdbfeSandi 152132bdbfeSandi if($user && $pass){ 153132bdbfeSandi // we got a cookie - see if we can trust it 154132bdbfeSandi if(isset($session) && 155132bdbfeSandi ($session['user'] == $user) && 156132bdbfeSandi ($session['pass'] == $pass) && //still crypted 157132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 158132bdbfeSandi // he has session, cookie and browser right - let him in 159132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 160132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 161132bdbfeSandi return true; 162132bdbfeSandi } 163132bdbfeSandi // no we don't trust it yet - recheck pass 164132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 165132bdbfeSandi return auth_login($user,$pass,$sticky); 166132bdbfeSandi } 167132bdbfeSandi } 168f3f0262cSandi //just to be sure 169f3f0262cSandi auth_logoff(); 170132bdbfeSandi return false; 171f3f0262cSandi} 172132bdbfeSandi 173132bdbfeSandi/** 174136ce040Sandi * Builds a pseudo UID from browser and IP data 175132bdbfeSandi * 176132bdbfeSandi * This is neither unique nor unfakable - still it adds some 177136ce040Sandi * security. Using the first part of the IP makes sure 178136ce040Sandi * proxy farms like AOLs are stil okay. 17915fae107Sandi * 18015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 18115fae107Sandi * 18215fae107Sandi * @return string a MD5 sum of various browser headers 183132bdbfeSandi */ 184132bdbfeSandifunction auth_browseruid(){ 185132bdbfeSandi $uid = ''; 186132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 187132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 188132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 189132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 190136ce040Sandi $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.')); 191132bdbfeSandi return md5($uid); 192132bdbfeSandi} 193132bdbfeSandi 194132bdbfeSandi/** 195132bdbfeSandi * Creates a random key to encrypt the password in cookies 19615fae107Sandi * 19715fae107Sandi * This function tries to read the password for encrypting 19898407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 19915fae107Sandi * if no such file is found a random key is created and 20015fae107Sandi * and stored in this file. 20115fae107Sandi * 20215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 20315fae107Sandi * 20415fae107Sandi * @return string 205132bdbfeSandi */ 206132bdbfeSandifunction auth_cookiesalt(){ 207132bdbfeSandi global $conf; 20898407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 209132bdbfeSandi $salt = io_readFile($file); 210132bdbfeSandi if(empty($salt)){ 211132bdbfeSandi $salt = uniqid(rand(),true); 212132bdbfeSandi io_saveFile($file,$salt); 213132bdbfeSandi } 214132bdbfeSandi return $salt; 215f3f0262cSandi} 216f3f0262cSandi 217f3f0262cSandi/** 218f3f0262cSandi * This clears all authenticationdata and thus log the user 219f3f0262cSandi * off 22015fae107Sandi * 22115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 222f3f0262cSandi */ 223f3f0262cSandifunction auth_logoff(){ 224f3f0262cSandi global $conf; 225f3f0262cSandi global $USERINFO; 2268b06d178Schris global $INFO, $ID; 2275298a619SAndreas Gohr global $auth; 22837065e65Sandi 22937065e65Sandi if(isset($_SESSION[$conf['title']]['auth']['user'])) 230132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['user']); 23137065e65Sandi if(isset($_SESSION[$conf['title']]['auth']['pass'])) 232132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['pass']); 23337065e65Sandi if(isset($_SESSION[$conf['title']]['auth']['info'])) 234132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['info']); 23537065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 236f3f0262cSandi unset($_SERVER['REMOTE_USER']); 237132bdbfeSandi $USERINFO=null; //FIXME 2381e866646Sandi setcookie(DOKU_COOKIE,'',time()-600000,'/'); 2395298a619SAndreas Gohr 2405298a619SAndreas Gohr if($auth && $auth->canDo('logoff')){ 2415298a619SAndreas Gohr $auth->logOff(); 2425298a619SAndreas Gohr } 243f3f0262cSandi} 244f3f0262cSandi 245f3f0262cSandi/** 24615fae107Sandi * Convinience function for auth_aclcheck() 24715fae107Sandi * 24815fae107Sandi * This checks the permissions for the current user 24915fae107Sandi * 25015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 25115fae107Sandi * 25215fae107Sandi * @param string $id page ID 25315fae107Sandi * @return int permission level 254f3f0262cSandi */ 255f3f0262cSandifunction auth_quickaclcheck($id){ 256f3f0262cSandi global $conf; 257f3f0262cSandi global $USERINFO; 258f3f0262cSandi # if no ACL is used always return upload rights 259f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 260f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 261f3f0262cSandi} 262f3f0262cSandi 263f3f0262cSandi/** 264f3f0262cSandi * Returns the maximum rights a user has for 265f3f0262cSandi * the given ID or its namespace 26615fae107Sandi * 26715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 26815fae107Sandi * 26915fae107Sandi * @param string $id page ID 27015fae107Sandi * @param string $user Username 27115fae107Sandi * @param array $groups Array of groups the user is in 27215fae107Sandi * @return int permission level 273f3f0262cSandi */ 274f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 275f3f0262cSandi global $conf; 276f3f0262cSandi global $AUTH_ACL; 277f3f0262cSandi 278f3f0262cSandi # if no ACL is used always return upload rights 279f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 280f3f0262cSandi 2816c2bb100SAndreas Gohr $user = auth_nameencode($user); 2826c2bb100SAndreas Gohr 28310a76f6fSfrank //if user is superuser return 255 (acl_admin) 284e838fc2eSAndreas Gohr if(auth_nameencode($conf['superuser']) == $user) { return AUTH_ADMIN; } 28510a76f6fSfrank 286074cf26bSandi //make sure groups is an array 287074cf26bSandi if(!is_array($groups)) $groups = array(); 288074cf26bSandi 2896c2bb100SAndreas Gohr //prepend groups with @ and nameencode 2902cd2db38Sandi $cnt = count($groups); 2912cd2db38Sandi for($i=0; $i<$cnt; $i++){ 2926c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 29310a76f6fSfrank } 29410a76f6fSfrank //if user is in superuser group return 255 (acl_admin) 295e838fc2eSAndreas Gohr if(in_array(auth_nameencode($conf['superuser'],true), $groups)) { return AUTH_ADMIN; } 29610a76f6fSfrank 297f3f0262cSandi $ns = getNS($id); 298f3f0262cSandi $perm = -1; 299f3f0262cSandi 300f3f0262cSandi if($user){ 301f3f0262cSandi //add ALL group 302f3f0262cSandi $groups[] = '@ALL'; 303f3f0262cSandi //add User 304f3f0262cSandi $groups[] = $user; 305f3f0262cSandi //build regexp 306f3f0262cSandi $regexp = join('|',$groups); 307f3f0262cSandi }else{ 308f3f0262cSandi $regexp = '@ALL'; 309f3f0262cSandi } 310f3f0262cSandi 311f3f0262cSandi //check exact match first 31242905504SAndreas Gohr $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL); 313f3f0262cSandi if(count($matches)){ 314f3f0262cSandi foreach($matches as $match){ 315f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 316f3f0262cSandi $acl = preg_split('/\s+/',$match); 3178ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 318f3f0262cSandi if($acl[2] > $perm){ 319f3f0262cSandi $perm = $acl[2]; 320f3f0262cSandi } 321f3f0262cSandi } 322f3f0262cSandi if($perm > -1){ 323f3f0262cSandi //we had a match - return it 324f3f0262cSandi return $perm; 325f3f0262cSandi } 326f3f0262cSandi } 327f3f0262cSandi 328f3f0262cSandi //still here? do the namespace checks 329f3f0262cSandi if($ns){ 330f3f0262cSandi $path = $ns.':\*'; 331f3f0262cSandi }else{ 332f3f0262cSandi $path = '\*'; //root document 333f3f0262cSandi } 334f3f0262cSandi 335f3f0262cSandi do{ 336f3f0262cSandi $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL); 337f3f0262cSandi if(count($matches)){ 338f3f0262cSandi foreach($matches as $match){ 339f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 340f3f0262cSandi $acl = preg_split('/\s+/',$match); 3418ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 342f3f0262cSandi if($acl[2] > $perm){ 343f3f0262cSandi $perm = $acl[2]; 344f3f0262cSandi } 345f3f0262cSandi } 346f3f0262cSandi //we had a match - return it 347f3f0262cSandi return $perm; 348f3f0262cSandi } 349f3f0262cSandi 350f3f0262cSandi //get next higher namespace 351f3f0262cSandi $ns = getNS($ns); 352f3f0262cSandi 353f3f0262cSandi if($path != '\*'){ 354f3f0262cSandi $path = $ns.':\*'; 355f3f0262cSandi if($path == ':\*') $path = '\*'; 356f3f0262cSandi }else{ 357f3f0262cSandi //we did this already 358f3f0262cSandi //looks like there is something wrong with the ACL 359f3f0262cSandi //break here 360d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 361d5ce66f6SAndreas Gohr return AUTH_NONE; 362f3f0262cSandi } 363f3f0262cSandi }while(1); //this should never loop endless 36452a5af8dSandi 36552a5af8dSandi //still here? return no permissions 36652a5af8dSandi return AUTH_NONE; 367f3f0262cSandi} 368f3f0262cSandi 369f3f0262cSandi/** 3706c2bb100SAndreas Gohr * Encode ASCII special chars 3716c2bb100SAndreas Gohr * 3726c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 3736c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 3746c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 3756c2bb100SAndreas Gohr * urlencoding!). 3766c2bb100SAndreas Gohr * 3776c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 3786c2bb100SAndreas Gohr * 3796c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 3806c2bb100SAndreas Gohr * @see rawurldecode() 3816c2bb100SAndreas Gohr */ 382e838fc2eSAndreas Gohrfunction auth_nameencode($name,$skip_group=false){ 383e838fc2eSAndreas Gohr if($skip_group && $name{0} =='@'){ 384e838fc2eSAndreas Gohr return '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 385e838fc2eSAndreas Gohr "'%'.dechex(ord('\\1'))",substr($name,1)); 386e838fc2eSAndreas Gohr }else{ 387e838fc2eSAndreas Gohr return preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 388e838fc2eSAndreas Gohr "'%'.dechex(ord('\\1'))",$name); 389e838fc2eSAndreas Gohr } 3906c2bb100SAndreas Gohr} 3916c2bb100SAndreas Gohr 3926c2bb100SAndreas Gohr/** 393f3f0262cSandi * Create a pronouncable password 394f3f0262cSandi * 39515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 39615fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 39715fae107Sandi * 39815fae107Sandi * @return string pronouncable password 399f3f0262cSandi */ 400f3f0262cSandifunction auth_pwgen(){ 401f3f0262cSandi $pw = ''; 402f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 403f3f0262cSandi $v = 'aeiou'; //vowels 404f3f0262cSandi $a = $c.$v; //both 405f3f0262cSandi 406f3f0262cSandi //use two syllables... 407f3f0262cSandi for($i=0;$i < 2; $i++){ 408f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 409f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 410f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 411f3f0262cSandi } 412f3f0262cSandi //... and add a nice number 413f3f0262cSandi $pw .= rand(10,99); 414f3f0262cSandi 415f3f0262cSandi return $pw; 416f3f0262cSandi} 417f3f0262cSandi 418f3f0262cSandi/** 419f3f0262cSandi * Sends a password to the given user 420f3f0262cSandi * 42115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 42215fae107Sandi * 42315fae107Sandi * @return bool true on success 424f3f0262cSandi */ 425f3f0262cSandifunction auth_sendPassword($user,$password){ 426f3f0262cSandi global $conf; 427f3f0262cSandi global $lang; 428cd52f92dSchris global $auth; 429cd52f92dSchris 430f3f0262cSandi $hdrs = ''; 431cd52f92dSchris $userinfo = $auth->getUserData($user); 432f3f0262cSandi 43387ddda95Sandi if(!$userinfo['mail']) return false; 434f3f0262cSandi 435f3f0262cSandi $text = rawLocale('password'); 436ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 43787ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 438f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 439f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 440f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 441f3f0262cSandi 44244f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 44344f669e9Sandi $lang['regpwmail'], 44444f669e9Sandi $text, 44544f669e9Sandi $conf['mailfrom']); 446f3f0262cSandi} 447f3f0262cSandi 448f3f0262cSandi/** 44915fae107Sandi * Register a new user 450f3f0262cSandi * 45115fae107Sandi * This registers a new user - Data is read directly from $_POST 45215fae107Sandi * 45315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 45415fae107Sandi * 45515fae107Sandi * @return bool true on success, false on any error 456f3f0262cSandi */ 457f3f0262cSandifunction register(){ 458f3f0262cSandi global $lang; 459eb5d07e4Sjan global $conf; 460cd52f92dSchris global $auth; 461f3f0262cSandi 462f3f0262cSandi if(!$_POST['save']) return false; 46382fd59b6SAndreas Gohr if(!$auth->canDo('addUser')) return false; 464640145a5Sandi 465f3f0262cSandi //clean username 466f3f0262cSandi $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 467f3f0262cSandi $_POST['login'] = cleanID($_POST['login']); 468f3f0262cSandi //clean fullname and email 46911d989c3SAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%]+/','',$_POST['fullname'])); 47011d989c3SAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%]+/','',$_POST['email'])); 471f3f0262cSandi 472f3f0262cSandi if( empty($_POST['login']) || 473f3f0262cSandi empty($_POST['fullname']) || 474f3f0262cSandi empty($_POST['email']) ){ 475f3f0262cSandi msg($lang['regmissing'],-1); 476f3f0262cSandi return false; 477f3f0262cSandi } 478f3f0262cSandi 479cab2716aSmatthias.grimm if ($conf['autopasswd']) { 480cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 481cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 482cab2716aSmatthias.grimm empty($_POST['passchk'])) { 483bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 484cab2716aSmatthias.grimm return false; 485cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 486bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 487cab2716aSmatthias.grimm return false; 488cab2716aSmatthias.grimm } else { 489cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 490cab2716aSmatthias.grimm } 491cab2716aSmatthias.grimm 492f3f0262cSandi //check mail 49344f669e9Sandi if(!mail_isvalid($_POST['email'])){ 494f3f0262cSandi msg($lang['regbadmail'],-1); 495f3f0262cSandi return false; 496f3f0262cSandi } 497f3f0262cSandi 498f3f0262cSandi //okay try to create the user 4991d096a10SAndreas Gohr if(!$auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email'])){ 500f3f0262cSandi msg($lang['reguexists'],-1); 501f3f0262cSandi return false; 502f3f0262cSandi } 503f3f0262cSandi 504*02a498e7Schris // create substitutions for use in notification email 505*02a498e7Schris $substitutions = array( 506*02a498e7Schris 'NEWUSER' => $_POST['login'], 507*02a498e7Schris 'NEWNAME' => $_POST['fullname'], 508*02a498e7Schris 'NEWEMAIL' => $_POST['email'], 509*02a498e7Schris ); 510*02a498e7Schris 511cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 512cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 513*02a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 514cab2716aSmatthias.grimm return true; 515cab2716aSmatthias.grimm } 516cab2716aSmatthias.grimm 517cab2716aSmatthias.grimm // autogenerated password? then send him the password 518f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 519f3f0262cSandi msg($lang['regsuccess'],1); 520*02a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 521f3f0262cSandi return true; 522f3f0262cSandi }else{ 523f3f0262cSandi msg($lang['regmailfail'],-1); 524f3f0262cSandi return false; 525f3f0262cSandi } 526f3f0262cSandi} 527f3f0262cSandi 52810a76f6fSfrank/** 5298b06d178Schris * Update user profile 5308b06d178Schris * 5318b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 5328b06d178Schris */ 5338b06d178Schrisfunction updateprofile() { 5348b06d178Schris global $conf; 5358b06d178Schris global $INFO; 5368b06d178Schris global $lang; 537cd52f92dSchris global $auth; 5388b06d178Schris 5398b06d178Schris if(!$_POST['save']) return false; 5408b06d178Schris 54182fd59b6SAndreas Gohr // should not be able to get here without Profile being possible... 54282fd59b6SAndreas Gohr if(!$auth->canDo('Profile')) { 5438b06d178Schris msg($lang['profna'],-1); 5448b06d178Schris return false; 5458b06d178Schris } 5468b06d178Schris 5478b06d178Schris if ($_POST['newpass'] != $_POST['passchk']) { 5488b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 5498b06d178Schris return false; 5508b06d178Schris } 5518b06d178Schris 5528b06d178Schris //clean fullname and email 55311d989c3SAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%]+/','',$_POST['fullname'])); 55411d989c3SAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%]+/','',$_POST['email'])); 5558b06d178Schris 5568b06d178Schris if (empty($_POST['fullname']) || empty($_POST['email'])) { 5578b06d178Schris msg($lang['profnoempty'],-1); 5588b06d178Schris return false; 5598b06d178Schris } 5608b06d178Schris 5618b06d178Schris if (!mail_isvalid($_POST['email'])){ 5628b06d178Schris msg($lang['regbadmail'],-1); 5638b06d178Schris return false; 5648b06d178Schris } 5658b06d178Schris 5668b06d178Schris if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname']; 5678b06d178Schris if ($_POST['email'] != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email']; 5688b06d178Schris if (!empty($_POST['newpass'])) $changes['pass'] = $_POST['newpass']; 5698b06d178Schris 5708b06d178Schris if (!count($changes)) { 5718b06d178Schris msg($lang['profnochange'], -1); 5728b06d178Schris return false; 5738b06d178Schris } 5748b06d178Schris 5758b06d178Schris if ($conf['profileconfirm']) { 5768b06d178Schris if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) { 5778b06d178Schris msg($lang['badlogin'],-1); 5788b06d178Schris return false; 5798b06d178Schris } 5808b06d178Schris } 5818b06d178Schris 582cd52f92dSchris return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes); 5838b06d178Schris} 5848b06d178Schris 5858b06d178Schris/** 5868b06d178Schris * Send a new password 5878b06d178Schris * 5881d5856cfSAndreas Gohr * This function handles both phases of the password reset: 5891d5856cfSAndreas Gohr * 5901d5856cfSAndreas Gohr * - handling the first request of password reset 5911d5856cfSAndreas Gohr * - validating the password reset auth token 5921d5856cfSAndreas Gohr * 5938b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 5948b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 5951d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 5968b06d178Schris * 5978b06d178Schris * @return bool true on success, false on any error 5988b06d178Schris*/ 5998b06d178Schrisfunction act_resendpwd(){ 6008b06d178Schris global $lang; 6018b06d178Schris global $conf; 602cd52f92dSchris global $auth; 6038b06d178Schris 604409d7af7SAndreas Gohr if(!actionOK('resendpwd')) return false; 6058b06d178Schris 60682fd59b6SAndreas Gohr // should not be able to get here without modPass being possible... 60782fd59b6SAndreas Gohr if(!$auth->canDo('modPass')) { 6088b06d178Schris msg($lang['resendna'],-1); 6098b06d178Schris return false; 6108b06d178Schris } 6118b06d178Schris 6121d5856cfSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']); 6138b06d178Schris 6141d5856cfSAndreas Gohr if($token){ 6151d5856cfSAndreas Gohr // we're in token phase 6161d5856cfSAndreas Gohr 6171d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 6181d5856cfSAndreas Gohr if(!@file_exists($tfile)){ 6191d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'],-1); 6201d5856cfSAndreas Gohr return false; 6211d5856cfSAndreas Gohr } 6221d5856cfSAndreas Gohr $user = io_readfile($tfile); 6231d5856cfSAndreas Gohr @unlink($tfile); 624cd52f92dSchris $userinfo = $auth->getUserData($user); 6258b06d178Schris if(!$userinfo['mail']) { 6268b06d178Schris msg($lang['resendpwdnouser'], -1); 6278b06d178Schris return false; 6288b06d178Schris } 6298b06d178Schris 6308b06d178Schris $pass = auth_pwgen(); 631cd52f92dSchris if (!$auth->modifyUser($user,array('pass' => $pass))) { 6328b06d178Schris msg('error modifying user data',-1); 6338b06d178Schris return false; 6348b06d178Schris } 6358b06d178Schris 6368b06d178Schris if (auth_sendPassword($user,$pass)) { 6378b06d178Schris msg($lang['resendpwdsuccess'],1); 6388b06d178Schris } else { 6398b06d178Schris msg($lang['regmailfail'],-1); 6408b06d178Schris } 6418b06d178Schris return true; 6421d5856cfSAndreas Gohr 6431d5856cfSAndreas Gohr } else { 6441d5856cfSAndreas Gohr // we're in request phase 6451d5856cfSAndreas Gohr 6461d5856cfSAndreas Gohr if(!$_POST['save']) return false; 6471d5856cfSAndreas Gohr 6481d5856cfSAndreas Gohr if (empty($_POST['login'])) { 6491d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 6501d5856cfSAndreas Gohr return false; 6511d5856cfSAndreas Gohr } else { 6521d5856cfSAndreas Gohr $user = $_POST['login']; 6531d5856cfSAndreas Gohr } 6541d5856cfSAndreas Gohr 6551d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 6561d5856cfSAndreas Gohr if(!$userinfo['mail']) { 6571d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 6581d5856cfSAndreas Gohr return false; 6591d5856cfSAndreas Gohr } 6601d5856cfSAndreas Gohr 6611d5856cfSAndreas Gohr // generate auth token 6621d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 6631d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 6641d5856cfSAndreas Gohr $url = wl('',array('do'=>'resendpwd','pwauth'=>$token),true,'&'); 6651d5856cfSAndreas Gohr 6661d5856cfSAndreas Gohr io_saveFile($tfile,$user); 6671d5856cfSAndreas Gohr 6681d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 6691d5856cfSAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 6701d5856cfSAndreas Gohr $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 6711d5856cfSAndreas Gohr $text = str_replace('@LOGIN@',$user,$text); 6721d5856cfSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 6731d5856cfSAndreas Gohr $text = str_replace('@CONFIRM@',$url,$text); 6741d5856cfSAndreas Gohr 6751d5856cfSAndreas Gohr if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 6761d5856cfSAndreas Gohr $lang['regpwmail'], 6771d5856cfSAndreas Gohr $text, 6781d5856cfSAndreas Gohr $conf['mailfrom'])){ 6791d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'],1); 6801d5856cfSAndreas Gohr }else{ 6811d5856cfSAndreas Gohr msg($lang['regmailfail'],-1); 6821d5856cfSAndreas Gohr } 6831d5856cfSAndreas Gohr return true; 6841d5856cfSAndreas Gohr } 6851d5856cfSAndreas Gohr 6861d5856cfSAndreas Gohr return false; // never reached 6878b06d178Schris} 6888b06d178Schris 6898b06d178Schris/** 69010a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid 69110a76f6fSfrank * 69210a76f6fSfrank * May not be completly RFC conform! 69310a76f6fSfrank * 69410a76f6fSfrank * @link http://www.webmasterworld.com/forum88/135.htm 69510a76f6fSfrank * 69610a76f6fSfrank * @param string $email the address to check 69710a76f6fSfrank * @return bool true if address is valid 69810a76f6fSfrank */ 69910a76f6fSfrankfunction isvalidemail($email){ 70010a76f6fSfrank return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 70110a76f6fSfrank} 70210a76f6fSfrank 703b0855b11Sandi/** 704b0855b11Sandi * Encrypts a password using the given method and salt 705b0855b11Sandi * 706b0855b11Sandi * If the selected method needs a salt and none was given, a random one 707b0855b11Sandi * is chosen. 708b0855b11Sandi * 709b0855b11Sandi * The following methods are understood: 710b0855b11Sandi * 711b0855b11Sandi * smd5 - Salted MD5 hashing 712b0855b11Sandi * md5 - Simple MD5 hashing 713b0855b11Sandi * sha1 - SHA1 hashing 714b0855b11Sandi * ssha - Salted SHA1 hashing 715d7be6245Sandi * crypt - Unix crypt 716d7be6245Sandi * mysql - MySQL password (old method) 717d7be6245Sandi * my411 - MySQL 4.1.1 password 718b0855b11Sandi * 719b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 720b0855b11Sandi * @return string The crypted password 721b0855b11Sandi */ 722b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){ 723b0855b11Sandi global $conf; 724b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 72510a76f6fSfrank 726b0855b11Sandi //prepare a salt 727b0855b11Sandi if(empty($salt)) $salt = md5(uniqid(rand(), true)); 728b0855b11Sandi 729b0855b11Sandi switch(strtolower($method)){ 730b0855b11Sandi case 'smd5': 731b0855b11Sandi return crypt($clear,'$1$'.substr($salt,0,8).'$'); 732b0855b11Sandi case 'md5': 733b0855b11Sandi return md5($clear); 734b0855b11Sandi case 'sha1': 735b0855b11Sandi return sha1($clear); 736b0855b11Sandi case 'ssha': 737b0855b11Sandi $salt=substr($salt,0,4); 738d6e54e02Smatthiasgrimm return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 739b0855b11Sandi case 'crypt': 740b0855b11Sandi return crypt($clear,substr($salt,0,2)); 741d7be6245Sandi case 'mysql': 742d7be6245Sandi //from http://www.php.net/mysql comment by <soren at byu dot edu> 743d7be6245Sandi $nr=0x50305735; 744d7be6245Sandi $nr2=0x12345671; 745d7be6245Sandi $add=7; 746d7be6245Sandi $charArr = preg_split("//", $clear); 747d7be6245Sandi foreach ($charArr as $char) { 748d7be6245Sandi if (($char == '') || ($char == ' ') || ($char == '\t')) continue; 749d7be6245Sandi $charVal = ord($char); 750d7be6245Sandi $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 751d7be6245Sandi $nr2 += ($nr2 << 8) ^ $nr; 752d7be6245Sandi $add += $charVal; 753d7be6245Sandi } 754d7be6245Sandi return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 755d7be6245Sandi case 'my411': 756d7be6245Sandi return '*'.sha1(pack("H*", sha1($clear))); 757b0855b11Sandi default: 758b0855b11Sandi msg("Unsupported crypt method $method",-1); 759b0855b11Sandi } 760b0855b11Sandi} 761b0855b11Sandi 762b0855b11Sandi/** 763b0855b11Sandi * Verifies a cleartext password against a crypted hash 764b0855b11Sandi * 765b0855b11Sandi * The method and salt used for the crypted hash is determined automatically 766b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs 767b0855b11Sandi * match true is is returned else false 768b0855b11Sandi * 769b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 770b0855b11Sandi * @return bool 771b0855b11Sandi */ 772b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 773b0855b11Sandi $method=''; 774b0855b11Sandi $salt=''; 775b0855b11Sandi 776b0855b11Sandi //determine the used method and salt 777d7be6245Sandi $len = strlen($crypt); 778b0855b11Sandi if(substr($crypt,0,3) == '$1$'){ 779b0855b11Sandi $method = 'smd5'; 780b0855b11Sandi $salt = substr($crypt,3,8); 781b0855b11Sandi }elseif(substr($crypt,0,6) == '{SSHA}'){ 782b0855b11Sandi $method = 'ssha'; 783b0855b11Sandi $salt = substr(base64_decode(substr($crypt, 6)),20); 784d7be6245Sandi }elseif($len == 32){ 785b0855b11Sandi $method = 'md5'; 786d7be6245Sandi }elseif($len == 40){ 787b0855b11Sandi $method = 'sha1'; 788d7be6245Sandi }elseif($len == 16){ 789d7be6245Sandi $method = 'mysql'; 790d7be6245Sandi }elseif($len == 41 && $crypt[0] == '*'){ 791d7be6245Sandi $method = 'my411'; 792b0855b11Sandi }else{ 793b0855b11Sandi $method = 'crypt'; 794b0855b11Sandi $salt = substr($crypt,0,2); 795b0855b11Sandi } 796b0855b11Sandi 797b0855b11Sandi //crypt and compare 798b0855b11Sandi if(auth_cryptPassword($clear,$method,$salt) === $crypt){ 799b0855b11Sandi return true; 800b0855b11Sandi } 801b0855b11Sandi return false; 802b0855b11Sandi} 803340756e4Sandi 804340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 805