xref: /dokuwiki/inc/auth.php (revision 5298a619f2836d718204a61624c6388923c51a29)
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');
15ed7b5f09Sandi  require_once(DOKU_INC.'inc/blowfish.php');
16ed7b5f09Sandi  require_once(DOKU_INC.'inc/mail.php');
178b06d178Schris
188b06d178Schris  // load the the backend auth functions and instantiate the auth object
198b06d178Schris  if (@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) {
208b06d178Schris    require_once(DOKU_INC.'inc/auth/basic.class.php');
218b06d178Schris    require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php');
228b06d178Schris
238b06d178Schris    $auth_class = "auth_".$conf['authtype'];
24cd52f92dSchris    if (class_exists($auth_class)) {
258b06d178Schris      $auth = new $auth_class();
26d2dde4ebSMatthias Grimm      if ($auth->success == false) {
27d2dde4ebSMatthias Grimm			  unset($auth);
28cd52f92dSchris				msg($lang['authtempfail'], -1);
29cd52f92dSchris
30cd52f92dSchris        // turn acl config setting off for the rest of this page
31cd52f92dSchris				$conf['useacl'] = 0;
32d2dde4ebSMatthias Grimm			}
338b06d178Schris		} else {
34cd52f92dSchris			die($lang['authmodfailed']);
35cd52f92dSchris		}
36cd52f92dSchris	} else {
37cd52f92dSchris	  die($lang['authmodfailed']);
388b06d178Schris	}
39f3f0262cSandi
401e866646Sandi  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5($conf['title']));
41e65afed4SSameer D. Sahasrabuddhe
4215fae107Sandi  // some ACL level defines
43f3f0262cSandi  define('AUTH_NONE',0);
44f3f0262cSandi  define('AUTH_READ',1);
45f3f0262cSandi  define('AUTH_EDIT',2);
46f3f0262cSandi  define('AUTH_CREATE',4);
47f3f0262cSandi  define('AUTH_UPLOAD',8);
488ef6b7caSandi  define('AUTH_DELETE',16);
4910a76f6fSfrank  define('AUTH_ADMIN',255);
50f3f0262cSandi
51f5cb575dSAndreas Gohr  // do the login either by cookie or provided credentials
52f3f0262cSandi  if($conf['useacl']){
53f5cb575dSAndreas Gohr    // external trust mechanism in place?
5482fd59b6SAndreas Gohr    if(!is_null($auth) && $auth->canDo('external')){
55f5cb575dSAndreas Gohr      $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
56f5cb575dSAndreas Gohr    }else{
57132bdbfeSandi      auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
58f5cb575dSAndreas Gohr    }
59f5cb575dSAndreas Gohr
6015fae107Sandi    //load ACL into a global array
61e7cb32dcSAndreas Gohr    if(is_readable(DOKU_CONF.'acl.auth.php')){
62e7cb32dcSAndreas Gohr      $AUTH_ACL = file(DOKU_CONF.'acl.auth.php');
6311799630Sandi    }else{
6411799630Sandi      $AUTH_ACL = array();
6511799630Sandi    }
66f3f0262cSandi  }
67f3f0262cSandi
68f3f0262cSandi/**
69f3f0262cSandi * This tries to login the user based on the sent auth credentials
70f3f0262cSandi *
71f3f0262cSandi * The authentication works like this: if a username was given
7215fae107Sandi * a new login is assumed and user/password are checked. If they
7315fae107Sandi * are correct the password is encrypted with blowfish and stored
7415fae107Sandi * together with the username in a cookie - the same info is stored
7515fae107Sandi * in the session, too. Additonally a browserID is stored in the
7615fae107Sandi * session.
7715fae107Sandi *
7815fae107Sandi * If no username was given the cookie is checked: if the username,
7915fae107Sandi * crypted password and browserID match between session and cookie
8015fae107Sandi * no further testing is done and the user is accepted
8115fae107Sandi *
8215fae107Sandi * If a cookie was found but no session info was availabe the
83136ce040Sandi * blowfish encrypted password from the cookie is decrypted and
8415fae107Sandi * together with username rechecked by calling this function again.
85f3f0262cSandi *
86f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO
87f3f0262cSandi * are set.
8815fae107Sandi *
8915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
9015fae107Sandi *
9115fae107Sandi * @param   string  $user    Username
9215fae107Sandi * @param   string  $pass    Cleartext Password
9315fae107Sandi * @param   bool    $sticky  Cookie should not expire
9415fae107Sandi * @return  bool             true on successful auth
95f3f0262cSandi*/
96132bdbfeSandifunction auth_login($user,$pass,$sticky=false){
97f3f0262cSandi  global $USERINFO;
98f3f0262cSandi  global $conf;
99f3f0262cSandi  global $lang;
100cd52f92dSchris	global $auth;
101132bdbfeSandi  $sticky ? $sticky = true : $sticky = false; //sanity check
102f3f0262cSandi
103f3f0262cSandi  if(isset($user)){
104132bdbfeSandi    //usual login
105cd52f92dSchris    if ($auth->checkPass($user,$pass)){
106132bdbfeSandi      // make logininfo globally available
107f3f0262cSandi      $_SERVER['REMOTE_USER'] = $user;
108cd52f92dSchris      $USERINFO = $auth->getUserData($user); //FIXME move all references to session
109132bdbfeSandi
110132bdbfeSandi      // set cookie
111132bdbfeSandi      $pass   = PMA_blowfish_encrypt($pass,auth_cookiesalt());
112132bdbfeSandi      $cookie = base64_encode("$user|$sticky|$pass");
113132bdbfeSandi      if($sticky) $time = time()+60*60*24*365; //one year
114e65afed4SSameer D. Sahasrabuddhe      setcookie(DOKU_COOKIE,$cookie,$time,'/');
115132bdbfeSandi
116132bdbfeSandi      // set session
117132bdbfeSandi      $_SESSION[$conf['title']]['auth']['user'] = $user;
118132bdbfeSandi      $_SESSION[$conf['title']]['auth']['pass'] = $pass;
119132bdbfeSandi      $_SESSION[$conf['title']]['auth']['buid'] = auth_browseruid();
120132bdbfeSandi      $_SESSION[$conf['title']]['auth']['info'] = $USERINFO;
121132bdbfeSandi      return true;
122f3f0262cSandi    }else{
123f3f0262cSandi      //invalid credentials - log off
124f3f0262cSandi      msg($lang['badlogin'],-1);
125f3f0262cSandi      auth_logoff();
126132bdbfeSandi      return false;
127f3f0262cSandi    }
128f3f0262cSandi  }else{
129132bdbfeSandi    // read cookie information
130e65afed4SSameer D. Sahasrabuddhe    $cookie = base64_decode($_COOKIE[DOKU_COOKIE]);
131132bdbfeSandi    list($user,$sticky,$pass) = split('\|',$cookie,3);
132132bdbfeSandi    // get session info
133132bdbfeSandi    $session = $_SESSION[$conf['title']]['auth'];
134132bdbfeSandi
135132bdbfeSandi    if($user && $pass){
136132bdbfeSandi      // we got a cookie - see if we can trust it
137132bdbfeSandi      if(isset($session) &&
138132bdbfeSandi        ($session['user'] == $user) &&
139132bdbfeSandi        ($session['pass'] == $pass) &&  //still crypted
140132bdbfeSandi        ($session['buid'] == auth_browseruid()) ){
141132bdbfeSandi        // he has session, cookie and browser right - let him in
142132bdbfeSandi        $_SERVER['REMOTE_USER'] = $user;
143132bdbfeSandi        $USERINFO = $session['info']; //FIXME move all references to session
144132bdbfeSandi        return true;
145132bdbfeSandi      }
146132bdbfeSandi      // no we don't trust it yet - recheck pass
147132bdbfeSandi      $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt());
148132bdbfeSandi      return auth_login($user,$pass,$sticky);
149132bdbfeSandi    }
150132bdbfeSandi  }
151f3f0262cSandi  //just to be sure
152f3f0262cSandi  auth_logoff();
153132bdbfeSandi  return false;
154f3f0262cSandi}
155132bdbfeSandi
156132bdbfeSandi/**
157136ce040Sandi * Builds a pseudo UID from browser and IP data
158132bdbfeSandi *
159132bdbfeSandi * This is neither unique nor unfakable - still it adds some
160136ce040Sandi * security. Using the first part of the IP makes sure
161136ce040Sandi * proxy farms like AOLs are stil okay.
16215fae107Sandi *
16315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
16415fae107Sandi *
16515fae107Sandi * @return  string  a MD5 sum of various browser headers
166132bdbfeSandi */
167132bdbfeSandifunction auth_browseruid(){
168132bdbfeSandi  $uid  = '';
169132bdbfeSandi  $uid .= $_SERVER['HTTP_USER_AGENT'];
170132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_ENCODING'];
171132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
172132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_CHARSET'];
173136ce040Sandi  $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.'));
174132bdbfeSandi  return md5($uid);
175132bdbfeSandi}
176132bdbfeSandi
177132bdbfeSandi/**
178132bdbfeSandi * Creates a random key to encrypt the password in cookies
17915fae107Sandi *
18015fae107Sandi * This function tries to read the password for encrypting
18198407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt'
18215fae107Sandi * if no such file is found a random key is created and
18315fae107Sandi * and stored in this file.
18415fae107Sandi *
18515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
18615fae107Sandi *
18715fae107Sandi * @return  string
188132bdbfeSandi */
189132bdbfeSandifunction auth_cookiesalt(){
190132bdbfeSandi  global $conf;
19198407a7aSandi  $file = $conf['metadir'].'/_htcookiesalt';
192132bdbfeSandi  $salt = io_readFile($file);
193132bdbfeSandi  if(empty($salt)){
194132bdbfeSandi    $salt = uniqid(rand(),true);
195132bdbfeSandi    io_saveFile($file,$salt);
196132bdbfeSandi  }
197132bdbfeSandi  return $salt;
198f3f0262cSandi}
199f3f0262cSandi
200f3f0262cSandi/**
201f3f0262cSandi * This clears all authenticationdata and thus log the user
202f3f0262cSandi * off
20315fae107Sandi *
20415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
205f3f0262cSandi */
206f3f0262cSandifunction auth_logoff(){
207f3f0262cSandi  global $conf;
208f3f0262cSandi  global $USERINFO;
2098b06d178Schris  global $INFO, $ID;
210*5298a619SAndreas Gohr  global $auth;
21137065e65Sandi
21237065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['user']))
213132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['user']);
21437065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['pass']))
215132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['pass']);
21637065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['info']))
217132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['info']);
21837065e65Sandi  if(isset($_SERVER['REMOTE_USER']))
219f3f0262cSandi    unset($_SERVER['REMOTE_USER']);
220132bdbfeSandi  $USERINFO=null; //FIXME
2211e866646Sandi  setcookie(DOKU_COOKIE,'',time()-600000,'/');
222*5298a619SAndreas Gohr
223*5298a619SAndreas Gohr  if($auth && $auth->canDo('logoff')){
224*5298a619SAndreas Gohr    $auth->logOff();
225*5298a619SAndreas Gohr  }
226f3f0262cSandi}
227f3f0262cSandi
228f3f0262cSandi/**
22915fae107Sandi * Convinience function for auth_aclcheck()
23015fae107Sandi *
23115fae107Sandi * This checks the permissions for the current user
23215fae107Sandi *
23315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
23415fae107Sandi *
23515fae107Sandi * @param  string  $id  page ID
23615fae107Sandi * @return int          permission level
237f3f0262cSandi */
238f3f0262cSandifunction auth_quickaclcheck($id){
239f3f0262cSandi  global $conf;
240f3f0262cSandi  global $USERINFO;
241f3f0262cSandi  # if no ACL is used always return upload rights
242f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
243f3f0262cSandi  return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']);
244f3f0262cSandi}
245f3f0262cSandi
246f3f0262cSandi/**
247f3f0262cSandi * Returns the maximum rights a user has for
248f3f0262cSandi * the given ID or its namespace
24915fae107Sandi *
25015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
25115fae107Sandi *
25215fae107Sandi * @param  string  $id     page ID
25315fae107Sandi * @param  string  $user   Username
25415fae107Sandi * @param  array   $groups Array of groups the user is in
25515fae107Sandi * @return int             permission level
256f3f0262cSandi */
257f3f0262cSandifunction auth_aclcheck($id,$user,$groups){
258f3f0262cSandi  global $conf;
259f3f0262cSandi  global $AUTH_ACL;
260f3f0262cSandi
261f3f0262cSandi  # if no ACL is used always return upload rights
262f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
263f3f0262cSandi
26410a76f6fSfrank  //if user is superuser return 255 (acl_admin)
26510a76f6fSfrank  if($conf['superuser'] == $user) { return AUTH_ADMIN; }
26610a76f6fSfrank
267074cf26bSandi  //make sure groups is an array
268074cf26bSandi  if(!is_array($groups)) $groups = array();
269074cf26bSandi
27010a76f6fSfrank  //prepend groups with @
2712cd2db38Sandi  $cnt = count($groups);
2722cd2db38Sandi  for($i=0; $i<$cnt; $i++){
27310a76f6fSfrank    $groups[$i] = '@'.$groups[$i];
27410a76f6fSfrank  }
27510a76f6fSfrank  //if user is in superuser group return 255 (acl_admin)
27610a76f6fSfrank  if(in_array($conf['superuser'], $groups)) { return AUTH_ADMIN; }
27710a76f6fSfrank
278f3f0262cSandi  $ns    = getNS($id);
279f3f0262cSandi  $perm  = -1;
280f3f0262cSandi
281f3f0262cSandi  if($user){
282f3f0262cSandi    //add ALL group
283f3f0262cSandi    $groups[] = '@ALL';
284f3f0262cSandi    //add User
285f3f0262cSandi    $groups[] = $user;
286f3f0262cSandi    //build regexp
287f3f0262cSandi    $regexp   = join('|',$groups);
288f3f0262cSandi  }else{
289f3f0262cSandi    $regexp = '@ALL';
290f3f0262cSandi  }
291f3f0262cSandi
292f3f0262cSandi  //check exact match first
29342905504SAndreas Gohr  $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL);
294f3f0262cSandi  if(count($matches)){
295f3f0262cSandi    foreach($matches as $match){
296f3f0262cSandi      $match = preg_replace('/#.*$/','',$match); //ignore comments
297f3f0262cSandi      $acl   = preg_split('/\s+/',$match);
2988ef6b7caSandi      if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
299f3f0262cSandi      if($acl[2] > $perm){
300f3f0262cSandi        $perm = $acl[2];
301f3f0262cSandi      }
302f3f0262cSandi    }
303f3f0262cSandi    if($perm > -1){
304f3f0262cSandi      //we had a match - return it
305f3f0262cSandi      return $perm;
306f3f0262cSandi    }
307f3f0262cSandi  }
308f3f0262cSandi
309f3f0262cSandi  //still here? do the namespace checks
310f3f0262cSandi  if($ns){
311f3f0262cSandi    $path = $ns.':\*';
312f3f0262cSandi  }else{
313f3f0262cSandi    $path = '\*'; //root document
314f3f0262cSandi  }
315f3f0262cSandi
316f3f0262cSandi  do{
317f3f0262cSandi    $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL);
318f3f0262cSandi    if(count($matches)){
319f3f0262cSandi      foreach($matches as $match){
320f3f0262cSandi        $match = preg_replace('/#.*$/','',$match); //ignore comments
321f3f0262cSandi        $acl   = preg_split('/\s+/',$match);
3228ef6b7caSandi        if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
323f3f0262cSandi        if($acl[2] > $perm){
324f3f0262cSandi          $perm = $acl[2];
325f3f0262cSandi        }
326f3f0262cSandi      }
327f3f0262cSandi      //we had a match - return it
328f3f0262cSandi      return $perm;
329f3f0262cSandi    }
330f3f0262cSandi
331f3f0262cSandi    //get next higher namespace
332f3f0262cSandi    $ns   = getNS($ns);
333f3f0262cSandi
334f3f0262cSandi    if($path != '\*'){
335f3f0262cSandi      $path = $ns.':\*';
336f3f0262cSandi      if($path == ':\*') $path = '\*';
337f3f0262cSandi    }else{
338f3f0262cSandi      //we did this already
339f3f0262cSandi      //looks like there is something wrong with the ACL
340f3f0262cSandi      //break here
341d5ce66f6SAndreas Gohr      msg('No ACL setup yet! Denying access to everyone.');
342d5ce66f6SAndreas Gohr      return AUTH_NONE;
343f3f0262cSandi    }
344f3f0262cSandi  }while(1); //this should never loop endless
34552a5af8dSandi
34652a5af8dSandi  //still here? return no permissions
34752a5af8dSandi  return AUTH_NONE;
348f3f0262cSandi}
349f3f0262cSandi
350f3f0262cSandi/**
351f3f0262cSandi * Create a pronouncable password
352f3f0262cSandi *
35315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
35415fae107Sandi * @link    http://www.phpbuilder.com/annotate/message.php3?id=1014451
35515fae107Sandi *
35615fae107Sandi * @return string  pronouncable password
357f3f0262cSandi */
358f3f0262cSandifunction auth_pwgen(){
359f3f0262cSandi  $pw = '';
360f3f0262cSandi  $c  = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
361f3f0262cSandi  $v  = 'aeiou';              //vowels
362f3f0262cSandi  $a  = $c.$v;                //both
363f3f0262cSandi
364f3f0262cSandi  //use two syllables...
365f3f0262cSandi  for($i=0;$i < 2; $i++){
366f3f0262cSandi    $pw .= $c[rand(0, strlen($c)-1)];
367f3f0262cSandi    $pw .= $v[rand(0, strlen($v)-1)];
368f3f0262cSandi    $pw .= $a[rand(0, strlen($a)-1)];
369f3f0262cSandi  }
370f3f0262cSandi  //... and add a nice number
371f3f0262cSandi  $pw .= rand(10,99);
372f3f0262cSandi
373f3f0262cSandi  return $pw;
374f3f0262cSandi}
375f3f0262cSandi
376f3f0262cSandi/**
377f3f0262cSandi * Sends a password to the given user
378f3f0262cSandi *
37915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
38015fae107Sandi *
38115fae107Sandi * @return bool  true on success
382f3f0262cSandi */
383f3f0262cSandifunction auth_sendPassword($user,$password){
384f3f0262cSandi  global $conf;
385f3f0262cSandi  global $lang;
386cd52f92dSchris	global $auth;
387cd52f92dSchris
388f3f0262cSandi  $hdrs  = '';
389cd52f92dSchris  $userinfo = $auth->getUserData($user);
390f3f0262cSandi
39187ddda95Sandi  if(!$userinfo['mail']) return false;
392f3f0262cSandi
393f3f0262cSandi  $text = rawLocale('password');
394ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
39587ddda95Sandi  $text = str_replace('@FULLNAME@',$userinfo['name'],$text);
396f3f0262cSandi  $text = str_replace('@LOGIN@',$user,$text);
397f3f0262cSandi  $text = str_replace('@PASSWORD@',$password,$text);
398f3f0262cSandi  $text = str_replace('@TITLE@',$conf['title'],$text);
399f3f0262cSandi
40044f669e9Sandi  return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>',
40144f669e9Sandi                   $lang['regpwmail'],
40244f669e9Sandi                   $text,
40344f669e9Sandi                   $conf['mailfrom']);
404f3f0262cSandi}
405f3f0262cSandi
406f3f0262cSandi/**
40715fae107Sandi * Register a new user
408f3f0262cSandi *
40915fae107Sandi * This registers a new user - Data is read directly from $_POST
41015fae107Sandi *
41115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
41215fae107Sandi *
41315fae107Sandi * @return bool  true on success, false on any error
414f3f0262cSandi */
415f3f0262cSandifunction register(){
416f3f0262cSandi  global $lang;
417eb5d07e4Sjan  global $conf;
418cd52f92dSchris	global $auth;
419f3f0262cSandi
420f3f0262cSandi  if(!$_POST['save']) return false;
42182fd59b6SAndreas Gohr	if(!$auth->canDo('addUser')) return false;
422640145a5Sandi
423f3f0262cSandi  //clean username
424f3f0262cSandi  $_POST['login'] = preg_replace('/.*:/','',$_POST['login']);
425f3f0262cSandi  $_POST['login'] = cleanID($_POST['login']);
426f3f0262cSandi  //clean fullname and email
427f3f0262cSandi  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
428f3f0262cSandi  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
429f3f0262cSandi
430f3f0262cSandi  if( empty($_POST['login']) ||
431f3f0262cSandi      empty($_POST['fullname']) ||
432f3f0262cSandi      empty($_POST['email']) ){
433f3f0262cSandi    msg($lang['regmissing'],-1);
434f3f0262cSandi    return false;
435f3f0262cSandi  }
436f3f0262cSandi
437cab2716aSmatthias.grimm  if ($conf['autopasswd']) {
438cab2716aSmatthias.grimm    $pass = auth_pwgen();                // automatically generate password
439cab2716aSmatthias.grimm  } elseif (empty($_POST['pass']) ||
440cab2716aSmatthias.grimm            empty($_POST['passchk'])) {
441bf12ec81Sjan    msg($lang['regmissing'], -1);        // complain about missing passwords
442cab2716aSmatthias.grimm    return false;
443cab2716aSmatthias.grimm  } elseif ($_POST['pass'] != $_POST['passchk']) {
444bf12ec81Sjan    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
445cab2716aSmatthias.grimm    return false;
446cab2716aSmatthias.grimm  } else {
447cab2716aSmatthias.grimm    $pass = $_POST['pass'];              // accept checked and valid password
448cab2716aSmatthias.grimm  }
449cab2716aSmatthias.grimm
450f3f0262cSandi  //check mail
45144f669e9Sandi  if(!mail_isvalid($_POST['email'])){
452f3f0262cSandi    msg($lang['regbadmail'],-1);
453f3f0262cSandi    return false;
454f3f0262cSandi  }
455f3f0262cSandi
456f3f0262cSandi  //okay try to create the user
457cd52f92dSchris  $pass = $auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email']);
458f3f0262cSandi  if(empty($pass)){
459f3f0262cSandi    msg($lang['reguexists'],-1);
460f3f0262cSandi    return false;
461f3f0262cSandi  }
462f3f0262cSandi
463cab2716aSmatthias.grimm  if (!$conf['autopasswd']) {
464cab2716aSmatthias.grimm    msg($lang['regsuccess2'],1);
465cab2716aSmatthias.grimm    return true;
466cab2716aSmatthias.grimm  }
467cab2716aSmatthias.grimm
468cab2716aSmatthias.grimm  // autogenerated password? then send him the password
469f3f0262cSandi  if (auth_sendPassword($_POST['login'],$pass)){
470f3f0262cSandi    msg($lang['regsuccess'],1);
471f3f0262cSandi    return true;
472f3f0262cSandi  }else{
473f3f0262cSandi    msg($lang['regmailfail'],-1);
474f3f0262cSandi    return false;
475f3f0262cSandi  }
476f3f0262cSandi}
477f3f0262cSandi
47810a76f6fSfrank/**
4798b06d178Schris * Update user profile
4808b06d178Schris *
4818b06d178Schris * @author    Christopher Smith <chris@jalakai.co.uk>
4828b06d178Schris */
4838b06d178Schrisfunction updateprofile() {
4848b06d178Schris  global $conf;
4858b06d178Schris  global $INFO;
4868b06d178Schris  global $lang;
487cd52f92dSchris	global $auth;
4888b06d178Schris
4898b06d178Schris  if(!$_POST['save']) return false;
4908b06d178Schris
49182fd59b6SAndreas Gohr  // should not be able to get here without Profile being possible...
49282fd59b6SAndreas Gohr  if(!$auth->canDo('Profile')) {
4938b06d178Schris    msg($lang['profna'],-1);
4948b06d178Schris    return false;
4958b06d178Schris  }
4968b06d178Schris
4978b06d178Schris  if ($_POST['newpass'] != $_POST['passchk']) {
4988b06d178Schris    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
4998b06d178Schris    return false;
5008b06d178Schris  }
5018b06d178Schris
5028b06d178Schris  //clean fullname and email
5038b06d178Schris  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
5048b06d178Schris  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
5058b06d178Schris
5068b06d178Schris  if (empty($_POST['fullname']) || empty($_POST['email'])) {
5078b06d178Schris    msg($lang['profnoempty'],-1);
5088b06d178Schris    return false;
5098b06d178Schris  }
5108b06d178Schris
5118b06d178Schris  if (!mail_isvalid($_POST['email'])){
5128b06d178Schris    msg($lang['regbadmail'],-1);
5138b06d178Schris    return false;
5148b06d178Schris  }
5158b06d178Schris
5168b06d178Schris  if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname'];
5178b06d178Schris  if ($_POST['email']    != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email'];
5188b06d178Schris  if (!empty($_POST['newpass']))  $changes['pass'] = $_POST['newpass'];
5198b06d178Schris
5208b06d178Schris  if (!count($changes)) {
5218b06d178Schris    msg($lang['profnochange'], -1);
5228b06d178Schris    return false;
5238b06d178Schris  }
5248b06d178Schris
5258b06d178Schris  if ($conf['profileconfirm']) {
5268b06d178Schris      if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) {
5278b06d178Schris      msg($lang['badlogin'],-1);
5288b06d178Schris      return false;
5298b06d178Schris    }
5308b06d178Schris  }
5318b06d178Schris
532cd52f92dSchris  return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes);
5338b06d178Schris}
5348b06d178Schris
5358b06d178Schris/**
5368b06d178Schris * Send a  new password
5378b06d178Schris *
5388b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info>
5398b06d178Schris * @author Chris Smith <chris@jalakai.co.uk>
5408b06d178Schris *
5418b06d178Schris * @return bool true on success, false on any error
5428b06d178Schris*/
5438b06d178Schrisfunction act_resendpwd(){
5448b06d178Schris    global $lang;
5458b06d178Schris    global $conf;
546cd52f92dSchris		global $auth;
5478b06d178Schris
5488b06d178Schris    if(!$_POST['save']) return false;
5498fd2f03aSAndreas Gohr    if(!$conf['resendpasswd']) return false;
5508b06d178Schris
55182fd59b6SAndreas Gohr    // should not be able to get here without modPass being possible...
55282fd59b6SAndreas Gohr    if(!$auth->canDo('modPass')) {
5538b06d178Schris      msg($lang['resendna'],-1);
5548b06d178Schris      return false;
5558b06d178Schris    }
5568b06d178Schris
5578b06d178Schris    if (empty($_POST['login'])) {
5588b06d178Schris      msg($lang['resendpwdmissing'], -1);
5598b06d178Schris      return false;
5608b06d178Schris    } else {
5618b06d178Schris      $user = $_POST['login'];
5628b06d178Schris    }
5638b06d178Schris
564cd52f92dSchris    $userinfo = $auth->getUserData($user);
5658b06d178Schris    if(!$userinfo['mail']) {
5668b06d178Schris      msg($lang['resendpwdnouser'], -1);
5678b06d178Schris      return false;
5688b06d178Schris    }
5698b06d178Schris
5708b06d178Schris    $pass = auth_pwgen();
571cd52f92dSchris    if (!$auth->modifyUser($user,array('pass' => $pass))) {
5728b06d178Schris      msg('error modifying user data',-1);
5738b06d178Schris      return false;
5748b06d178Schris    }
5758b06d178Schris
5768b06d178Schris    if (auth_sendPassword($user,$pass)) {
5778b06d178Schris      msg($lang['resendpwdsuccess'],1);
5788b06d178Schris    } else {
5798b06d178Schris      msg($lang['regmailfail'],-1);
5808b06d178Schris    }
5818b06d178Schris    return true;
5828b06d178Schris}
5838b06d178Schris
5848b06d178Schris/**
58510a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid
58610a76f6fSfrank *
58710a76f6fSfrank * May not be completly RFC conform!
58810a76f6fSfrank *
58910a76f6fSfrank * @link    http://www.webmasterworld.com/forum88/135.htm
59010a76f6fSfrank *
59110a76f6fSfrank * @param   string $email the address to check
59210a76f6fSfrank * @return  bool          true if address is valid
59310a76f6fSfrank */
59410a76f6fSfrankfunction isvalidemail($email){
59510a76f6fSfrank  return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
59610a76f6fSfrank}
59710a76f6fSfrank
598b0855b11Sandi/**
599b0855b11Sandi * Encrypts a password using the given method and salt
600b0855b11Sandi *
601b0855b11Sandi * If the selected method needs a salt and none was given, a random one
602b0855b11Sandi * is chosen.
603b0855b11Sandi *
604b0855b11Sandi * The following methods are understood:
605b0855b11Sandi *
606b0855b11Sandi *   smd5  - Salted MD5 hashing
607b0855b11Sandi *   md5   - Simple MD5 hashing
608b0855b11Sandi *   sha1  - SHA1 hashing
609b0855b11Sandi *   ssha  - Salted SHA1 hashing
610d7be6245Sandi *   crypt - Unix crypt
611d7be6245Sandi *   mysql - MySQL password (old method)
612d7be6245Sandi *   my411 - MySQL 4.1.1 password
613b0855b11Sandi *
614b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
615b0855b11Sandi * @return  string  The crypted password
616b0855b11Sandi */
617b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){
618b0855b11Sandi  global $conf;
619b0855b11Sandi  if(empty($method)) $method = $conf['passcrypt'];
62010a76f6fSfrank
621b0855b11Sandi  //prepare a salt
622b0855b11Sandi  if(empty($salt)) $salt = md5(uniqid(rand(), true));
623b0855b11Sandi
624b0855b11Sandi  switch(strtolower($method)){
625b0855b11Sandi    case 'smd5':
626b0855b11Sandi        return crypt($clear,'$1$'.substr($salt,0,8).'$');
627b0855b11Sandi    case 'md5':
628b0855b11Sandi      return md5($clear);
629b0855b11Sandi    case 'sha1':
630b0855b11Sandi      return sha1($clear);
631b0855b11Sandi    case 'ssha':
632b0855b11Sandi      $salt=substr($salt,0,4);
633d6e54e02Smatthiasgrimm      return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt);
634b0855b11Sandi    case 'crypt':
635b0855b11Sandi      return crypt($clear,substr($salt,0,2));
636d7be6245Sandi    case 'mysql':
637d7be6245Sandi      //from http://www.php.net/mysql comment by <soren at byu dot edu>
638d7be6245Sandi      $nr=0x50305735;
639d7be6245Sandi      $nr2=0x12345671;
640d7be6245Sandi      $add=7;
641d7be6245Sandi      $charArr = preg_split("//", $clear);
642d7be6245Sandi      foreach ($charArr as $char) {
643d7be6245Sandi        if (($char == '') || ($char == ' ') || ($char == '\t')) continue;
644d7be6245Sandi        $charVal = ord($char);
645d7be6245Sandi        $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8);
646d7be6245Sandi        $nr2 += ($nr2 << 8) ^ $nr;
647d7be6245Sandi        $add += $charVal;
648d7be6245Sandi      }
649d7be6245Sandi      return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff));
650d7be6245Sandi    case 'my411':
651d7be6245Sandi      return '*'.sha1(pack("H*", sha1($clear)));
652b0855b11Sandi    default:
653b0855b11Sandi      msg("Unsupported crypt method $method",-1);
654b0855b11Sandi  }
655b0855b11Sandi}
656b0855b11Sandi
657b0855b11Sandi/**
658b0855b11Sandi * Verifies a cleartext password against a crypted hash
659b0855b11Sandi *
660b0855b11Sandi * The method and salt used for the crypted hash is determined automatically
661b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs
662b0855b11Sandi * match true is is returned else false
663b0855b11Sandi *
664b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
665b0855b11Sandi * @return  bool
666b0855b11Sandi */
667b0855b11Sandifunction auth_verifyPassword($clear,$crypt){
668b0855b11Sandi  $method='';
669b0855b11Sandi  $salt='';
670b0855b11Sandi
671b0855b11Sandi  //determine the used method and salt
672d7be6245Sandi  $len = strlen($crypt);
673b0855b11Sandi  if(substr($crypt,0,3) == '$1$'){
674b0855b11Sandi    $method = 'smd5';
675b0855b11Sandi    $salt   = substr($crypt,3,8);
676b0855b11Sandi  }elseif(substr($crypt,0,6) == '{SSHA}'){
677b0855b11Sandi    $method = 'ssha';
678b0855b11Sandi    $salt   = substr(base64_decode(substr($crypt, 6)),20);
679d7be6245Sandi  }elseif($len == 32){
680b0855b11Sandi    $method = 'md5';
681d7be6245Sandi  }elseif($len == 40){
682b0855b11Sandi    $method = 'sha1';
683d7be6245Sandi  }elseif($len == 16){
684d7be6245Sandi    $method = 'mysql';
685d7be6245Sandi  }elseif($len == 41 && $crypt[0] == '*'){
686d7be6245Sandi    $method = 'my411';
687b0855b11Sandi  }else{
688b0855b11Sandi    $method = 'crypt';
689b0855b11Sandi    $salt   = substr($crypt,0,2);
690b0855b11Sandi  }
691b0855b11Sandi
692b0855b11Sandi  //crypt and compare
693b0855b11Sandi  if(auth_cryptPassword($clear,$method,$salt) === $crypt){
694b0855b11Sandi    return true;
695b0855b11Sandi  }
696b0855b11Sandi  return false;
697b0855b11Sandi}
698340756e4Sandi
699340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
700