xref: /dokuwiki/inc/auth.php (revision 3178426797da3e426be572a7681c3595d2166390)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * Authentication library
415fae107Sandi *
515fae107Sandi * Including this file will automatically try to login
615fae107Sandi * a user by calling auth_login()
715fae107Sandi *
815fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
915fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
1015fae107Sandi */
1115fae107Sandi
12ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
13ed7b5f09Sandi  require_once(DOKU_INC.'inc/common.php');
14ed7b5f09Sandi  require_once(DOKU_INC.'inc/io.php');
151c73890cSAndreas Gohr
16ebf97c8fSAndreas Gohr  // some ACL level defines
17ebf97c8fSAndreas Gohr  define('AUTH_NONE',0);
18ebf97c8fSAndreas Gohr  define('AUTH_READ',1);
19ebf97c8fSAndreas Gohr  define('AUTH_EDIT',2);
20ebf97c8fSAndreas Gohr  define('AUTH_CREATE',4);
21ebf97c8fSAndreas Gohr  define('AUTH_UPLOAD',8);
22ebf97c8fSAndreas Gohr  define('AUTH_DELETE',16);
23ebf97c8fSAndreas Gohr  define('AUTH_ADMIN',255);
24ebf97c8fSAndreas Gohr
25742c66f8Schris  global $conf;
26742c66f8Schris
271c73890cSAndreas Gohr  if($conf['useacl']){
28ed7b5f09Sandi    require_once(DOKU_INC.'inc/blowfish.php');
29ed7b5f09Sandi    require_once(DOKU_INC.'inc/mail.php');
308b06d178Schris
3103c4aec3Schris    global $auth;
3203c4aec3Schris
338b06d178Schris    // load the the backend auth functions and instantiate the auth object
348b06d178Schris    if (@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) {
358b06d178Schris      require_once(DOKU_INC.'inc/auth/basic.class.php');
368b06d178Schris      require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php');
378b06d178Schris
388b06d178Schris      $auth_class = "auth_".$conf['authtype'];
39cd52f92dSchris      if (class_exists($auth_class)) {
408b06d178Schris        $auth = new $auth_class();
41d2dde4ebSMatthias Grimm        if ($auth->success == false) {
42d2dde4ebSMatthias Grimm          unset($auth);
43cd52f92dSchris          msg($lang['authtempfail'], -1);
44cd52f92dSchris
45cd52f92dSchris          // turn acl config setting off for the rest of this page
46cd52f92dSchris          $conf['useacl'] = 0;
47d2dde4ebSMatthias Grimm        }
488b06d178Schris      } else {
493816dcbcSAndreas Gohr        nice_die($lang['authmodfailed']);
50cd52f92dSchris      }
51cd52f92dSchris    } else {
523816dcbcSAndreas Gohr      nice_die($lang['authmodfailed']);
538b06d178Schris    }
541c73890cSAndreas Gohr  }
55f3f0262cSandi
56f5cb575dSAndreas Gohr  // do the login either by cookie or provided credentials
57f3f0262cSandi  if($conf['useacl']){
58bbbd6568SAndreas Gohr    if (!isset($_REQUEST['u'])) $_REQUEST['u'] = '';
59bbbd6568SAndreas Gohr    if (!isset($_REQUEST['p'])) $_REQUEST['p'] = '';
60bbbd6568SAndreas Gohr    if (!isset($_REQUEST['r'])) $_REQUEST['r'] = '';
61bbbd6568SAndreas Gohr
621e8c9c90SAndreas Gohr    // if no credentials were given try to use HTTP auth (for SSO)
634a26ad85Schris    if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])){
641e8c9c90SAndreas Gohr      $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER'];
651e8c9c90SAndreas Gohr      $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW'];
661e8c9c90SAndreas Gohr    }
671e8c9c90SAndreas Gohr
68f5cb575dSAndreas Gohr    // external trust mechanism in place?
6982fd59b6SAndreas Gohr    if(!is_null($auth) && $auth->canDo('external')){
70f5cb575dSAndreas Gohr      $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
71f5cb575dSAndreas Gohr    }else{
72132bdbfeSandi      auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
73f5cb575dSAndreas Gohr    }
74f5cb575dSAndreas Gohr
7515fae107Sandi    //load ACL into a global array
7688e6a4f2SAndreas Gohr    global $AUTH_ACL;
77e7cb32dcSAndreas Gohr    if(is_readable(DOKU_CONF.'acl.auth.php')){
78e7cb32dcSAndreas Gohr      $AUTH_ACL = file(DOKU_CONF.'acl.auth.php');
7911799630Sandi    }else{
8011799630Sandi      $AUTH_ACL = array();
8111799630Sandi    }
82f3f0262cSandi  }
83f3f0262cSandi
84f3f0262cSandi/**
85f3f0262cSandi * This tries to login the user based on the sent auth credentials
86f3f0262cSandi *
87f3f0262cSandi * The authentication works like this: if a username was given
8815fae107Sandi * a new login is assumed and user/password are checked. If they
8915fae107Sandi * are correct the password is encrypted with blowfish and stored
9015fae107Sandi * together with the username in a cookie - the same info is stored
9115fae107Sandi * in the session, too. Additonally a browserID is stored in the
9215fae107Sandi * session.
9315fae107Sandi *
9415fae107Sandi * If no username was given the cookie is checked: if the username,
9515fae107Sandi * crypted password and browserID match between session and cookie
9615fae107Sandi * no further testing is done and the user is accepted
9715fae107Sandi *
9815fae107Sandi * If a cookie was found but no session info was availabe the
99136ce040Sandi * blowfish encrypted password from the cookie is decrypted and
10015fae107Sandi * together with username rechecked by calling this function again.
101f3f0262cSandi *
102f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO
103f3f0262cSandi * are set.
10415fae107Sandi *
10515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
10615fae107Sandi *
10715fae107Sandi * @param   string  $user    Username
10815fae107Sandi * @param   string  $pass    Cleartext Password
10915fae107Sandi * @param   bool    $sticky  Cookie should not expire
110f112c2faSAndreas Gohr * @param   bool    $silent  Don't show error on bad auth
11115fae107Sandi * @return  bool             true on successful auth
112f3f0262cSandi*/
113f112c2faSAndreas Gohrfunction auth_login($user,$pass,$sticky=false,$silent=false){
114f3f0262cSandi  global $USERINFO;
115f3f0262cSandi  global $conf;
116f3f0262cSandi  global $lang;
117cd52f92dSchris  global $auth;
118132bdbfeSandi  $sticky ? $sticky = true : $sticky = false; //sanity check
119f3f0262cSandi
120bbbd6568SAndreas Gohr  if(!empty($user)){
121132bdbfeSandi    //usual login
122cd52f92dSchris    if ($auth->checkPass($user,$pass)){
123132bdbfeSandi      // make logininfo globally available
124f3f0262cSandi      $_SERVER['REMOTE_USER'] = $user;
125cd52f92dSchris      $USERINFO = $auth->getUserData($user); //FIXME move all references to session
126132bdbfeSandi
127132bdbfeSandi      // set cookie
128132bdbfeSandi      $pass   = PMA_blowfish_encrypt($pass,auth_cookiesalt());
129132bdbfeSandi      $cookie = base64_encode("$user|$sticky|$pass");
130132bdbfeSandi      if($sticky) $time = time()+60*60*24*365; //one year
131e65afed4SSameer D. Sahasrabuddhe      setcookie(DOKU_COOKIE,$cookie,$time,'/');
132132bdbfeSandi
133132bdbfeSandi      // set session
134e71ce681SAndreas Gohr      $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
135e71ce681SAndreas Gohr      $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass;
136e71ce681SAndreas Gohr      $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid();
137e71ce681SAndreas Gohr      $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
138132bdbfeSandi      return true;
139f3f0262cSandi    }else{
140f3f0262cSandi      //invalid credentials - log off
141f112c2faSAndreas Gohr      if(!$silent) msg($lang['badlogin'],-1);
142f3f0262cSandi      auth_logoff();
143132bdbfeSandi      return false;
144f3f0262cSandi    }
145f3f0262cSandi  }else{
146132bdbfeSandi    // read cookie information
147e65afed4SSameer D. Sahasrabuddhe    $cookie = base64_decode($_COOKIE[DOKU_COOKIE]);
148132bdbfeSandi    list($user,$sticky,$pass) = split('\|',$cookie,3);
149132bdbfeSandi    // get session info
150e71ce681SAndreas Gohr    $session = $_SESSION[DOKU_COOKIE]['auth'];
151132bdbfeSandi    if($user && $pass){
152132bdbfeSandi      // we got a cookie - see if we can trust it
153132bdbfeSandi      if(isset($session) &&
154132bdbfeSandi        ($session['user'] == $user) &&
155132bdbfeSandi        ($session['pass'] == $pass) &&  //still crypted
156132bdbfeSandi        ($session['buid'] == auth_browseruid()) ){
157132bdbfeSandi        // he has session, cookie and browser right - let him in
158132bdbfeSandi        $_SERVER['REMOTE_USER'] = $user;
159132bdbfeSandi        $USERINFO = $session['info']; //FIXME move all references to session
160132bdbfeSandi        return true;
161132bdbfeSandi      }
162f112c2faSAndreas Gohr      // no we don't trust it yet - recheck pass but silent
163132bdbfeSandi      $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt());
164f112c2faSAndreas Gohr      return auth_login($user,$pass,$sticky,true);
165132bdbfeSandi    }
166132bdbfeSandi  }
167f3f0262cSandi  //just to be sure
168f3f0262cSandi  auth_logoff();
169132bdbfeSandi  return false;
170f3f0262cSandi}
171132bdbfeSandi
172132bdbfeSandi/**
173136ce040Sandi * Builds a pseudo UID from browser and IP data
174132bdbfeSandi *
175132bdbfeSandi * This is neither unique nor unfakable - still it adds some
176136ce040Sandi * security. Using the first part of the IP makes sure
177136ce040Sandi * proxy farms like AOLs are stil okay.
17815fae107Sandi *
17915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
18015fae107Sandi *
18115fae107Sandi * @return  string  a MD5 sum of various browser headers
182132bdbfeSandi */
183132bdbfeSandifunction auth_browseruid(){
184132bdbfeSandi  $uid  = '';
185132bdbfeSandi  $uid .= $_SERVER['HTTP_USER_AGENT'];
186132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_ENCODING'];
187132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
188132bdbfeSandi  $uid .= $_SERVER['HTTP_ACCEPT_CHARSET'];
189136ce040Sandi  $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.'));
190132bdbfeSandi  return md5($uid);
191132bdbfeSandi}
192132bdbfeSandi
193132bdbfeSandi/**
194132bdbfeSandi * Creates a random key to encrypt the password in cookies
19515fae107Sandi *
19615fae107Sandi * This function tries to read the password for encrypting
19798407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt'
19815fae107Sandi * if no such file is found a random key is created and
19915fae107Sandi * and stored in this file.
20015fae107Sandi *
20115fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
20215fae107Sandi *
20315fae107Sandi * @return  string
204132bdbfeSandi */
205132bdbfeSandifunction auth_cookiesalt(){
206132bdbfeSandi  global $conf;
20798407a7aSandi  $file = $conf['metadir'].'/_htcookiesalt';
208132bdbfeSandi  $salt = io_readFile($file);
209132bdbfeSandi  if(empty($salt)){
210132bdbfeSandi    $salt = uniqid(rand(),true);
211132bdbfeSandi    io_saveFile($file,$salt);
212132bdbfeSandi  }
213132bdbfeSandi  return $salt;
214f3f0262cSandi}
215f3f0262cSandi
216f3f0262cSandi/**
217f3f0262cSandi * This clears all authenticationdata and thus log the user
218f3f0262cSandi * off
21915fae107Sandi *
22015fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
221f3f0262cSandi */
222f3f0262cSandifunction auth_logoff(){
223f3f0262cSandi  global $conf;
224f3f0262cSandi  global $USERINFO;
2258b06d178Schris  global $INFO, $ID;
2265298a619SAndreas Gohr  global $auth;
22737065e65Sandi
228e71ce681SAndreas Gohr  if(isset($_SESSION[DOKU_COOKIE]['auth']['user']))
229e71ce681SAndreas Gohr    unset($_SESSION[DOKU_COOKIE]['auth']['user']);
230e71ce681SAndreas Gohr  if(isset($_SESSION[DOKU_COOKIE]['auth']['pass']))
231e71ce681SAndreas Gohr    unset($_SESSION[DOKU_COOKIE]['auth']['pass']);
232e71ce681SAndreas Gohr  if(isset($_SESSION[DOKU_COOKIE]['auth']['info']))
233e71ce681SAndreas Gohr    unset($_SESSION[DOKU_COOKIE]['auth']['info']);
23437065e65Sandi  if(isset($_SERVER['REMOTE_USER']))
235f3f0262cSandi    unset($_SERVER['REMOTE_USER']);
236132bdbfeSandi  $USERINFO=null; //FIXME
2371e866646Sandi  setcookie(DOKU_COOKIE,'',time()-600000,'/');
2385298a619SAndreas Gohr
2395298a619SAndreas Gohr  if($auth && $auth->canDo('logoff')){
2405298a619SAndreas Gohr    $auth->logOff();
2415298a619SAndreas Gohr  }
242f3f0262cSandi}
243f3f0262cSandi
244f3f0262cSandi/**
245f8cc712eSAndreas Gohr * Check if a user is a manager
246f8cc712eSAndreas Gohr *
247f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current
248f8cc712eSAndreas Gohr * user.
249f8cc712eSAndreas Gohr *
250f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too
251f8cc712eSAndreas Gohr *
252f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
253f8cc712eSAndreas Gohr * @see    auth_isadmin
254f8cc712eSAndreas Gohr * @param  string user      - Username
255f8cc712eSAndreas Gohr * @param  array  groups    - List of groups the user is in
256f8cc712eSAndreas Gohr * @param  bool   adminonly - when true checks if user is admin
257f8cc712eSAndreas Gohr */
258f8cc712eSAndreas Gohrfunction auth_ismanager($user=null,$groups=null,$adminonly=false){
259f8cc712eSAndreas Gohr  global $conf;
260f8cc712eSAndreas Gohr  global $USERINFO;
261f8cc712eSAndreas Gohr
262f8cc712eSAndreas Gohr  if(!$conf['useacl']) return false;
263f8cc712eSAndreas Gohr  if(is_null($user))   $user   = $_SERVER['REMOTE_USER'];
264f8cc712eSAndreas Gohr  if(is_null($groups)) $groups = $USERINFO['grps'];
265f8cc712eSAndreas Gohr  $user   = auth_nameencode($user);
266f8cc712eSAndreas Gohr
267f8cc712eSAndreas Gohr  // check username against superuser and manager
268f8cc712eSAndreas Gohr  if(auth_nameencode($conf['superuser']) == $user) return true;
269f8cc712eSAndreas Gohr  if(!$adminonly){
270f8cc712eSAndreas Gohr    if(auth_nameencode($conf['manager']) == $user) return true;
271f8cc712eSAndreas Gohr  }
272f8cc712eSAndreas Gohr
273f8cc712eSAndreas Gohr  //prepend groups with @ and nameencode
274f8cc712eSAndreas Gohr  $cnt = count($groups);
275f8cc712eSAndreas Gohr  for($i=0; $i<$cnt; $i++){
276f8cc712eSAndreas Gohr    $groups[$i] = '@'.auth_nameencode($groups[$i]);
277f8cc712eSAndreas Gohr  }
278f8cc712eSAndreas Gohr
279f8cc712eSAndreas Gohr  // check groups against superuser and manager
280f8cc712eSAndreas Gohr  if(in_array(auth_nameencode($conf['superuser'],true), $groups)) return true;
281f8cc712eSAndreas Gohr  if(!$adminonly){
282f8cc712eSAndreas Gohr    if(in_array(auth_nameencode($conf['manager'],true), $groups)) return true;
283f8cc712eSAndreas Gohr  }
284f8cc712eSAndreas Gohr  return false;
285f8cc712eSAndreas Gohr}
286f8cc712eSAndreas Gohr
287f8cc712eSAndreas Gohr/**
288f8cc712eSAndreas Gohr * Check if a user is admin
289f8cc712eSAndreas Gohr *
290f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true
291f8cc712eSAndreas Gohr *
292f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too
293f8cc712eSAndreas Gohr *
294f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
295f8cc712eSAndreas Gohr * @see auth_ismanager
296f8cc712eSAndreas Gohr */
297f8cc712eSAndreas Gohrfunction auth_isadmin($user=null,$groups=null){
298f8cc712eSAndreas Gohr  return auth_ismanager($user,$groups,true);
299f8cc712eSAndreas Gohr}
300f8cc712eSAndreas Gohr
301f8cc712eSAndreas Gohr/**
30215fae107Sandi * Convinience function for auth_aclcheck()
30315fae107Sandi *
30415fae107Sandi * This checks the permissions for the current user
30515fae107Sandi *
30615fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
30715fae107Sandi *
30815fae107Sandi * @param  string  $id  page ID
30915fae107Sandi * @return int          permission level
310f3f0262cSandi */
311f3f0262cSandifunction auth_quickaclcheck($id){
312f3f0262cSandi  global $conf;
313f3f0262cSandi  global $USERINFO;
314f3f0262cSandi  # if no ACL is used always return upload rights
315f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
316f3f0262cSandi  return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']);
317f3f0262cSandi}
318f3f0262cSandi
319f3f0262cSandi/**
320f3f0262cSandi * Returns the maximum rights a user has for
321f3f0262cSandi * the given ID or its namespace
32215fae107Sandi *
32315fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
32415fae107Sandi *
32515fae107Sandi * @param  string  $id     page ID
32615fae107Sandi * @param  string  $user   Username
32715fae107Sandi * @param  array   $groups Array of groups the user is in
32815fae107Sandi * @return int             permission level
329f3f0262cSandi */
330f3f0262cSandifunction auth_aclcheck($id,$user,$groups){
331f3f0262cSandi  global $conf;
332f3f0262cSandi  global $AUTH_ACL;
333f3f0262cSandi
334f3f0262cSandi  # if no ACL is used always return upload rights
335f3f0262cSandi  if(!$conf['useacl']) return AUTH_UPLOAD;
336f3f0262cSandi
3376c2bb100SAndreas Gohr  $user = auth_nameencode($user);
3386c2bb100SAndreas Gohr
33910a76f6fSfrank  //if user is superuser return 255 (acl_admin)
340e838fc2eSAndreas Gohr  if(auth_nameencode($conf['superuser']) == $user) { return AUTH_ADMIN; }
34110a76f6fSfrank
342074cf26bSandi  //make sure groups is an array
343074cf26bSandi  if(!is_array($groups)) $groups = array();
344074cf26bSandi
3456c2bb100SAndreas Gohr  //prepend groups with @ and nameencode
3462cd2db38Sandi  $cnt = count($groups);
3472cd2db38Sandi  for($i=0; $i<$cnt; $i++){
3486c2bb100SAndreas Gohr    $groups[$i] = '@'.auth_nameencode($groups[$i]);
34910a76f6fSfrank  }
35010a76f6fSfrank  //if user is in superuser group return 255 (acl_admin)
351e838fc2eSAndreas Gohr  if(in_array(auth_nameencode($conf['superuser'],true), $groups)) { return AUTH_ADMIN; }
35210a76f6fSfrank
353f3f0262cSandi  $ns    = getNS($id);
354f3f0262cSandi  $perm  = -1;
355f3f0262cSandi
356f3f0262cSandi  if($user){
357f3f0262cSandi    //add ALL group
358f3f0262cSandi    $groups[] = '@ALL';
359f3f0262cSandi    //add User
360f3f0262cSandi    $groups[] = $user;
361f3f0262cSandi    //build regexp
362f3f0262cSandi    $regexp   = join('|',$groups);
363f3f0262cSandi  }else{
364f3f0262cSandi    $regexp = '@ALL';
365f3f0262cSandi  }
366f3f0262cSandi
367f3f0262cSandi  //check exact match first
36842905504SAndreas Gohr  $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL);
369f3f0262cSandi  if(count($matches)){
370f3f0262cSandi    foreach($matches as $match){
371f3f0262cSandi      $match = preg_replace('/#.*$/','',$match); //ignore comments
372f3f0262cSandi      $acl   = preg_split('/\s+/',$match);
3738ef6b7caSandi      if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
374f3f0262cSandi      if($acl[2] > $perm){
375f3f0262cSandi        $perm = $acl[2];
376f3f0262cSandi      }
377f3f0262cSandi    }
378f3f0262cSandi    if($perm > -1){
379f3f0262cSandi      //we had a match - return it
380f3f0262cSandi      return $perm;
381f3f0262cSandi    }
382f3f0262cSandi  }
383f3f0262cSandi
384f3f0262cSandi  //still here? do the namespace checks
385f3f0262cSandi  if($ns){
386f3f0262cSandi    $path = $ns.':\*';
387f3f0262cSandi  }else{
388f3f0262cSandi    $path = '\*'; //root document
389f3f0262cSandi  }
390f3f0262cSandi
391f3f0262cSandi  do{
392f3f0262cSandi    $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL);
393f3f0262cSandi    if(count($matches)){
394f3f0262cSandi      foreach($matches as $match){
395f3f0262cSandi        $match = preg_replace('/#.*$/','',$match); //ignore comments
396f3f0262cSandi        $acl   = preg_split('/\s+/',$match);
3978ef6b7caSandi        if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL!
398f3f0262cSandi        if($acl[2] > $perm){
399f3f0262cSandi          $perm = $acl[2];
400f3f0262cSandi        }
401f3f0262cSandi      }
402f3f0262cSandi      //we had a match - return it
403f3f0262cSandi      return $perm;
404f3f0262cSandi    }
405f3f0262cSandi
406f3f0262cSandi    //get next higher namespace
407f3f0262cSandi    $ns   = getNS($ns);
408f3f0262cSandi
409f3f0262cSandi    if($path != '\*'){
410f3f0262cSandi      $path = $ns.':\*';
411f3f0262cSandi      if($path == ':\*') $path = '\*';
412f3f0262cSandi    }else{
413f3f0262cSandi      //we did this already
414f3f0262cSandi      //looks like there is something wrong with the ACL
415f3f0262cSandi      //break here
416d5ce66f6SAndreas Gohr      msg('No ACL setup yet! Denying access to everyone.');
417d5ce66f6SAndreas Gohr      return AUTH_NONE;
418f3f0262cSandi    }
419f3f0262cSandi  }while(1); //this should never loop endless
42052a5af8dSandi
42152a5af8dSandi  //still here? return no permissions
42252a5af8dSandi  return AUTH_NONE;
423f3f0262cSandi}
424f3f0262cSandi
425f3f0262cSandi/**
4266c2bb100SAndreas Gohr * Encode ASCII special chars
4276c2bb100SAndreas Gohr *
4286c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames
4296c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars
4306c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual
4316c2bb100SAndreas Gohr * urlencoding!).
4326c2bb100SAndreas Gohr *
4336c2bb100SAndreas Gohr * Decoding can be done with rawurldecode
4346c2bb100SAndreas Gohr *
4356c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4366c2bb100SAndreas Gohr * @see rawurldecode()
4376c2bb100SAndreas Gohr */
438e838fc2eSAndreas Gohrfunction auth_nameencode($name,$skip_group=false){
439a424cd8eSchris  global $cache_authname;
440a424cd8eSchris  $cache =& $cache_authname;
441*31784267SAndreas Gohr  $name  = (string) $name;
442a424cd8eSchris
443a424cd8eSchris  if (!isset($cache[$name][$skip_group])) {
444e838fc2eSAndreas Gohr    if($skip_group && $name{0} =='@'){
445a424cd8eSchris      $cache[$name][$skip_group] = '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e',
446e838fc2eSAndreas Gohr                                                    "'%'.dechex(ord('\\1'))",substr($name,1));
447e838fc2eSAndreas Gohr    }else{
448a424cd8eSchris      $cache[$name][$skip_group] = preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e',
449e838fc2eSAndreas Gohr                                                "'%'.dechex(ord('\\1'))",$name);
450e838fc2eSAndreas Gohr    }
4516c2bb100SAndreas Gohr  }
4526c2bb100SAndreas Gohr
453a424cd8eSchris  return $cache[$name][$skip_group];
454a424cd8eSchris}
455a424cd8eSchris
4566c2bb100SAndreas Gohr/**
457f3f0262cSandi * Create a pronouncable password
458f3f0262cSandi *
45915fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
46015fae107Sandi * @link    http://www.phpbuilder.com/annotate/message.php3?id=1014451
46115fae107Sandi *
46215fae107Sandi * @return string  pronouncable password
463f3f0262cSandi */
464f3f0262cSandifunction auth_pwgen(){
465f3f0262cSandi  $pw = '';
466f3f0262cSandi  $c  = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
467f3f0262cSandi  $v  = 'aeiou';              //vowels
468f3f0262cSandi  $a  = $c.$v;                //both
469f3f0262cSandi
470f3f0262cSandi  //use two syllables...
471f3f0262cSandi  for($i=0;$i < 2; $i++){
472f3f0262cSandi    $pw .= $c[rand(0, strlen($c)-1)];
473f3f0262cSandi    $pw .= $v[rand(0, strlen($v)-1)];
474f3f0262cSandi    $pw .= $a[rand(0, strlen($a)-1)];
475f3f0262cSandi  }
476f3f0262cSandi  //... and add a nice number
477f3f0262cSandi  $pw .= rand(10,99);
478f3f0262cSandi
479f3f0262cSandi  return $pw;
480f3f0262cSandi}
481f3f0262cSandi
482f3f0262cSandi/**
483f3f0262cSandi * Sends a password to the given user
484f3f0262cSandi *
48515fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
48615fae107Sandi *
48715fae107Sandi * @return bool  true on success
488f3f0262cSandi */
489f3f0262cSandifunction auth_sendPassword($user,$password){
490f3f0262cSandi  global $conf;
491f3f0262cSandi  global $lang;
492cd52f92dSchris  global $auth;
493cd52f92dSchris
494f3f0262cSandi  $hdrs  = '';
495cd52f92dSchris  $userinfo = $auth->getUserData($user);
496f3f0262cSandi
49787ddda95Sandi  if(!$userinfo['mail']) return false;
498f3f0262cSandi
499f3f0262cSandi  $text = rawLocale('password');
500ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
50187ddda95Sandi  $text = str_replace('@FULLNAME@',$userinfo['name'],$text);
502f3f0262cSandi  $text = str_replace('@LOGIN@',$user,$text);
503f3f0262cSandi  $text = str_replace('@PASSWORD@',$password,$text);
504f3f0262cSandi  $text = str_replace('@TITLE@',$conf['title'],$text);
505f3f0262cSandi
50644f669e9Sandi  return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>',
50744f669e9Sandi                   $lang['regpwmail'],
50844f669e9Sandi                   $text,
50944f669e9Sandi                   $conf['mailfrom']);
510f3f0262cSandi}
511f3f0262cSandi
512f3f0262cSandi/**
51315fae107Sandi * Register a new user
514f3f0262cSandi *
51515fae107Sandi * This registers a new user - Data is read directly from $_POST
51615fae107Sandi *
51715fae107Sandi * @author  Andreas Gohr <andi@splitbrain.org>
51815fae107Sandi *
51915fae107Sandi * @return bool  true on success, false on any error
520f3f0262cSandi */
521f3f0262cSandifunction register(){
522f3f0262cSandi  global $lang;
523eb5d07e4Sjan  global $conf;
524cd52f92dSchris  global $auth;
525f3f0262cSandi
526f3f0262cSandi  if(!$_POST['save']) return false;
52782fd59b6SAndreas Gohr  if(!$auth->canDo('addUser')) return false;
528640145a5Sandi
529f3f0262cSandi  //clean username
530f3f0262cSandi  $_POST['login'] = preg_replace('/.*:/','',$_POST['login']);
531f3f0262cSandi  $_POST['login'] = cleanID($_POST['login']);
532f3f0262cSandi  //clean fullname and email
53354f0e6eaSAndreas Gohr  $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname']));
53454f0e6eaSAndreas Gohr  $_POST['email']    = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email']));
535f3f0262cSandi
536f3f0262cSandi  if( empty($_POST['login']) ||
537f3f0262cSandi      empty($_POST['fullname']) ||
538f3f0262cSandi      empty($_POST['email']) ){
539f3f0262cSandi    msg($lang['regmissing'],-1);
540f3f0262cSandi    return false;
541f3f0262cSandi  }
542f3f0262cSandi
543cab2716aSmatthias.grimm  if ($conf['autopasswd']) {
544cab2716aSmatthias.grimm    $pass = auth_pwgen();                // automatically generate password
545cab2716aSmatthias.grimm  } elseif (empty($_POST['pass']) ||
546cab2716aSmatthias.grimm            empty($_POST['passchk'])) {
547bf12ec81Sjan    msg($lang['regmissing'], -1);        // complain about missing passwords
548cab2716aSmatthias.grimm    return false;
549cab2716aSmatthias.grimm  } elseif ($_POST['pass'] != $_POST['passchk']) {
550bf12ec81Sjan    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
551cab2716aSmatthias.grimm    return false;
552cab2716aSmatthias.grimm  } else {
553cab2716aSmatthias.grimm    $pass = $_POST['pass'];              // accept checked and valid password
554cab2716aSmatthias.grimm  }
555cab2716aSmatthias.grimm
556f3f0262cSandi  //check mail
55744f669e9Sandi  if(!mail_isvalid($_POST['email'])){
558f3f0262cSandi    msg($lang['regbadmail'],-1);
559f3f0262cSandi    return false;
560f3f0262cSandi  }
561f3f0262cSandi
562f3f0262cSandi  //okay try to create the user
5631d096a10SAndreas Gohr  if(!$auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email'])){
564f3f0262cSandi    msg($lang['reguexists'],-1);
565f3f0262cSandi    return false;
566f3f0262cSandi  }
567f3f0262cSandi
56802a498e7Schris  // create substitutions for use in notification email
56902a498e7Schris  $substitutions = array(
57002a498e7Schris    'NEWUSER' => $_POST['login'],
57102a498e7Schris    'NEWNAME' => $_POST['fullname'],
57202a498e7Schris    'NEWEMAIL' => $_POST['email'],
57302a498e7Schris  );
57402a498e7Schris
575cab2716aSmatthias.grimm  if (!$conf['autopasswd']) {
576cab2716aSmatthias.grimm    msg($lang['regsuccess2'],1);
57702a498e7Schris    notify('', 'register', '', $_POST['login'], false, $substitutions);
578cab2716aSmatthias.grimm    return true;
579cab2716aSmatthias.grimm  }
580cab2716aSmatthias.grimm
581cab2716aSmatthias.grimm  // autogenerated password? then send him the password
582f3f0262cSandi  if (auth_sendPassword($_POST['login'],$pass)){
583f3f0262cSandi    msg($lang['regsuccess'],1);
58402a498e7Schris    notify('', 'register', '', $_POST['login'], false, $substitutions);
585f3f0262cSandi    return true;
586f3f0262cSandi  }else{
587f3f0262cSandi    msg($lang['regmailfail'],-1);
588f3f0262cSandi    return false;
589f3f0262cSandi  }
590f3f0262cSandi}
591f3f0262cSandi
59210a76f6fSfrank/**
5938b06d178Schris * Update user profile
5948b06d178Schris *
5958b06d178Schris * @author    Christopher Smith <chris@jalakai.co.uk>
5968b06d178Schris */
5978b06d178Schrisfunction updateprofile() {
5988b06d178Schris  global $conf;
5998b06d178Schris  global $INFO;
6008b06d178Schris  global $lang;
601cd52f92dSchris  global $auth;
6028b06d178Schris
603bb4866bdSchris  if(empty($_POST['save'])) return false;
6048b06d178Schris
60582fd59b6SAndreas Gohr  // should not be able to get here without Profile being possible...
60682fd59b6SAndreas Gohr  if(!$auth->canDo('Profile')) {
6078b06d178Schris    msg($lang['profna'],-1);
6088b06d178Schris    return false;
6098b06d178Schris  }
6108b06d178Schris
6118b06d178Schris  if ($_POST['newpass'] != $_POST['passchk']) {
6128b06d178Schris    msg($lang['regbadpass'], -1);      // complain about misspelled passwords
6138b06d178Schris    return false;
6148b06d178Schris  }
6158b06d178Schris
6168b06d178Schris  //clean fullname and email
61754f0e6eaSAndreas Gohr  $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname']));
61854f0e6eaSAndreas Gohr  $_POST['email']    = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email']));
6198b06d178Schris
6208b06d178Schris  if (empty($_POST['fullname']) || empty($_POST['email'])) {
6218b06d178Schris    msg($lang['profnoempty'],-1);
6228b06d178Schris    return false;
6238b06d178Schris  }
6248b06d178Schris
6258b06d178Schris  if (!mail_isvalid($_POST['email'])){
6268b06d178Schris    msg($lang['regbadmail'],-1);
6278b06d178Schris    return false;
6288b06d178Schris  }
6298b06d178Schris
6308b06d178Schris  if ($_POST['fullname'] != $INFO['userinfo']['name']) $changes['name'] = $_POST['fullname'];
6318b06d178Schris  if ($_POST['email']    != $INFO['userinfo']['mail']) $changes['mail'] = $_POST['email'];
6328b06d178Schris  if (!empty($_POST['newpass']))  $changes['pass'] = $_POST['newpass'];
6338b06d178Schris
6348b06d178Schris  if (!count($changes)) {
6358b06d178Schris    msg($lang['profnochange'], -1);
6368b06d178Schris    return false;
6378b06d178Schris  }
6388b06d178Schris
6398b06d178Schris  if ($conf['profileconfirm']) {
6408b06d178Schris      if (!auth_verifyPassword($_POST['oldpass'],$INFO['userinfo']['pass'])) {
6418b06d178Schris      msg($lang['badlogin'],-1);
6428b06d178Schris      return false;
6438b06d178Schris    }
6448b06d178Schris  }
6458b06d178Schris
646cd52f92dSchris  return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes);
6478b06d178Schris}
6488b06d178Schris
6498b06d178Schris/**
6508b06d178Schris * Send a  new password
6518b06d178Schris *
6521d5856cfSAndreas Gohr * This function handles both phases of the password reset:
6531d5856cfSAndreas Gohr *
6541d5856cfSAndreas Gohr *   - handling the first request of password reset
6551d5856cfSAndreas Gohr *   - validating the password reset auth token
6561d5856cfSAndreas Gohr *
6578b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info>
6588b06d178Schris * @author Chris Smith <chris@jalakai.co.uk>
6591d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6608b06d178Schris *
6618b06d178Schris * @return bool true on success, false on any error
6628b06d178Schris*/
6638b06d178Schrisfunction act_resendpwd(){
6648b06d178Schris    global $lang;
6658b06d178Schris    global $conf;
666cd52f92dSchris    global $auth;
6678b06d178Schris
668409d7af7SAndreas Gohr    if(!actionOK('resendpwd')) return false;
6698b06d178Schris
67082fd59b6SAndreas Gohr    // should not be able to get here without modPass being possible...
67182fd59b6SAndreas Gohr    if(!$auth->canDo('modPass')) {
6728b06d178Schris        msg($lang['resendna'],-1);
6738b06d178Schris        return false;
6748b06d178Schris    }
6758b06d178Schris
6761d5856cfSAndreas Gohr    $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']);
6778b06d178Schris
6781d5856cfSAndreas Gohr    if($token){
6791d5856cfSAndreas Gohr        // we're in token phase
6801d5856cfSAndreas Gohr
6811d5856cfSAndreas Gohr        $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth';
6821d5856cfSAndreas Gohr        if(!@file_exists($tfile)){
6831d5856cfSAndreas Gohr            msg($lang['resendpwdbadauth'],-1);
6841d5856cfSAndreas Gohr            return false;
6851d5856cfSAndreas Gohr        }
6861d5856cfSAndreas Gohr        $user = io_readfile($tfile);
6871d5856cfSAndreas Gohr        @unlink($tfile);
688cd52f92dSchris        $userinfo = $auth->getUserData($user);
6898b06d178Schris        if(!$userinfo['mail']) {
6908b06d178Schris            msg($lang['resendpwdnouser'], -1);
6918b06d178Schris            return false;
6928b06d178Schris        }
6938b06d178Schris
6948b06d178Schris        $pass = auth_pwgen();
695cd52f92dSchris        if (!$auth->modifyUser($user,array('pass' => $pass))) {
6968b06d178Schris            msg('error modifying user data',-1);
6978b06d178Schris            return false;
6988b06d178Schris        }
6998b06d178Schris
7008b06d178Schris        if (auth_sendPassword($user,$pass)) {
7018b06d178Schris            msg($lang['resendpwdsuccess'],1);
7028b06d178Schris        } else {
7038b06d178Schris            msg($lang['regmailfail'],-1);
7048b06d178Schris        }
7058b06d178Schris        return true;
7061d5856cfSAndreas Gohr
7071d5856cfSAndreas Gohr    } else {
7081d5856cfSAndreas Gohr        // we're in request phase
7091d5856cfSAndreas Gohr
7101d5856cfSAndreas Gohr        if(!$_POST['save']) return false;
7111d5856cfSAndreas Gohr
7121d5856cfSAndreas Gohr        if (empty($_POST['login'])) {
7131d5856cfSAndreas Gohr            msg($lang['resendpwdmissing'], -1);
7141d5856cfSAndreas Gohr            return false;
7151d5856cfSAndreas Gohr        } else {
71616470b1dSchris            $_POST['login'] = preg_replace('/.*:/','',$_POST['login']);
71716470b1dSchris            $user = cleanID($_POST['login']);
7181d5856cfSAndreas Gohr        }
7191d5856cfSAndreas Gohr
7201d5856cfSAndreas Gohr        $userinfo = $auth->getUserData($user);
7211d5856cfSAndreas Gohr        if(!$userinfo['mail']) {
7221d5856cfSAndreas Gohr            msg($lang['resendpwdnouser'], -1);
7231d5856cfSAndreas Gohr            return false;
7241d5856cfSAndreas Gohr        }
7251d5856cfSAndreas Gohr
7261d5856cfSAndreas Gohr        // generate auth token
7271d5856cfSAndreas Gohr        $token = md5(auth_cookiesalt().$user); //secret but user based
7281d5856cfSAndreas Gohr        $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth';
7291d5856cfSAndreas Gohr        $url = wl('',array('do'=>'resendpwd','pwauth'=>$token),true,'&');
7301d5856cfSAndreas Gohr
7311d5856cfSAndreas Gohr        io_saveFile($tfile,$user);
7321d5856cfSAndreas Gohr
7331d5856cfSAndreas Gohr        $text = rawLocale('pwconfirm');
7341d5856cfSAndreas Gohr        $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
7351d5856cfSAndreas Gohr        $text = str_replace('@FULLNAME@',$userinfo['name'],$text);
7361d5856cfSAndreas Gohr        $text = str_replace('@LOGIN@',$user,$text);
7371d5856cfSAndreas Gohr        $text = str_replace('@TITLE@',$conf['title'],$text);
7381d5856cfSAndreas Gohr        $text = str_replace('@CONFIRM@',$url,$text);
7391d5856cfSAndreas Gohr
7401d5856cfSAndreas Gohr        if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>',
7411d5856cfSAndreas Gohr                     $lang['regpwmail'],
7421d5856cfSAndreas Gohr                     $text,
7431d5856cfSAndreas Gohr                     $conf['mailfrom'])){
7441d5856cfSAndreas Gohr            msg($lang['resendpwdconfirm'],1);
7451d5856cfSAndreas Gohr        }else{
7461d5856cfSAndreas Gohr            msg($lang['regmailfail'],-1);
7471d5856cfSAndreas Gohr        }
7481d5856cfSAndreas Gohr        return true;
7491d5856cfSAndreas Gohr    }
7501d5856cfSAndreas Gohr
7511d5856cfSAndreas Gohr    return false; // never reached
7528b06d178Schris}
7538b06d178Schris
7548b06d178Schris/**
75510a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid
75610a76f6fSfrank *
75710a76f6fSfrank * May not be completly RFC conform!
75810a76f6fSfrank *
75910a76f6fSfrank * @link    http://www.webmasterworld.com/forum88/135.htm
76010a76f6fSfrank *
76110a76f6fSfrank * @param   string $email the address to check
76210a76f6fSfrank * @return  bool          true if address is valid
76310a76f6fSfrank */
76410a76f6fSfrankfunction isvalidemail($email){
76510a76f6fSfrank  return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email);
76610a76f6fSfrank}
76710a76f6fSfrank
768b0855b11Sandi/**
769b0855b11Sandi * Encrypts a password using the given method and salt
770b0855b11Sandi *
771b0855b11Sandi * If the selected method needs a salt and none was given, a random one
772b0855b11Sandi * is chosen.
773b0855b11Sandi *
774b0855b11Sandi * The following methods are understood:
775b0855b11Sandi *
776b0855b11Sandi *   smd5  - Salted MD5 hashing
777b0855b11Sandi *   md5   - Simple MD5 hashing
778b0855b11Sandi *   sha1  - SHA1 hashing
779b0855b11Sandi *   ssha  - Salted SHA1 hashing
780d7be6245Sandi *   crypt - Unix crypt
781d7be6245Sandi *   mysql - MySQL password (old method)
782d7be6245Sandi *   my411 - MySQL 4.1.1 password
783b0855b11Sandi *
784b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
785b0855b11Sandi * @return  string  The crypted password
786b0855b11Sandi */
787b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){
788b0855b11Sandi  global $conf;
789b0855b11Sandi  if(empty($method)) $method = $conf['passcrypt'];
79010a76f6fSfrank
791b0855b11Sandi  //prepare a salt
792b0855b11Sandi  if(empty($salt)) $salt = md5(uniqid(rand(), true));
793b0855b11Sandi
794b0855b11Sandi  switch(strtolower($method)){
795b0855b11Sandi    case 'smd5':
796b0855b11Sandi        return crypt($clear,'$1$'.substr($salt,0,8).'$');
797b0855b11Sandi    case 'md5':
798b0855b11Sandi      return md5($clear);
799b0855b11Sandi    case 'sha1':
800b0855b11Sandi      return sha1($clear);
801b0855b11Sandi    case 'ssha':
802b0855b11Sandi      $salt=substr($salt,0,4);
803d6e54e02Smatthiasgrimm      return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt);
804b0855b11Sandi    case 'crypt':
805b0855b11Sandi      return crypt($clear,substr($salt,0,2));
806d7be6245Sandi    case 'mysql':
807d7be6245Sandi      //from http://www.php.net/mysql comment by <soren at byu dot edu>
808d7be6245Sandi      $nr=0x50305735;
809d7be6245Sandi      $nr2=0x12345671;
810d7be6245Sandi      $add=7;
811d7be6245Sandi      $charArr = preg_split("//", $clear);
812d7be6245Sandi      foreach ($charArr as $char) {
813d7be6245Sandi        if (($char == '') || ($char == ' ') || ($char == '\t')) continue;
814d7be6245Sandi        $charVal = ord($char);
815d7be6245Sandi        $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8);
816d7be6245Sandi        $nr2 += ($nr2 << 8) ^ $nr;
817d7be6245Sandi        $add += $charVal;
818d7be6245Sandi      }
819d7be6245Sandi      return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff));
820d7be6245Sandi    case 'my411':
821d7be6245Sandi      return '*'.sha1(pack("H*", sha1($clear)));
822b0855b11Sandi    default:
823b0855b11Sandi      msg("Unsupported crypt method $method",-1);
824b0855b11Sandi  }
825b0855b11Sandi}
826b0855b11Sandi
827b0855b11Sandi/**
828b0855b11Sandi * Verifies a cleartext password against a crypted hash
829b0855b11Sandi *
830b0855b11Sandi * The method and salt used for the crypted hash is determined automatically
831b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs
832b0855b11Sandi * match true is is returned else false
833b0855b11Sandi *
834b0855b11Sandi * @author  Andreas Gohr <andi@splitbrain.org>
835b0855b11Sandi * @return  bool
836b0855b11Sandi */
837b0855b11Sandifunction auth_verifyPassword($clear,$crypt){
838b0855b11Sandi  $method='';
839b0855b11Sandi  $salt='';
840b0855b11Sandi
841b0855b11Sandi  //determine the used method and salt
842d7be6245Sandi  $len = strlen($crypt);
843b0855b11Sandi  if(substr($crypt,0,3) == '$1$'){
844b0855b11Sandi    $method = 'smd5';
845b0855b11Sandi    $salt   = substr($crypt,3,8);
846b0855b11Sandi  }elseif(substr($crypt,0,6) == '{SSHA}'){
847b0855b11Sandi    $method = 'ssha';
848b0855b11Sandi    $salt   = substr(base64_decode(substr($crypt, 6)),20);
849d7be6245Sandi  }elseif($len == 32){
850b0855b11Sandi    $method = 'md5';
851d7be6245Sandi  }elseif($len == 40){
852b0855b11Sandi    $method = 'sha1';
853d7be6245Sandi  }elseif($len == 16){
854d7be6245Sandi    $method = 'mysql';
855d7be6245Sandi  }elseif($len == 41 && $crypt[0] == '*'){
856d7be6245Sandi    $method = 'my411';
857b0855b11Sandi  }else{
858b0855b11Sandi    $method = 'crypt';
859b0855b11Sandi    $salt   = substr($crypt,0,2);
860b0855b11Sandi  }
861b0855b11Sandi
862b0855b11Sandi  //crypt and compare
863b0855b11Sandi  if(auth_cryptPassword($clear,$method,$salt) === $crypt){
864b0855b11Sandi    return true;
865b0855b11Sandi  }
866b0855b11Sandi  return false;
867b0855b11Sandi}
868340756e4Sandi
869340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
870