xref: /dokuwiki/inc/auth.php (revision 8fd2f03ac0a6725c8eea9c5495c643ee64d7bb77)
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?
54cd52f92dSchris    if(!is_null($auth) && $auth->canDo('trustExternal')){
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;
21037065e65Sandi
21137065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['user']))
212132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['user']);
21337065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['pass']))
214132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['pass']);
21537065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['info']))
216132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['info']);
21737065e65Sandi  if(isset($_SERVER['REMOTE_USER']))
218f3f0262cSandi    unset($_SERVER['REMOTE_USER']);
219132bdbfeSandi  $USERINFO=null; //FIXME
2201e866646Sandi  setcookie(DOKU_COOKIE,'',time()-600000,'/');
221f3f0262cSandi}
222f3f0262cSandi
223f3f0262cSandi/**
22415fae107Sandi * Convinience function for auth_aclcheck()
22515fae107Sandi *
22615fae107Sandi * This checks the permissions for the current user
22715fae107Sandi *
22815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
22915fae107Sandi *
23015fae107Sandi * @param  string  $id  page ID
23115fae107Sandi * @return int          permission level
232f3f0262cSandi */
233f3f0262cSandifunction auth_quickaclcheck($id){
234f3f0262cSandi  global $conf;
235f3f0262cSandi  global $USERINFO;
236f3f0262cSandi  # if no ACL is used always return upload rights
237f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
238f3f0262cSandi  return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']);
239f3f0262cSandi}
240f3f0262cSandi
241f3f0262cSandi/**
242f3f0262cSandi * Returns the maximum rights a user has for
243f3f0262cSandi * the given ID or its namespace
24415fae107Sandi *
24515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
24615fae107Sandi *
24715fae107Sandi * @param  string  $id     page ID
24815fae107Sandi * @param  string  $user   Username
24915fae107Sandi * @param  array   $groups Array of groups the user is in
25015fae107Sandi * @return int             permission level
251f3f0262cSandi */
252f3f0262cSandifunction auth_aclcheck($id,$user,$groups){
253f3f0262cSandi  global $conf;
254f3f0262cSandi  global $AUTH_ACL;
255f3f0262cSandi
256f3f0262cSandi  # if no ACL is used always return upload rights
257f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
258f3f0262cSandi
25910a76f6fSfrank  //if user is superuser return 255 (acl_admin)
26010a76f6fSfrank  if($conf['superuser'] == $user) { return AUTH_ADMIN; }
26110a76f6fSfrank
262074cf26bSandi  //make sure groups is an array
263074cf26bSandi  if(!is_array($groups)) $groups = array();
264074cf26bSandi
26510a76f6fSfrank  //prepend groups with @
2662cd2db38Sandi  $cnt = count($groups);
2672cd2db38Sandi  for($i=0; $i<$cnt; $i++){
26810a76f6fSfrank    $groups[$i] = '@'.$groups[$i];
26910a76f6fSfrank  }
27010a76f6fSfrank  //if user is in superuser group return 255 (acl_admin)
27110a76f6fSfrank  if(in_array($conf['superuser'], $groups)) { return AUTH_ADMIN; }
27210a76f6fSfrank
273f3f0262cSandi  $ns    = getNS($id);
274f3f0262cSandi  $perm  = -1;
275f3f0262cSandi
276f3f0262cSandi  if($user){
277f3f0262cSandi    //add ALL group
278f3f0262cSandi    $groups[] = '@ALL';
279f3f0262cSandi    //add User
280f3f0262cSandi    $groups[] = $user;
281f3f0262cSandi    //build regexp
282f3f0262cSandi    $regexp   = join('|',$groups);
283f3f0262cSandi  }else{
284f3f0262cSandi    $regexp = '@ALL';
285f3f0262cSandi  }
286f3f0262cSandi
287f3f0262cSandi  //check exact match first
28842905504SAndreas Gohr  $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL);
289f3f0262cSandi  if(count($matches)){
290f3f0262cSandi    foreach($matches as $match){
291f3f0262cSandi      $match = preg_replace('/#.*$/','',$match); //ignore comments
292f3f0262cSandi      $acl   = preg_split('/\s+/',$match);
2938ef6b7caSandi      if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
294f3f0262cSandi      if($acl[2] > $perm){
295f3f0262cSandi        $perm = $acl[2];
296f3f0262cSandi      }
297f3f0262cSandi    }
298f3f0262cSandi    if($perm > -1){
299f3f0262cSandi      //we had a match - return it
300f3f0262cSandi      return $perm;
301f3f0262cSandi    }
302f3f0262cSandi  }
303f3f0262cSandi
304f3f0262cSandi  //still here? do the namespace checks
305f3f0262cSandi  if($ns){
306f3f0262cSandi    $path = $ns.':\*';
307f3f0262cSandi  }else{
308f3f0262cSandi    $path = '\*'; //root document
309f3f0262cSandi  }
310f3f0262cSandi
311f3f0262cSandi  do{
312f3f0262cSandi    $matches = preg_grep('/^'.$path.'\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      //we had a match - return it
323f3f0262cSandi      return $perm;
324f3f0262cSandi    }
325f3f0262cSandi
326f3f0262cSandi    //get next higher namespace
327f3f0262cSandi    $ns   = getNS($ns);
328f3f0262cSandi
329f3f0262cSandi    if($path != '\*'){
330f3f0262cSandi      $path = $ns.':\*';
331f3f0262cSandi      if($path == ':\*') $path = '\*';
332f3f0262cSandi    }else{
333f3f0262cSandi      //we did this already
334f3f0262cSandi      //looks like there is something wrong with the ACL
335f3f0262cSandi      //break here
336d5ce66f6SAndreas Gohr      msg('No ACL setup yet! Denying access to everyone.');
337d5ce66f6SAndreas Gohr      return AUTH_NONE;
338f3f0262cSandi    }
339f3f0262cSandi  }while(1); //this should never loop endless
34052a5af8dSandi
34152a5af8dSandi  //still here? return no permissions
34252a5af8dSandi  return AUTH_NONE;
343f3f0262cSandi}
344f3f0262cSandi
345f3f0262cSandi/**
346f3f0262cSandi * Create a pronouncable password
347f3f0262cSandi *
34815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
34915fae107Sandi * @link    http://www.phpbuilder.com/annotate/message.php3?id=1014451
35015fae107Sandi *
35115fae107Sandi * @return string  pronouncable password
352f3f0262cSandi */
353f3f0262cSandifunction auth_pwgen(){
354f3f0262cSandi  $pw = '';
355f3f0262cSandi  $c  = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
356f3f0262cSandi  $v  = 'aeiou';              //vowels
357f3f0262cSandi  $a  = $c.$v;                //both
358f3f0262cSandi
359f3f0262cSandi  //use two syllables...
360f3f0262cSandi  for($i=0;$i < 2; $i++){
361f3f0262cSandi    $pw .= $c[rand(0, strlen($c)-1)];
362f3f0262cSandi    $pw .= $v[rand(0, strlen($v)-1)];
363f3f0262cSandi    $pw .= $a[rand(0, strlen($a)-1)];
364f3f0262cSandi  }
365f3f0262cSandi  //... and add a nice number
366f3f0262cSandi  $pw .= rand(10,99);
367f3f0262cSandi
368f3f0262cSandi  return $pw;
369f3f0262cSandi}
370f3f0262cSandi
371f3f0262cSandi/**
372f3f0262cSandi * Sends a password to the given user
373f3f0262cSandi *
37415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
37515fae107Sandi *
37615fae107Sandi * @return bool  true on success
377f3f0262cSandi */
378f3f0262cSandifunction auth_sendPassword($user,$password){
379f3f0262cSandi  global $conf;
380f3f0262cSandi  global $lang;
381cd52f92dSchris	global $auth;
382cd52f92dSchris
383f3f0262cSandi  $hdrs  = '';
384cd52f92dSchris  $userinfo = $auth->getUserData($user);
385f3f0262cSandi
38687ddda95Sandi  if(!$userinfo['mail']) return false;
387f3f0262cSandi
388f3f0262cSandi  $text = rawLocale('password');
389ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
39087ddda95Sandi  $text = str_replace('@FULLNAME@',$userinfo['name'],$text);
391f3f0262cSandi  $text = str_replace('@LOGIN@',$user,$text);
392f3f0262cSandi  $text = str_replace('@PASSWORD@',$password,$text);
393f3f0262cSandi  $text = str_replace('@TITLE@',$conf['title'],$text);
394f3f0262cSandi
39544f669e9Sandi  return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>',
39644f669e9Sandi                   $lang['regpwmail'],
39744f669e9Sandi                   $text,
39844f669e9Sandi                   $conf['mailfrom']);
399f3f0262cSandi}
400f3f0262cSandi
401f3f0262cSandi/**
40215fae107Sandi * Register a new user
403f3f0262cSandi *
40415fae107Sandi * This registers a new user - Data is read directly from $_POST
40515fae107Sandi *
40615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
40715fae107Sandi *
40815fae107Sandi * @return bool  true on success, false on any error
409f3f0262cSandi */
410f3f0262cSandifunction register(){
411f3f0262cSandi  global $lang;
412eb5d07e4Sjan  global $conf;
413cd52f92dSchris	global $auth;
414f3f0262cSandi
415f3f0262cSandi  if(!$_POST['save']) return false;
416cd52f92dSchris	if(!$auth->canDo('createUser')) return false;
417640145a5Sandi
418f3f0262cSandi  //clean username
419f3f0262cSandi  $_POST['login'] = preg_replace('/.*:/','',$_POST['login']);
420f3f0262cSandi  $_POST['login'] = cleanID($_POST['login']);
421f3f0262cSandi  //clean fullname and email
422f3f0262cSandi  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
423f3f0262cSandi  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
424f3f0262cSandi
425f3f0262cSandi  if( empty($_POST['login']) ||
426f3f0262cSandi      empty($_POST['fullname']) ||
427f3f0262cSandi      empty($_POST['email']) ){
428f3f0262cSandi    msg($lang['regmissing'],-1);
429f3f0262cSandi    return false;
430f3f0262cSandi  }
431f3f0262cSandi
432cab2716aSmatthias.grimm  if ($conf['autopasswd']) {
433cab2716aSmatthias.grimm    $pass = auth_pwgen();                // automatically generate password
434cab2716aSmatthias.grimm  } elseif (empty($_POST['pass']) ||
435cab2716aSmatthias.grimm            empty($_POST['passchk'])) {
436bf12ec81Sjan    msg($lang['regmissing'], -1);        // complain about missing passwords
437cab2716aSmatthias.grimm    return false;
438cab2716aSmatthias.grimm  } elseif ($_POST['pass'] != $_POST['passchk']) {
439bf12ec81Sjan    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
440cab2716aSmatthias.grimm    return false;
441cab2716aSmatthias.grimm  } else {
442cab2716aSmatthias.grimm    $pass = $_POST['pass'];              // accept checked and valid password
443cab2716aSmatthias.grimm  }
444cab2716aSmatthias.grimm
445f3f0262cSandi  //check mail
44644f669e9Sandi  if(!mail_isvalid($_POST['email'])){
447f3f0262cSandi    msg($lang['regbadmail'],-1);
448f3f0262cSandi    return false;
449f3f0262cSandi  }
450f3f0262cSandi
451f3f0262cSandi  //okay try to create the user
452cd52f92dSchris  $pass = $auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email']);
453f3f0262cSandi  if(empty($pass)){
454f3f0262cSandi    msg($lang['reguexists'],-1);
455f3f0262cSandi    return false;
456f3f0262cSandi  }
457f3f0262cSandi
458cab2716aSmatthias.grimm  if (!$conf['autopasswd']) {
459cab2716aSmatthias.grimm    msg($lang['regsuccess2'],1);
460cab2716aSmatthias.grimm    return true;
461cab2716aSmatthias.grimm  }
462cab2716aSmatthias.grimm
463cab2716aSmatthias.grimm  // autogenerated password? then send him the password
464f3f0262cSandi  if (auth_sendPassword($_POST['login'],$pass)){
465f3f0262cSandi    msg($lang['regsuccess'],1);
466f3f0262cSandi    return true;
467f3f0262cSandi  }else{
468f3f0262cSandi    msg($lang['regmailfail'],-1);
469f3f0262cSandi    return false;
470f3f0262cSandi  }
471f3f0262cSandi}
472f3f0262cSandi
47310a76f6fSfrank/**
4748b06d178Schris * Update user profile
4758b06d178Schris *
4768b06d178Schris * @author    Christopher Smith <chris@jalakai.co.uk>
4778b06d178Schris */
4788b06d178Schrisfunction updateprofile() {
4798b06d178Schris  global $conf;
4808b06d178Schris  global $INFO;
4818b06d178Schris  global $lang;
482cd52f92dSchris	global $auth;
4838b06d178Schris
4848b06d178Schris  if(!$_POST['save']) return false;
4858b06d178Schris
4868b06d178Schris  // should not be able to get here without modifyUser being possible...
487cd52f92dSchris  if(!$auth->canDo('modifyUser')) {
4888b06d178Schris    msg($lang['profna'],-1);
4898b06d178Schris    return false;
4908b06d178Schris  }
4918b06d178Schris
4928b06d178Schris  if ($_POST['newpass'] != $_POST['passchk']) {
4938b06d178Schris    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
4948b06d178Schris    return false;
4958b06d178Schris  }
4968b06d178Schris
4978b06d178Schris  //clean fullname and email
4988b06d178Schris  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
4998b06d178Schris  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
5008b06d178Schris
5018b06d178Schris  if (empty($_POST['fullname']) || empty($_POST['email'])) {
5028b06d178Schris    msg($lang['profnoempty'],-1);
5038b06d178Schris    return false;
5048b06d178Schris  }
5058b06d178Schris
5068b06d178Schris  if (!mail_isvalid($_POST['email'])){
5078b06d178Schris    msg($lang['regbadmail'],-1);
5088b06d178Schris    return false;
5098b06d178Schris  }
5108b06d178Schris
5118b06d178Schris  if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname'];
5128b06d178Schris  if ($_POST['email']    != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email'];
5138b06d178Schris  if (!empty($_POST['newpass']))  $changes['pass'] = $_POST['newpass'];
5148b06d178Schris
5158b06d178Schris  if (!count($changes)) {
5168b06d178Schris    msg($lang['profnochange'], -1);
5178b06d178Schris    return false;
5188b06d178Schris  }
5198b06d178Schris
5208b06d178Schris  if ($conf['profileconfirm']) {
5218b06d178Schris      if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) {
5228b06d178Schris      msg($lang['badlogin'],-1);
5238b06d178Schris      return false;
5248b06d178Schris    }
5258b06d178Schris  }
5268b06d178Schris
527cd52f92dSchris  return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes);
5288b06d178Schris}
5298b06d178Schris
5308b06d178Schris/**
5318b06d178Schris * Send a  new password
5328b06d178Schris *
5338b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info>
5348b06d178Schris * @author Chris Smith <chris@jalakai.co.uk>
5358b06d178Schris *
5368b06d178Schris * @return bool true on success, false on any error
5378b06d178Schris*/
5388b06d178Schrisfunction act_resendpwd(){
5398b06d178Schris    global $lang;
5408b06d178Schris    global $conf;
541cd52f92dSchris		global $auth;
5428b06d178Schris
5438b06d178Schris    if(!$_POST['save']) return false;
544*8fd2f03aSAndreas Gohr    if(!$conf['resendpasswd']) return false;
5458b06d178Schris
5468b06d178Schris    // should not be able to get here without modifyUser being possible...
547cd52f92dSchris	if(!$auth->canDo('modifyUser')) {
5488b06d178Schris      msg($lang['resendna'],-1);
5498b06d178Schris      return false;
5508b06d178Schris	}
5518b06d178Schris
5528b06d178Schris    if (empty($_POST['login'])) {
5538b06d178Schris      msg($lang['resendpwdmissing'], -1);
5548b06d178Schris      return false;
5558b06d178Schris    } else {
5568b06d178Schris      $user = $_POST['login'];
5578b06d178Schris    }
5588b06d178Schris
559cd52f92dSchris    $userinfo = $auth->getUserData($user);
5608b06d178Schris    if(!$userinfo['mail']) {
5618b06d178Schris      msg($lang['resendpwdnouser'], -1);
5628b06d178Schris      return false;
5638b06d178Schris    }
5648b06d178Schris
5658b06d178Schris    $pass = auth_pwgen();
566cd52f92dSchris    if (!$auth->modifyUser($user,array('pass' => $pass))) {
5678b06d178Schris      msg('error modifying user data',-1);
5688b06d178Schris      return false;
5698b06d178Schris    }
5708b06d178Schris
5718b06d178Schris    if (auth_sendPassword($user,$pass)) {
5728b06d178Schris      msg($lang['resendpwdsuccess'],1);
5738b06d178Schris    } else {
5748b06d178Schris      msg($lang['regmailfail'],-1);
5758b06d178Schris    }
5768b06d178Schris    return true;
5778b06d178Schris}
5788b06d178Schris
5798b06d178Schris/**
58010a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid
58110a76f6fSfrank *
58210a76f6fSfrank * May not be completly RFC conform!
58310a76f6fSfrank *
58410a76f6fSfrank * @link    http://www.webmasterworld.com/forum88/135.htm
58510a76f6fSfrank *
58610a76f6fSfrank * @param   string $email the address to check
58710a76f6fSfrank * @return  bool          true if address is valid
58810a76f6fSfrank */
58910a76f6fSfrankfunction isvalidemail($email){
59010a76f6fSfrank  return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
59110a76f6fSfrank}
59210a76f6fSfrank
593b0855b11Sandi/**
594b0855b11Sandi * Encrypts a password using the given method and salt
595b0855b11Sandi *
596b0855b11Sandi * If the selected method needs a salt and none was given, a random one
597b0855b11Sandi * is chosen.
598b0855b11Sandi *
599b0855b11Sandi * The following methods are understood:
600b0855b11Sandi *
601b0855b11Sandi *   smd5  - Salted MD5 hashing
602b0855b11Sandi *   md5   - Simple MD5 hashing
603b0855b11Sandi *   sha1  - SHA1 hashing
604b0855b11Sandi *   ssha  - Salted SHA1 hashing
605d7be6245Sandi *   crypt - Unix crypt
606d7be6245Sandi *   mysql - MySQL password (old method)
607d7be6245Sandi *   my411 - MySQL 4.1.1 password
608b0855b11Sandi *
609b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
610b0855b11Sandi * @return  string  The crypted password
611b0855b11Sandi */
612b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){
613b0855b11Sandi  global $conf;
614b0855b11Sandi  if(empty($method)) $method = $conf['passcrypt'];
61510a76f6fSfrank
616b0855b11Sandi  //prepare a salt
617b0855b11Sandi  if(empty($salt)) $salt = md5(uniqid(rand(), true));
618b0855b11Sandi
619b0855b11Sandi  switch(strtolower($method)){
620b0855b11Sandi    case 'smd5':
621b0855b11Sandi        return crypt($clear,'$1$'.substr($salt,0,8).'$');
622b0855b11Sandi    case 'md5':
623b0855b11Sandi      return md5($clear);
624b0855b11Sandi    case 'sha1':
625b0855b11Sandi      return sha1($clear);
626b0855b11Sandi    case 'ssha':
627b0855b11Sandi      $salt=substr($salt,0,4);
628d6e54e02Smatthiasgrimm      return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt);
629b0855b11Sandi    case 'crypt':
630b0855b11Sandi      return crypt($clear,substr($salt,0,2));
631d7be6245Sandi    case 'mysql':
632d7be6245Sandi      //from http://www.php.net/mysql comment by <soren at byu dot edu>
633d7be6245Sandi      $nr=0x50305735;
634d7be6245Sandi      $nr2=0x12345671;
635d7be6245Sandi      $add=7;
636d7be6245Sandi      $charArr = preg_split("//", $clear);
637d7be6245Sandi      foreach ($charArr as $char) {
638d7be6245Sandi        if (($char == '') || ($char == ' ') || ($char == '\t')) continue;
639d7be6245Sandi        $charVal = ord($char);
640d7be6245Sandi        $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8);
641d7be6245Sandi        $nr2 += ($nr2 << 8) ^ $nr;
642d7be6245Sandi        $add += $charVal;
643d7be6245Sandi      }
644d7be6245Sandi      return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff));
645d7be6245Sandi    case 'my411':
646d7be6245Sandi      return '*'.sha1(pack("H*", sha1($clear)));
647b0855b11Sandi    default:
648b0855b11Sandi      msg("Unsupported crypt method $method",-1);
649b0855b11Sandi  }
650b0855b11Sandi}
651b0855b11Sandi
652b0855b11Sandi/**
653b0855b11Sandi * Verifies a cleartext password against a crypted hash
654b0855b11Sandi *
655b0855b11Sandi * The method and salt used for the crypted hash is determined automatically
656b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs
657b0855b11Sandi * match true is is returned else false
658b0855b11Sandi *
659b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
660b0855b11Sandi * @return  bool
661b0855b11Sandi */
662b0855b11Sandifunction auth_verifyPassword($clear,$crypt){
663b0855b11Sandi  $method='';
664b0855b11Sandi  $salt='';
665b0855b11Sandi
666b0855b11Sandi  //determine the used method and salt
667d7be6245Sandi  $len = strlen($crypt);
668b0855b11Sandi  if(substr($crypt,0,3) == '$1$'){
669b0855b11Sandi    $method = 'smd5';
670b0855b11Sandi    $salt   = substr($crypt,3,8);
671b0855b11Sandi  }elseif(substr($crypt,0,6) == '{SSHA}'){
672b0855b11Sandi    $method = 'ssha';
673b0855b11Sandi    $salt   = substr(base64_decode(substr($crypt, 6)),20);
674d7be6245Sandi  }elseif($len == 32){
675b0855b11Sandi    $method = 'md5';
676d7be6245Sandi  }elseif($len == 40){
677b0855b11Sandi    $method = 'sha1';
678d7be6245Sandi  }elseif($len == 16){
679d7be6245Sandi    $method = 'mysql';
680d7be6245Sandi  }elseif($len == 41 && $crypt[0] == '*'){
681d7be6245Sandi    $method = 'my411';
682b0855b11Sandi  }else{
683b0855b11Sandi    $method = 'crypt';
684b0855b11Sandi    $salt   = substr($crypt,0,2);
685b0855b11Sandi  }
686b0855b11Sandi
687b0855b11Sandi  //crypt and compare
688b0855b11Sandi  if(auth_cryptPassword($clear,$method,$salt) === $crypt){
689b0855b11Sandi    return true;
690b0855b11Sandi  }
691b0855b11Sandi  return false;
692b0855b11Sandi}
693340756e4Sandi
694340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
695