xref: /dokuwiki/inc/auth.php (revision 1c73890c5027011d808d38c583561abc309e8086)
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');
15*1c73890cSAndreas Gohr
16*1c73890cSAndreas Gohr  if($conf['useacl']){
17ed7b5f09Sandi    require_once(DOKU_INC.'inc/blowfish.php');
18ed7b5f09Sandi    require_once(DOKU_INC.'inc/mail.php');
198b06d178Schris
208b06d178Schris    // load the the backend auth functions and instantiate the auth object
218b06d178Schris    if (@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) {
228b06d178Schris      require_once(DOKU_INC.'inc/auth/basic.class.php');
238b06d178Schris      require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php');
248b06d178Schris
258b06d178Schris      $auth_class = "auth_".$conf['authtype'];
26cd52f92dSchris      if (class_exists($auth_class)) {
278b06d178Schris        $auth = new $auth_class();
28d2dde4ebSMatthias Grimm        if ($auth->success == false) {
29d2dde4ebSMatthias Grimm          unset($auth);
30cd52f92dSchris          msg($lang['authtempfail'], -1);
31cd52f92dSchris
32cd52f92dSchris          // turn acl config setting off for the rest of this page
33cd52f92dSchris          $conf['useacl'] = 0;
34d2dde4ebSMatthias Grimm        }
358b06d178Schris      } else {
36cd52f92dSchris        die($lang['authmodfailed']);
37cd52f92dSchris      }
38cd52f92dSchris    } else {
39cd52f92dSchris      die($lang['authmodfailed']);
408b06d178Schris    }
41*1c73890cSAndreas Gohr  }
42f3f0262cSandi
431e866646Sandi  if (!defined('DOKU_COOKIE')) define('DOKU_COOKIE', 'DW'.md5($conf['title']));
44e65afed4SSameer D. Sahasrabuddhe
4515fae107Sandi  // some ACL level defines
46f3f0262cSandi  define('AUTH_NONE',0);
47f3f0262cSandi  define('AUTH_READ',1);
48f3f0262cSandi  define('AUTH_EDIT',2);
49f3f0262cSandi  define('AUTH_CREATE',4);
50f3f0262cSandi  define('AUTH_UPLOAD',8);
518ef6b7caSandi  define('AUTH_DELETE',16);
5210a76f6fSfrank  define('AUTH_ADMIN',255);
53f3f0262cSandi
54f5cb575dSAndreas Gohr  // do the login either by cookie or provided credentials
55f3f0262cSandi  if($conf['useacl']){
56f5cb575dSAndreas Gohr    // external trust mechanism in place?
5782fd59b6SAndreas Gohr    if(!is_null($auth) && $auth->canDo('external')){
58f5cb575dSAndreas Gohr      $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
59f5cb575dSAndreas Gohr    }else{
60132bdbfeSandi      auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
61f5cb575dSAndreas Gohr    }
62f5cb575dSAndreas Gohr
6315fae107Sandi    //load ACL into a global array
64e7cb32dcSAndreas Gohr    if(is_readable(DOKU_CONF.'acl.auth.php')){
65e7cb32dcSAndreas Gohr      $AUTH_ACL = file(DOKU_CONF.'acl.auth.php');
6611799630Sandi    }else{
6711799630Sandi      $AUTH_ACL = array();
6811799630Sandi    }
69f3f0262cSandi  }
70f3f0262cSandi
71f3f0262cSandi/**
72f3f0262cSandi * This tries to login the user based on the sent auth credentials
73f3f0262cSandi *
74f3f0262cSandi * The authentication works like this: if a username was given
7515fae107Sandi * a new login is assumed and user/password are checked. If they
7615fae107Sandi * are correct the password is encrypted with blowfish and stored
7715fae107Sandi * together with the username in a cookie - the same info is stored
7815fae107Sandi * in the session, too. Additonally a browserID is stored in the
7915fae107Sandi * session.
8015fae107Sandi *
8115fae107Sandi * If no username was given the cookie is checked: if the username,
8215fae107Sandi * crypted password and browserID match between session and cookie
8315fae107Sandi * no further testing is done and the user is accepted
8415fae107Sandi *
8515fae107Sandi * If a cookie was found but no session info was availabe the
86136ce040Sandi * blowfish encrypted password from the cookie is decrypted and
8715fae107Sandi * together with username rechecked by calling this function again.
88f3f0262cSandi *
89f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO
90f3f0262cSandi * are set.
9115fae107Sandi *
9215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
9315fae107Sandi *
9415fae107Sandi * @param   string  $user    Username
9515fae107Sandi * @param   string  $pass    Cleartext Password
9615fae107Sandi * @param   bool    $sticky  Cookie should not expire
9715fae107Sandi * @return  bool             true on successful auth
98f3f0262cSandi*/
99132bdbfeSandifunction auth_login($user,$pass,$sticky=false){
100f3f0262cSandi  global $USERINFO;
101f3f0262cSandi  global $conf;
102f3f0262cSandi  global $lang;
103cd52f92dSchris  global $auth;
104132bdbfeSandi  $sticky ? $sticky = true : $sticky = false; //sanity check
105f3f0262cSandi
106f3f0262cSandi  if(isset($user)){
107132bdbfeSandi    //usual login
108cd52f92dSchris    if ($auth->checkPass($user,$pass)){
109132bdbfeSandi      // make logininfo globally available
110f3f0262cSandi      $_SERVER['REMOTE_USER'] = $user;
111cd52f92dSchris      $USERINFO = $auth->getUserData($user); //FIXME move all references to session
112132bdbfeSandi
113132bdbfeSandi      // set cookie
114132bdbfeSandi      $pass   = PMA_blowfish_encrypt($pass,auth_cookiesalt());
115132bdbfeSandi      $cookie = base64_encode("$user|$sticky|$pass");
116132bdbfeSandi      if($sticky) $time = time()+60*60*24*365; //one year
117e65afed4SSameer D. Sahasrabuddhe      setcookie(DOKU_COOKIE,$cookie,$time,'/');
118132bdbfeSandi
119132bdbfeSandi      // set session
120132bdbfeSandi      $_SESSION[$conf['title']]['auth']['user'] = $user;
121132bdbfeSandi      $_SESSION[$conf['title']]['auth']['pass'] = $pass;
122132bdbfeSandi      $_SESSION[$conf['title']]['auth']['buid'] = auth_browseruid();
123132bdbfeSandi      $_SESSION[$conf['title']]['auth']['info'] = $USERINFO;
124132bdbfeSandi      return true;
125f3f0262cSandi    }else{
126f3f0262cSandi      //invalid credentials - log off
127f3f0262cSandi      msg($lang['badlogin'],-1);
128f3f0262cSandi      auth_logoff();
129132bdbfeSandi      return false;
130f3f0262cSandi    }
131f3f0262cSandi  }else{
132132bdbfeSandi    // read cookie information
133e65afed4SSameer D. Sahasrabuddhe    $cookie = base64_decode($_COOKIE[DOKU_COOKIE]);
134132bdbfeSandi    list($user,$sticky,$pass) = split('\|',$cookie,3);
135132bdbfeSandi    // get session info
136132bdbfeSandi    $session = $_SESSION[$conf['title']]['auth'];
137132bdbfeSandi
138132bdbfeSandi    if($user && $pass){
139132bdbfeSandi      // we got a cookie - see if we can trust it
140132bdbfeSandi      if(isset($session) &&
141132bdbfeSandi        ($session['user'] == $user) &&
142132bdbfeSandi        ($session['pass'] == $pass) &&  //still crypted
143132bdbfeSandi        ($session['buid'] == auth_browseruid()) ){
144132bdbfeSandi        // he has session, cookie and browser right - let him in
145132bdbfeSandi        $_SERVER['REMOTE_USER'] = $user;
146132bdbfeSandi        $USERINFO = $session['info']; //FIXME move all references to session
147132bdbfeSandi        return true;
148132bdbfeSandi      }
149132bdbfeSandi      // no we don't trust it yet - recheck pass
150132bdbfeSandi      $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt());
151132bdbfeSandi      return auth_login($user,$pass,$sticky);
152132bdbfeSandi    }
153132bdbfeSandi  }
154f3f0262cSandi  //just to be sure
155f3f0262cSandi  auth_logoff();
156132bdbfeSandi  return false;
157f3f0262cSandi}
158132bdbfeSandi
159132bdbfeSandi/**
160136ce040Sandi * Builds a pseudo UID from browser and IP data
161132bdbfeSandi *
162132bdbfeSandi * This is neither unique nor unfakable - still it adds some
163136ce040Sandi * security. Using the first part of the IP makes sure
164136ce040Sandi * proxy farms like AOLs are stil okay.
16515fae107Sandi *
16615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
16715fae107Sandi *
16815fae107Sandi * @return  string  a MD5 sum of various browser headers
169132bdbfeSandi */
170132bdbfeSandifunction auth_browseruid(){
171132bdbfeSandi  $uid  = '';
172132bdbfeSandi  $uid .= $_SERVER['HTTP_USER_AGENT'];
173132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_ENCODING'];
174132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
175132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_CHARSET'];
176136ce040Sandi  $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.'));
177132bdbfeSandi  return md5($uid);
178132bdbfeSandi}
179132bdbfeSandi
180132bdbfeSandi/**
181132bdbfeSandi * Creates a random key to encrypt the password in cookies
18215fae107Sandi *
18315fae107Sandi * This function tries to read the password for encrypting
18498407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt'
18515fae107Sandi * if no such file is found a random key is created and
18615fae107Sandi * and stored in this file.
18715fae107Sandi *
18815fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
18915fae107Sandi *
19015fae107Sandi * @return  string
191132bdbfeSandi */
192132bdbfeSandifunction auth_cookiesalt(){
193132bdbfeSandi  global $conf;
19498407a7aSandi  $file = $conf['metadir'].'/_htcookiesalt';
195132bdbfeSandi  $salt = io_readFile($file);
196132bdbfeSandi  if(empty($salt)){
197132bdbfeSandi    $salt = uniqid(rand(),true);
198132bdbfeSandi    io_saveFile($file,$salt);
199132bdbfeSandi  }
200132bdbfeSandi  return $salt;
201f3f0262cSandi}
202f3f0262cSandi
203f3f0262cSandi/**
204f3f0262cSandi * This clears all authenticationdata and thus log the user
205f3f0262cSandi * off
20615fae107Sandi *
20715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
208f3f0262cSandi */
209f3f0262cSandifunction auth_logoff(){
210f3f0262cSandi  global $conf;
211f3f0262cSandi  global $USERINFO;
2128b06d178Schris  global $INFO, $ID;
2135298a619SAndreas Gohr  global $auth;
21437065e65Sandi
21537065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['user']))
216132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['user']);
21737065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['pass']))
218132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['pass']);
21937065e65Sandi  if(isset($_SESSION[$conf['title']]['auth']['info']))
220132bdbfeSandi    unset($_SESSION[$conf['title']]['auth']['info']);
22137065e65Sandi  if(isset($_SERVER['REMOTE_USER']))
222f3f0262cSandi    unset($_SERVER['REMOTE_USER']);
223132bdbfeSandi  $USERINFO=null; //FIXME
2241e866646Sandi  setcookie(DOKU_COOKIE,'',time()-600000,'/');
2255298a619SAndreas Gohr
2265298a619SAndreas Gohr  if($auth && $auth->canDo('logoff')){
2275298a619SAndreas Gohr    $auth->logOff();
2285298a619SAndreas Gohr  }
229f3f0262cSandi}
230f3f0262cSandi
231f3f0262cSandi/**
23215fae107Sandi * Convinience function for auth_aclcheck()
23315fae107Sandi *
23415fae107Sandi * This checks the permissions for the current user
23515fae107Sandi *
23615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
23715fae107Sandi *
23815fae107Sandi * @param  string  $id  page ID
23915fae107Sandi * @return int          permission level
240f3f0262cSandi */
241f3f0262cSandifunction auth_quickaclcheck($id){
242f3f0262cSandi  global $conf;
243f3f0262cSandi  global $USERINFO;
244f3f0262cSandi  # if no ACL is used always return upload rights
245f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
246f3f0262cSandi  return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']);
247f3f0262cSandi}
248f3f0262cSandi
249f3f0262cSandi/**
250f3f0262cSandi * Returns the maximum rights a user has for
251f3f0262cSandi * the given ID or its namespace
25215fae107Sandi *
25315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
25415fae107Sandi *
25515fae107Sandi * @param  string  $id     page ID
25615fae107Sandi * @param  string  $user   Username
25715fae107Sandi * @param  array   $groups Array of groups the user is in
25815fae107Sandi * @return int             permission level
259f3f0262cSandi */
260f3f0262cSandifunction auth_aclcheck($id,$user,$groups){
261f3f0262cSandi  global $conf;
262f3f0262cSandi  global $AUTH_ACL;
263f3f0262cSandi
264f3f0262cSandi  # if no ACL is used always return upload rights
265f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
266f3f0262cSandi
26710a76f6fSfrank  //if user is superuser return 255 (acl_admin)
26810a76f6fSfrank  if($conf['superuser'] == $user) { return AUTH_ADMIN; }
26910a76f6fSfrank
270074cf26bSandi  //make sure groups is an array
271074cf26bSandi  if(!is_array($groups)) $groups = array();
272074cf26bSandi
27310a76f6fSfrank  //prepend groups with @
2742cd2db38Sandi  $cnt = count($groups);
2752cd2db38Sandi  for($i=0; $i<$cnt; $i++){
27610a76f6fSfrank    $groups[$i] = '@'.$groups[$i];
27710a76f6fSfrank  }
27810a76f6fSfrank  //if user is in superuser group return 255 (acl_admin)
27910a76f6fSfrank  if(in_array($conf['superuser'], $groups)) { return AUTH_ADMIN; }
28010a76f6fSfrank
281f3f0262cSandi  $ns    = getNS($id);
282f3f0262cSandi  $perm  = -1;
283f3f0262cSandi
284f3f0262cSandi  if($user){
285f3f0262cSandi    //add ALL group
286f3f0262cSandi    $groups[] = '@ALL';
287f3f0262cSandi    //add User
288f3f0262cSandi    $groups[] = $user;
289f3f0262cSandi    //build regexp
290f3f0262cSandi    $regexp   = join('|',$groups);
291f3f0262cSandi  }else{
292f3f0262cSandi    $regexp = '@ALL';
293f3f0262cSandi  }
294f3f0262cSandi
295f3f0262cSandi  //check exact match first
29642905504SAndreas Gohr  $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL);
297f3f0262cSandi  if(count($matches)){
298f3f0262cSandi    foreach($matches as $match){
299f3f0262cSandi      $match = preg_replace('/#.*$/','',$match); //ignore comments
300f3f0262cSandi      $acl   = preg_split('/\s+/',$match);
3018ef6b7caSandi      if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
302f3f0262cSandi      if($acl[2] > $perm){
303f3f0262cSandi        $perm = $acl[2];
304f3f0262cSandi      }
305f3f0262cSandi    }
306f3f0262cSandi    if($perm > -1){
307f3f0262cSandi      //we had a match - return it
308f3f0262cSandi      return $perm;
309f3f0262cSandi    }
310f3f0262cSandi  }
311f3f0262cSandi
312f3f0262cSandi  //still here? do the namespace checks
313f3f0262cSandi  if($ns){
314f3f0262cSandi    $path = $ns.':\*';
315f3f0262cSandi  }else{
316f3f0262cSandi    $path = '\*'; //root document
317f3f0262cSandi  }
318f3f0262cSandi
319f3f0262cSandi  do{
320f3f0262cSandi    $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL);
321f3f0262cSandi    if(count($matches)){
322f3f0262cSandi      foreach($matches as $match){
323f3f0262cSandi        $match = preg_replace('/#.*$/','',$match); //ignore comments
324f3f0262cSandi        $acl   = preg_split('/\s+/',$match);
3258ef6b7caSandi        if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
326f3f0262cSandi        if($acl[2] > $perm){
327f3f0262cSandi          $perm = $acl[2];
328f3f0262cSandi        }
329f3f0262cSandi      }
330f3f0262cSandi      //we had a match - return it
331f3f0262cSandi      return $perm;
332f3f0262cSandi    }
333f3f0262cSandi
334f3f0262cSandi    //get next higher namespace
335f3f0262cSandi    $ns   = getNS($ns);
336f3f0262cSandi
337f3f0262cSandi    if($path != '\*'){
338f3f0262cSandi      $path = $ns.':\*';
339f3f0262cSandi      if($path == ':\*') $path = '\*';
340f3f0262cSandi    }else{
341f3f0262cSandi      //we did this already
342f3f0262cSandi      //looks like there is something wrong with the ACL
343f3f0262cSandi      //break here
344d5ce66f6SAndreas Gohr      msg('No ACL setup yet! Denying access to everyone.');
345d5ce66f6SAndreas Gohr      return AUTH_NONE;
346f3f0262cSandi    }
347f3f0262cSandi  }while(1); //this should never loop endless
34852a5af8dSandi
34952a5af8dSandi  //still here? return no permissions
35052a5af8dSandi  return AUTH_NONE;
351f3f0262cSandi}
352f3f0262cSandi
353f3f0262cSandi/**
354f3f0262cSandi * Create a pronouncable password
355f3f0262cSandi *
35615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
35715fae107Sandi * @link    http://www.phpbuilder.com/annotate/message.php3?id=1014451
35815fae107Sandi *
35915fae107Sandi * @return string  pronouncable password
360f3f0262cSandi */
361f3f0262cSandifunction auth_pwgen(){
362f3f0262cSandi  $pw = '';
363f3f0262cSandi  $c  = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
364f3f0262cSandi  $v  = 'aeiou';              //vowels
365f3f0262cSandi  $a  = $c.$v;                //both
366f3f0262cSandi
367f3f0262cSandi  //use two syllables...
368f3f0262cSandi  for($i=0;$i < 2; $i++){
369f3f0262cSandi    $pw .= $c[rand(0, strlen($c)-1)];
370f3f0262cSandi    $pw .= $v[rand(0, strlen($v)-1)];
371f3f0262cSandi    $pw .= $a[rand(0, strlen($a)-1)];
372f3f0262cSandi  }
373f3f0262cSandi  //... and add a nice number
374f3f0262cSandi  $pw .= rand(10,99);
375f3f0262cSandi
376f3f0262cSandi  return $pw;
377f3f0262cSandi}
378f3f0262cSandi
379f3f0262cSandi/**
380f3f0262cSandi * Sends a password to the given user
381f3f0262cSandi *
38215fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
38315fae107Sandi *
38415fae107Sandi * @return bool  true on success
385f3f0262cSandi */
386f3f0262cSandifunction auth_sendPassword($user,$password){
387f3f0262cSandi  global $conf;
388f3f0262cSandi  global $lang;
389cd52f92dSchris  global $auth;
390cd52f92dSchris
391f3f0262cSandi  $hdrs  = '';
392cd52f92dSchris  $userinfo = $auth->getUserData($user);
393f3f0262cSandi
39487ddda95Sandi  if(!$userinfo['mail']) return false;
395f3f0262cSandi
396f3f0262cSandi  $text = rawLocale('password');
397ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
39887ddda95Sandi  $text = str_replace('@FULLNAME@',$userinfo['name'],$text);
399f3f0262cSandi  $text = str_replace('@LOGIN@',$user,$text);
400f3f0262cSandi  $text = str_replace('@PASSWORD@',$password,$text);
401f3f0262cSandi  $text = str_replace('@TITLE@',$conf['title'],$text);
402f3f0262cSandi
40344f669e9Sandi  return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>',
40444f669e9Sandi                   $lang['regpwmail'],
40544f669e9Sandi                   $text,
40644f669e9Sandi                   $conf['mailfrom']);
407f3f0262cSandi}
408f3f0262cSandi
409f3f0262cSandi/**
41015fae107Sandi * Register a new user
411f3f0262cSandi *
41215fae107Sandi * This registers a new user - Data is read directly from $_POST
41315fae107Sandi *
41415fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
41515fae107Sandi *
41615fae107Sandi * @return bool  true on success, false on any error
417f3f0262cSandi */
418f3f0262cSandifunction register(){
419f3f0262cSandi  global $lang;
420eb5d07e4Sjan  global $conf;
421cd52f92dSchris  global $auth;
422f3f0262cSandi
423f3f0262cSandi  if(!$_POST['save']) return false;
42482fd59b6SAndreas Gohr  if(!$auth->canDo('addUser')) return false;
425640145a5Sandi
426f3f0262cSandi  //clean username
427f3f0262cSandi  $_POST['login'] = preg_replace('/.*:/','',$_POST['login']);
428f3f0262cSandi  $_POST['login'] = cleanID($_POST['login']);
429f3f0262cSandi  //clean fullname and email
430f3f0262cSandi  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
431f3f0262cSandi  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
432f3f0262cSandi
433f3f0262cSandi  if( empty($_POST['login']) ||
434f3f0262cSandi      empty($_POST['fullname']) ||
435f3f0262cSandi      empty($_POST['email']) ){
436f3f0262cSandi    msg($lang['regmissing'],-1);
437f3f0262cSandi    return false;
438f3f0262cSandi  }
439f3f0262cSandi
440cab2716aSmatthias.grimm  if ($conf['autopasswd']) {
441cab2716aSmatthias.grimm    $pass = auth_pwgen();                // automatically generate password
442cab2716aSmatthias.grimm  } elseif (empty($_POST['pass']) ||
443cab2716aSmatthias.grimm            empty($_POST['passchk'])) {
444bf12ec81Sjan    msg($lang['regmissing'], -1);        // complain about missing passwords
445cab2716aSmatthias.grimm    return false;
446cab2716aSmatthias.grimm  } elseif ($_POST['pass'] != $_POST['passchk']) {
447bf12ec81Sjan    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
448cab2716aSmatthias.grimm    return false;
449cab2716aSmatthias.grimm  } else {
450cab2716aSmatthias.grimm    $pass = $_POST['pass'];              // accept checked and valid password
451cab2716aSmatthias.grimm  }
452cab2716aSmatthias.grimm
453f3f0262cSandi  //check mail
45444f669e9Sandi  if(!mail_isvalid($_POST['email'])){
455f3f0262cSandi    msg($lang['regbadmail'],-1);
456f3f0262cSandi    return false;
457f3f0262cSandi  }
458f3f0262cSandi
459f3f0262cSandi  //okay try to create the user
4601d096a10SAndreas Gohr  if(!$auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email'])){
461f3f0262cSandi    msg($lang['reguexists'],-1);
462f3f0262cSandi    return false;
463f3f0262cSandi  }
464f3f0262cSandi
465cab2716aSmatthias.grimm  if (!$conf['autopasswd']) {
466cab2716aSmatthias.grimm    msg($lang['regsuccess2'],1);
467cab2716aSmatthias.grimm    return true;
468cab2716aSmatthias.grimm  }
469cab2716aSmatthias.grimm
470cab2716aSmatthias.grimm  // autogenerated password? then send him the password
471f3f0262cSandi  if (auth_sendPassword($_POST['login'],$pass)){
472f3f0262cSandi    msg($lang['regsuccess'],1);
473f3f0262cSandi    return true;
474f3f0262cSandi  }else{
475f3f0262cSandi    msg($lang['regmailfail'],-1);
476f3f0262cSandi    return false;
477f3f0262cSandi  }
478f3f0262cSandi}
479f3f0262cSandi
48010a76f6fSfrank/**
4818b06d178Schris * Update user profile
4828b06d178Schris *
4838b06d178Schris * @author    Christopher Smith <chris@jalakai.co.uk>
4848b06d178Schris */
4858b06d178Schrisfunction updateprofile() {
4868b06d178Schris  global $conf;
4878b06d178Schris  global $INFO;
4888b06d178Schris  global $lang;
489cd52f92dSchris  global $auth;
4908b06d178Schris
4918b06d178Schris  if(!$_POST['save']) return false;
4928b06d178Schris
49382fd59b6SAndreas Gohr  // should not be able to get here without Profile being possible...
49482fd59b6SAndreas Gohr  if(!$auth->canDo('Profile')) {
4958b06d178Schris    msg($lang['profna'],-1);
4968b06d178Schris    return false;
4978b06d178Schris  }
4988b06d178Schris
4998b06d178Schris  if ($_POST['newpass'] != $_POST['passchk']) {
5008b06d178Schris    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
5018b06d178Schris    return false;
5028b06d178Schris  }
5038b06d178Schris
5048b06d178Schris  //clean fullname and email
5058b06d178Schris  $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname']));
5068b06d178Schris  $_POST['email']    = trim(str_replace(':','',$_POST['email']));
5078b06d178Schris
5088b06d178Schris  if (empty($_POST['fullname']) || empty($_POST['email'])) {
5098b06d178Schris    msg($lang['profnoempty'],-1);
5108b06d178Schris    return false;
5118b06d178Schris  }
5128b06d178Schris
5138b06d178Schris  if (!mail_isvalid($_POST['email'])){
5148b06d178Schris    msg($lang['regbadmail'],-1);
5158b06d178Schris    return false;
5168b06d178Schris  }
5178b06d178Schris
5188b06d178Schris  if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname'];
5198b06d178Schris  if ($_POST['email']    != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email'];
5208b06d178Schris  if (!empty($_POST['newpass']))  $changes['pass'] = $_POST['newpass'];
5218b06d178Schris
5228b06d178Schris  if (!count($changes)) {
5238b06d178Schris    msg($lang['profnochange'], -1);
5248b06d178Schris    return false;
5258b06d178Schris  }
5268b06d178Schris
5278b06d178Schris  if ($conf['profileconfirm']) {
5288b06d178Schris      if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) {
5298b06d178Schris      msg($lang['badlogin'],-1);
5308b06d178Schris      return false;
5318b06d178Schris    }
5328b06d178Schris  }
5338b06d178Schris
534cd52f92dSchris  return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes);
5358b06d178Schris}
5368b06d178Schris
5378b06d178Schris/**
5388b06d178Schris * Send a  new password
5398b06d178Schris *
5408b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info>
5418b06d178Schris * @author Chris Smith <chris@jalakai.co.uk>
5428b06d178Schris *
5438b06d178Schris * @return bool true on success, false on any error
5448b06d178Schris*/
5458b06d178Schrisfunction act_resendpwd(){
5468b06d178Schris    global $lang;
5478b06d178Schris    global $conf;
548cd52f92dSchris    global $auth;
5498b06d178Schris
5508b06d178Schris    if(!$_POST['save']) return false;
5518fd2f03aSAndreas Gohr    if(!$conf['resendpasswd']) return false;
5528b06d178Schris
55382fd59b6SAndreas Gohr    // should not be able to get here without modPass being possible...
55482fd59b6SAndreas Gohr    if(!$auth->canDo('modPass')) {
5558b06d178Schris      msg($lang['resendna'],-1);
5568b06d178Schris      return false;
5578b06d178Schris    }
5588b06d178Schris
5598b06d178Schris    if (empty($_POST['login'])) {
5608b06d178Schris      msg($lang['resendpwdmissing'], -1);
5618b06d178Schris      return false;
5628b06d178Schris    } else {
5638b06d178Schris      $user = $_POST['login'];
5648b06d178Schris    }
5658b06d178Schris
566cd52f92dSchris    $userinfo = $auth->getUserData($user);
5678b06d178Schris    if(!$userinfo['mail']) {
5688b06d178Schris      msg($lang['resendpwdnouser'], -1);
5698b06d178Schris      return false;
5708b06d178Schris    }
5718b06d178Schris
5728b06d178Schris    $pass = auth_pwgen();
573cd52f92dSchris    if (!$auth->modifyUser($user,array('pass' => $pass))) {
5748b06d178Schris      msg('error modifying user data',-1);
5758b06d178Schris      return false;
5768b06d178Schris    }
5778b06d178Schris
5788b06d178Schris    if (auth_sendPassword($user,$pass)) {
5798b06d178Schris      msg($lang['resendpwdsuccess'],1);
5808b06d178Schris    } else {
5818b06d178Schris      msg($lang['regmailfail'],-1);
5828b06d178Schris    }
5838b06d178Schris    return true;
5848b06d178Schris}
5858b06d178Schris
5868b06d178Schris/**
58710a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid
58810a76f6fSfrank *
58910a76f6fSfrank * May not be completly RFC conform!
59010a76f6fSfrank *
59110a76f6fSfrank * @link    http://www.webmasterworld.com/forum88/135.htm
59210a76f6fSfrank *
59310a76f6fSfrank * @param   string $email the address to check
59410a76f6fSfrank * @return  bool          true if address is valid
59510a76f6fSfrank */
59610a76f6fSfrankfunction isvalidemail($email){
59710a76f6fSfrank  return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
59810a76f6fSfrank}
59910a76f6fSfrank
600b0855b11Sandi/**
601b0855b11Sandi * Encrypts a password using the given method and salt
602b0855b11Sandi *
603b0855b11Sandi * If the selected method needs a salt and none was given, a random one
604b0855b11Sandi * is chosen.
605b0855b11Sandi *
606b0855b11Sandi * The following methods are understood:
607b0855b11Sandi *
608b0855b11Sandi *   smd5  - Salted MD5 hashing
609b0855b11Sandi *   md5   - Simple MD5 hashing
610b0855b11Sandi *   sha1  - SHA1 hashing
611b0855b11Sandi *   ssha  - Salted SHA1 hashing
612d7be6245Sandi *   crypt - Unix crypt
613d7be6245Sandi *   mysql - MySQL password (old method)
614d7be6245Sandi *   my411 - MySQL 4.1.1 password
615b0855b11Sandi *
616b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
617b0855b11Sandi * @return  string  The crypted password
618b0855b11Sandi */
619b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){
620b0855b11Sandi  global $conf;
621b0855b11Sandi  if(empty($method)) $method = $conf['passcrypt'];
62210a76f6fSfrank
623b0855b11Sandi  //prepare a salt
624b0855b11Sandi  if(empty($salt)) $salt = md5(uniqid(rand(), true));
625b0855b11Sandi
626b0855b11Sandi  switch(strtolower($method)){
627b0855b11Sandi    case 'smd5':
628b0855b11Sandi        return crypt($clear,'$1$'.substr($salt,0,8).'$');
629b0855b11Sandi    case 'md5':
630b0855b11Sandi      return md5($clear);
631b0855b11Sandi    case 'sha1':
632b0855b11Sandi      return sha1($clear);
633b0855b11Sandi    case 'ssha':
634b0855b11Sandi      $salt=substr($salt,0,4);
635d6e54e02Smatthiasgrimm      return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt);
636b0855b11Sandi    case 'crypt':
637b0855b11Sandi      return crypt($clear,substr($salt,0,2));
638d7be6245Sandi    case 'mysql':
639d7be6245Sandi      //from http://www.php.net/mysql comment by <soren at byu dot edu>
640d7be6245Sandi      $nr=0x50305735;
641d7be6245Sandi      $nr2=0x12345671;
642d7be6245Sandi      $add=7;
643d7be6245Sandi      $charArr = preg_split("//", $clear);
644d7be6245Sandi      foreach ($charArr as $char) {
645d7be6245Sandi        if (($char == '') || ($char == ' ') || ($char == '\t')) continue;
646d7be6245Sandi        $charVal = ord($char);
647d7be6245Sandi        $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8);
648d7be6245Sandi        $nr2 += ($nr2 << 8) ^ $nr;
649d7be6245Sandi        $add += $charVal;
650d7be6245Sandi      }
651d7be6245Sandi      return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff));
652d7be6245Sandi    case 'my411':
653d7be6245Sandi      return '*'.sha1(pack("H*", sha1($clear)));
654b0855b11Sandi    default:
655b0855b11Sandi      msg("Unsupported crypt method $method",-1);
656b0855b11Sandi  }
657b0855b11Sandi}
658b0855b11Sandi
659b0855b11Sandi/**
660b0855b11Sandi * Verifies a cleartext password against a crypted hash
661b0855b11Sandi *
662b0855b11Sandi * The method and salt used for the crypted hash is determined automatically
663b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs
664b0855b11Sandi * match true is is returned else false
665b0855b11Sandi *
666b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
667b0855b11Sandi * @return  bool
668b0855b11Sandi */
669b0855b11Sandifunction auth_verifyPassword($clear,$crypt){
670b0855b11Sandi  $method='';
671b0855b11Sandi  $salt='';
672b0855b11Sandi
673b0855b11Sandi  //determine the used method and salt
674d7be6245Sandi  $len = strlen($crypt);
675b0855b11Sandi  if(substr($crypt,0,3) == '$1$'){
676b0855b11Sandi    $method = 'smd5';
677b0855b11Sandi    $salt   = substr($crypt,3,8);
678b0855b11Sandi  }elseif(substr($crypt,0,6) == '{SSHA}'){
679b0855b11Sandi    $method = 'ssha';
680b0855b11Sandi    $salt   = substr(base64_decode(substr($crypt, 6)),20);
681d7be6245Sandi  }elseif($len == 32){
682b0855b11Sandi    $method = 'md5';
683d7be6245Sandi  }elseif($len == 40){
684b0855b11Sandi    $method = 'sha1';
685d7be6245Sandi  }elseif($len == 16){
686d7be6245Sandi    $method = 'mysql';
687d7be6245Sandi  }elseif($len == 41 && $crypt[0] == '*'){
688d7be6245Sandi    $method = 'my411';
689b0855b11Sandi  }else{
690b0855b11Sandi    $method = 'crypt';
691b0855b11Sandi    $salt   = substr($crypt,0,2);
692b0855b11Sandi  }
693b0855b11Sandi
694b0855b11Sandi  //crypt and compare
695b0855b11Sandi  if(auth_cryptPassword($clear,$method,$salt) === $crypt){
696b0855b11Sandi    return true;
697b0855b11Sandi  }
698b0855b11Sandi  return false;
699b0855b11Sandi}
700340756e4Sandi
701340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
702