xref: /dokuwiki/lib/plugins/authad/auth.php (revision 454d868b911059adb8889a2d6afefa016d6a21f5)
1f4476bd9SJan Schumann<?php
2f4476bd9SJan Schumann// must be run within Dokuwiki
3f4476bd9SJan Schumannif(!defined('DOKU_INC')) die();
4f4476bd9SJan Schumann
5f4476bd9SJan Schumannrequire_once(DOKU_INC.'inc/adLDAP.php');
6f4476bd9SJan Schumann
7f4476bd9SJan Schumann/**
8f4476bd9SJan Schumann * Active Directory authentication backend for DokuWiki
9f4476bd9SJan Schumann *
10f4476bd9SJan Schumann * This makes authentication with a Active Directory server much easier
11f4476bd9SJan Schumann * than when using the normal LDAP backend by utilizing the adLDAP library
12f4476bd9SJan Schumann *
13f4476bd9SJan Schumann * Usage:
14f4476bd9SJan Schumann *   Set DokuWiki's local.protected.php auth setting to read
15f4476bd9SJan Schumann *
16f4476bd9SJan Schumann *   $conf['useacl']         = 1;
17f4476bd9SJan Schumann *   $conf['disableactions'] = 'register';
18f4476bd9SJan Schumann *   $conf['autopasswd']     = 0;
19f4476bd9SJan Schumann *   $conf['authtype']       = 'authad';
20f4476bd9SJan Schumann *   $conf['passcrypt']      = 'ssha';
21f4476bd9SJan Schumann *
2293a7873eSAndreas Gohr *   $conf['auth']['ad']['account_suffix']     = '
2393a7873eSAndreas Gohr *
2493a7873eSAndreas Gohr * @my.domain.org';
2593a7873eSAndreas Gohr *   $conf['auth']['ad']['base_dn']            = 'DC=my,DC=domain,DC=org';
2693a7873eSAndreas Gohr *   $conf['auth']['ad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org';
27f4476bd9SJan Schumann *
28f4476bd9SJan Schumann *   //optional:
2993a7873eSAndreas Gohr *   $conf['auth']['ad']['sso']                = 1;
3093a7873eSAndreas Gohr *   $conf['auth']['ad']['ad_username']        = 'root';
3193a7873eSAndreas Gohr *   $conf['auth']['ad']['ad_password']        = 'pass';
3293a7873eSAndreas Gohr *   $conf['auth']['ad']['real_primarygroup']  = 1;
3393a7873eSAndreas Gohr *   $conf['auth']['ad']['use_ssl']            = 1;
3493a7873eSAndreas Gohr *   $conf['auth']['ad']['use_tls']            = 1;
3593a7873eSAndreas Gohr *   $conf['auth']['ad']['debug']              = 1;
3693a7873eSAndreas Gohr *   // warn user about expiring password this many days in advance:
3793a7873eSAndreas Gohr *   $conf['auth']['ad']['expirywarn']         = 5;
38f4476bd9SJan Schumann *
39f4476bd9SJan Schumann *   // get additional information to the userinfo array
40f4476bd9SJan Schumann *   // add a list of comma separated ldap contact fields.
41f4476bd9SJan Schumann *   $conf['plugin']['authad']['additional'] = 'field1,field2';
42f4476bd9SJan Schumann *
43f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
44f4476bd9SJan Schumann * @author  James Van Lommel <jamesvl@gmail.com>
45f4476bd9SJan Schumann * @link    http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/
46f4476bd9SJan Schumann * @author  Andreas Gohr <andi@splitbrain.org>
47f4476bd9SJan Schumann * @author  Jan Schumann <js@schumann-it.com>
48f4476bd9SJan Schumann */
4993a7873eSAndreas Gohr
5093a7873eSAndreas Gohrrequire_once(DOKU_INC.'inc/adLDAP/adLDAP.php');
5193a7873eSAndreas Gohr
5293a7873eSAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin {
5393a7873eSAndreas Gohr    /**
5493a7873eSAndreas Gohr     * @var array copy of the auth backend configuration
5593a7873eSAndreas Gohr     */
5693a7873eSAndreas Gohr    protected $cnf = array();
5793a7873eSAndreas Gohr    /**
5893a7873eSAndreas Gohr     * @var array hold connection data for a specific AD domain
5993a7873eSAndreas Gohr     */
6093a7873eSAndreas Gohr    protected $opts = array();
6193a7873eSAndreas Gohr    /**
6293a7873eSAndreas Gohr     * @var array open connections for each AD domain, as adLDAP objects
6393a7873eSAndreas Gohr     */
6493a7873eSAndreas Gohr    protected $adldap = array();
6593a7873eSAndreas Gohr
6693a7873eSAndreas Gohr    /**
6793a7873eSAndreas Gohr     * @var bool message state
6893a7873eSAndreas Gohr     */
6993a7873eSAndreas Gohr    protected $msgshown = false;
7093a7873eSAndreas Gohr
7193a7873eSAndreas Gohr    /**
7293a7873eSAndreas Gohr     * @var array user listing cache
7393a7873eSAndreas Gohr     */
7493a7873eSAndreas Gohr    protected $users = array();
7593a7873eSAndreas Gohr
7693a7873eSAndreas Gohr    /**
7793a7873eSAndreas Gohr     * @var array filter patterns for listing users
7893a7873eSAndreas Gohr     */
7993a7873eSAndreas Gohr    protected $_pattern = array();
80f4476bd9SJan Schumann
81f4476bd9SJan Schumann    /**
82f4476bd9SJan Schumann     * Constructor
83f4476bd9SJan Schumann     */
8493a7873eSAndreas Gohr    public function __construct() {
85*454d868bSAndreas Gohr        parent::__construct();
86*454d868bSAndreas Gohr
87f4476bd9SJan Schumann        global $conf;
88f4476bd9SJan Schumann        $this->cnf = $conf['auth']['ad'];
89f4476bd9SJan Schumann
90f4476bd9SJan Schumann        // additional information fields
91f4476bd9SJan Schumann        if(isset($this->cnf['additional'])) {
92f4476bd9SJan Schumann            $this->cnf['additional'] = str_replace(' ', '', $this->cnf['additional']);
93f4476bd9SJan Schumann            $this->cnf['additional'] = explode(',', $this->cnf['additional']);
94f4476bd9SJan Schumann        } else $this->cnf['additional'] = array();
95f4476bd9SJan Schumann
96f4476bd9SJan Schumann        // ldap extension is needed
97f4476bd9SJan Schumann        if(!function_exists('ldap_connect')) {
98f4476bd9SJan Schumann            if($this->cnf['debug'])
99f4476bd9SJan Schumann                msg("AD Auth: PHP LDAP extension not found.", -1);
100f4476bd9SJan Schumann            $this->success = false;
101f4476bd9SJan Schumann            return;
102f4476bd9SJan Schumann        }
103f4476bd9SJan Schumann
104f4476bd9SJan Schumann        // Prepare SSO
10593a7873eSAndreas Gohr        if(!utf8_check($_SERVER['REMOTE_USER'])) {
10693a7873eSAndreas Gohr            $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']);
10793a7873eSAndreas Gohr        }
108f4476bd9SJan Schumann        if($_SERVER['REMOTE_USER'] && $this->cnf['sso']) {
10993a7873eSAndreas Gohr            $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']);
110f4476bd9SJan Schumann
111f4476bd9SJan Schumann            // we need to simulate a login
112f4476bd9SJan Schumann            if(empty($_COOKIE[DOKU_COOKIE])) {
113f4476bd9SJan Schumann                $_REQUEST['u'] = $_SERVER['REMOTE_USER'];
114f4476bd9SJan Schumann                $_REQUEST['p'] = 'sso_only';
115f4476bd9SJan Schumann            }
116f4476bd9SJan Schumann        }
117f4476bd9SJan Schumann
11893a7873eSAndreas Gohr        // other can do's are changed in $this->_loadServerConfig() base on domain setup
119f4476bd9SJan Schumann        $this->cando['modName'] = true;
120f4476bd9SJan Schumann        $this->cando['modMail'] = true;
121f4476bd9SJan Schumann    }
122f4476bd9SJan Schumann
123f4476bd9SJan Schumann    /**
124f4476bd9SJan Schumann     * Check user+password [required auth function]
125f4476bd9SJan Schumann     *
126f4476bd9SJan Schumann     * Checks if the given user exists and the given
127f4476bd9SJan Schumann     * plaintext password is correct by trying to bind
128f4476bd9SJan Schumann     * to the LDAP server
129f4476bd9SJan Schumann     *
130f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
13193a7873eSAndreas Gohr     * @param string $user
13293a7873eSAndreas Gohr     * @param string $pass
133f4476bd9SJan Schumann     * @return  bool
134f4476bd9SJan Schumann     */
13593a7873eSAndreas Gohr    public function checkPass($user, $pass) {
136f4476bd9SJan Schumann        if($_SERVER['REMOTE_USER'] &&
137f4476bd9SJan Schumann            $_SERVER['REMOTE_USER'] == $user &&
13893a7873eSAndreas Gohr            $this->cnf['sso']
13993a7873eSAndreas Gohr        ) return true;
140f4476bd9SJan Schumann
14193a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
14293a7873eSAndreas Gohr        if(!$adldap) return false;
14393a7873eSAndreas Gohr
14493a7873eSAndreas Gohr        return $adldap->authenticate($this->_userName($user), $pass);
145f4476bd9SJan Schumann    }
146f4476bd9SJan Schumann
147f4476bd9SJan Schumann    /**
148f4476bd9SJan Schumann     * Return user info [required auth function]
149f4476bd9SJan Schumann     *
150f4476bd9SJan Schumann     * Returns info about the given user needs to contain
151f4476bd9SJan Schumann     * at least these fields:
152f4476bd9SJan Schumann     *
153f4476bd9SJan Schumann     * name    string  full name of the user
154f4476bd9SJan Schumann     * mail    string  email address of the user
155f4476bd9SJan Schumann     * grps    array   list of groups the user is in
156f4476bd9SJan Schumann     *
15793a7873eSAndreas Gohr     * This AD specific function returns the following
158f4476bd9SJan Schumann     * addional fields:
159f4476bd9SJan Schumann     *
160f4476bd9SJan Schumann     * dn         string    distinguished name (DN)
16193a7873eSAndreas Gohr     * uid        string    samaccountname
16293a7873eSAndreas Gohr     * lastpwd    int       timestamp of the date when the password was set
16393a7873eSAndreas Gohr     * expires    true      if the password expires
16493a7873eSAndreas Gohr     * expiresin  int       seconds until the password expires
16593a7873eSAndreas Gohr     * any fields specified in the 'additional' config option
166f4476bd9SJan Schumann     *
167f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
16893a7873eSAndreas Gohr     * @param string $user
16993a7873eSAndreas Gohr     * @return array
170f4476bd9SJan Schumann     */
17193a7873eSAndreas Gohr    public function getUserData($user) {
172f4476bd9SJan Schumann        global $conf;
17393a7873eSAndreas Gohr        global $lang;
17493a7873eSAndreas Gohr        global $ID;
17593a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
17693a7873eSAndreas Gohr        if(!$adldap) return false;
177f4476bd9SJan Schumann
17893a7873eSAndreas Gohr        if($user == '') return array();
17993a7873eSAndreas Gohr
18093a7873eSAndreas Gohr        $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol');
181f4476bd9SJan Schumann
182f4476bd9SJan Schumann        // add additional fields to read
183f4476bd9SJan Schumann        $fields = array_merge($fields, $this->cnf['additional']);
184f4476bd9SJan Schumann        $fields = array_unique($fields);
185f4476bd9SJan Schumann
186f4476bd9SJan Schumann        //get info for given user
18793a7873eSAndreas Gohr        $result = $this->adldap->user()->info($this->_userName($user), $fields);
18893a7873eSAndreas Gohr        if($result == false){
18993a7873eSAndreas Gohr            return array();
19093a7873eSAndreas Gohr        }
19193a7873eSAndreas Gohr
192f4476bd9SJan Schumann        //general user info
193f4476bd9SJan Schumann        $info['name'] = $result[0]['displayname'][0];
194f4476bd9SJan Schumann        $info['mail'] = $result[0]['mail'][0];
195f4476bd9SJan Schumann        $info['uid']  = $result[0]['samaccountname'][0];
196f4476bd9SJan Schumann        $info['dn']   = $result[0]['dn'];
19793a7873eSAndreas Gohr        //last password set (Windows counts from January 1st 1601)
19893a7873eSAndreas Gohr        $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600;
19993a7873eSAndreas Gohr        //will it expire?
20093a7873eSAndreas Gohr        $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
201f4476bd9SJan Schumann
202f4476bd9SJan Schumann        // additional information
203f4476bd9SJan Schumann        foreach($this->cnf['additional'] as $field) {
204f4476bd9SJan Schumann            if(isset($result[0][strtolower($field)])) {
205f4476bd9SJan Schumann                $info[$field] = $result[0][strtolower($field)][0];
206f4476bd9SJan Schumann            }
207f4476bd9SJan Schumann        }
208f4476bd9SJan Schumann
209f4476bd9SJan Schumann        // handle ActiveDirectory memberOf
21093a7873eSAndreas Gohr        $info['grps'] = $this->adldap->user()->groups($this->_userName($user),(bool) $this->opts['recursive_groups']);
211f4476bd9SJan Schumann
212f4476bd9SJan Schumann        if(is_array($info['grps'])) {
213f4476bd9SJan Schumann            foreach($info['grps'] as $ndx => $group) {
214f4476bd9SJan Schumann                $info['grps'][$ndx] = $this->cleanGroup($group);
215f4476bd9SJan Schumann            }
216f4476bd9SJan Schumann        }
217f4476bd9SJan Schumann
218f4476bd9SJan Schumann        // always add the default group to the list of groups
219f4476bd9SJan Schumann        if(!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
220f4476bd9SJan Schumann            $info['grps'][] = $conf['defaultgroup'];
221f4476bd9SJan Schumann        }
222f4476bd9SJan Schumann
22393a7873eSAndreas Gohr        // add the user's domain to the groups
22493a7873eSAndreas Gohr        $domain = $this->_userDomain($user);
22593a7873eSAndreas Gohr        if($domain && !in_array("domain-$domain", (array) $info['grps'])) {
22693a7873eSAndreas Gohr            $info['grps'][] = $this->cleanGroup("domain-$domain");
22793a7873eSAndreas Gohr        }
22893a7873eSAndreas Gohr
22993a7873eSAndreas Gohr        // check expiry time
23093a7873eSAndreas Gohr        if($info['expires'] && $this->cnf['expirywarn']){
23193a7873eSAndreas Gohr            $timeleft = $this->adldap->user()->passwordExpiry($user); // returns unixtime
23293a7873eSAndreas Gohr            $timeleft = round($timeleft/(24*60*60));
23393a7873eSAndreas Gohr            $info['expiresin'] = $timeleft;
23493a7873eSAndreas Gohr
23593a7873eSAndreas Gohr            // if this is the current user, warn him (once per request only)
23693a7873eSAndreas Gohr            if(($_SERVER['REMOTE_USER'] == $user) &&
23793a7873eSAndreas Gohr                ($timeleft <= $this->cnf['expirywarn']) &&
23893a7873eSAndreas Gohr                !$this->msgshown
23993a7873eSAndreas Gohr            ) {
24093a7873eSAndreas Gohr                $msg = sprintf($lang['authpwdexpire'], $timeleft);
24193a7873eSAndreas Gohr                if($this->canDo('modPass')) {
24293a7873eSAndreas Gohr                    $url = wl($ID, array('do'=> 'profile'));
24393a7873eSAndreas Gohr                    $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
24493a7873eSAndreas Gohr                }
24593a7873eSAndreas Gohr                msg($msg);
24693a7873eSAndreas Gohr                $this->msgshown = true;
24793a7873eSAndreas Gohr            }
24893a7873eSAndreas Gohr        }
24993a7873eSAndreas Gohr
250f4476bd9SJan Schumann        return $info;
251f4476bd9SJan Schumann    }
252f4476bd9SJan Schumann
253f4476bd9SJan Schumann    /**
254f4476bd9SJan Schumann     * Make AD group names usable by DokuWiki.
255f4476bd9SJan Schumann     *
256f4476bd9SJan Schumann     * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
257f4476bd9SJan Schumann     *
258f4476bd9SJan Schumann     * @author  James Van Lommel (jamesvl@gmail.com)
25993a7873eSAndreas Gohr     * @param string $group
26093a7873eSAndreas Gohr     * @return string
261f4476bd9SJan Schumann     */
26293a7873eSAndreas Gohr    public function cleanGroup($group) {
26393a7873eSAndreas Gohr        $group = str_replace('\\', '', $group);
26493a7873eSAndreas Gohr        $group = str_replace('#', '', $group);
26593a7873eSAndreas Gohr        $group = preg_replace('[\s]', '_', $group);
26693a7873eSAndreas Gohr        $group = utf8_strtolower(trim($group));
26793a7873eSAndreas Gohr        return $group;
268f4476bd9SJan Schumann    }
269f4476bd9SJan Schumann
270f4476bd9SJan Schumann    /**
271f4476bd9SJan Schumann     * Sanitize user names
27293a7873eSAndreas Gohr     *
27393a7873eSAndreas Gohr     * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
27493a7873eSAndreas Gohr     *
27593a7873eSAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
27693a7873eSAndreas Gohr     * @param string $user
27793a7873eSAndreas Gohr     * @return string
278f4476bd9SJan Schumann     */
27993a7873eSAndreas Gohr    public function cleanUser($user) {
28093a7873eSAndreas Gohr        $domain = '';
28193a7873eSAndreas Gohr
28293a7873eSAndreas Gohr        // get NTLM or Kerberos domain part
28393a7873eSAndreas Gohr        list($dom, $user) = explode('\\', $user, 2);
28493a7873eSAndreas Gohr        if(!$user) $user = $dom;
28593a7873eSAndreas Gohr        if($dom) $domain = $dom;
28693a7873eSAndreas Gohr        list($user, $dom) = explode('@', $user, 2);
28793a7873eSAndreas Gohr        if($dom) $domain = $dom;
28893a7873eSAndreas Gohr
28993a7873eSAndreas Gohr        // clean up both
29093a7873eSAndreas Gohr        $domain = utf8_strtolower(trim($domain));
29193a7873eSAndreas Gohr        $user   = utf8_strtolower(trim($user));
29293a7873eSAndreas Gohr
29393a7873eSAndreas Gohr        // is this a known, valid domain? if not discard
29493a7873eSAndreas Gohr        if(!is_array($this->cnf[$domain])) {
29593a7873eSAndreas Gohr            $domain = '';
29693a7873eSAndreas Gohr        }
29793a7873eSAndreas Gohr
29893a7873eSAndreas Gohr        // reattach domain
29993a7873eSAndreas Gohr        if($domain) $user = "$user@$domain";
30093a7873eSAndreas Gohr        return $user;
301f4476bd9SJan Schumann    }
302f4476bd9SJan Schumann
303f4476bd9SJan Schumann    /**
304f4476bd9SJan Schumann     * Most values in LDAP are case-insensitive
30593a7873eSAndreas Gohr     *
30693a7873eSAndreas Gohr     * @return bool
307f4476bd9SJan Schumann     */
30893a7873eSAndreas Gohr    public function isCaseSensitive() {
309f4476bd9SJan Schumann        return false;
310f4476bd9SJan Schumann    }
311f4476bd9SJan Schumann
312f4476bd9SJan Schumann    /**
313f4476bd9SJan Schumann     * Bulk retrieval of user data
314f4476bd9SJan Schumann     *
315f4476bd9SJan Schumann     * @author  Dominik Eckelmann <dokuwiki@cosmocode.de>
31693a7873eSAndreas Gohr     * @param   int   $start     index of first user to be returned
31793a7873eSAndreas Gohr     * @param   int   $limit     max number of users to be returned
31893a7873eSAndreas Gohr     * @param   array $filter    array of field/pattern pairs, null for no filter
31993a7873eSAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
320f4476bd9SJan Schumann     */
32193a7873eSAndreas Gohr    public function retrieveUsers($start = 0, $limit = -1, $filter = array()) {
32293a7873eSAndreas Gohr        $adldap = $this->_adldap(null);
32393a7873eSAndreas Gohr        if(!$adldap) return false;
324f4476bd9SJan Schumann
325f4476bd9SJan Schumann        if($this->users === null) {
326f4476bd9SJan Schumann            //get info for given user
32793a7873eSAndreas Gohr            $result = $this->adldap->user()->all();
328f4476bd9SJan Schumann            if (!$result) return array();
329f4476bd9SJan Schumann            $this->users = array_fill_keys($result, false);
330f4476bd9SJan Schumann        }
331f4476bd9SJan Schumann
332f4476bd9SJan Schumann        $i     = 0;
333f4476bd9SJan Schumann        $count = 0;
334f4476bd9SJan Schumann        $this->_constructPattern($filter);
335f4476bd9SJan Schumann        $result = array();
336f4476bd9SJan Schumann
337f4476bd9SJan Schumann        foreach($this->users as $user => &$info) {
338f4476bd9SJan Schumann            if($i++ < $start) {
339f4476bd9SJan Schumann                continue;
340f4476bd9SJan Schumann            }
341f4476bd9SJan Schumann            if($info === false) {
342f4476bd9SJan Schumann                $info = $this->getUserData($user);
343f4476bd9SJan Schumann            }
344f4476bd9SJan Schumann            if($this->_filter($user, $info)) {
345f4476bd9SJan Schumann                $result[$user] = $info;
346f4476bd9SJan Schumann                if(($limit >= 0) && (++$count >= $limit)) break;
347f4476bd9SJan Schumann            }
348f4476bd9SJan Schumann        }
349f4476bd9SJan Schumann        return $result;
350f4476bd9SJan Schumann    }
351f4476bd9SJan Schumann
352f4476bd9SJan Schumann    /**
353f4476bd9SJan Schumann     * Modify user data
354f4476bd9SJan Schumann     *
35593a7873eSAndreas Gohr     * @param   string $user      nick of the user to be changed
35693a7873eSAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed
357f4476bd9SJan Schumann     * @return  bool
358f4476bd9SJan Schumann     */
35993a7873eSAndreas Gohr    public function modifyUser($user, $changes) {
360f4476bd9SJan Schumann        $return = true;
36193a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
36293a7873eSAndreas Gohr        if(!$adldap) return false;
363f4476bd9SJan Schumann
364f4476bd9SJan Schumann        // password changing
365f4476bd9SJan Schumann        if(isset($changes['pass'])) {
366f4476bd9SJan Schumann            try {
36793a7873eSAndreas Gohr                $return = $this->adldap->user()->password($this->_userName($user),$changes['pass']);
368f4476bd9SJan Schumann            } catch (adLDAPException $e) {
369f4476bd9SJan Schumann                if ($this->cnf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
370f4476bd9SJan Schumann                $return = false;
371f4476bd9SJan Schumann            }
372f4476bd9SJan Schumann            if(!$return) msg('AD Auth: failed to change the password. Maybe the password policy was not met?', -1);
373f4476bd9SJan Schumann        }
374f4476bd9SJan Schumann
375f4476bd9SJan Schumann        // changing user data
376f4476bd9SJan Schumann        $adchanges = array();
377f4476bd9SJan Schumann        if(isset($changes['name'])) {
378f4476bd9SJan Schumann            // get first and last name
379f4476bd9SJan Schumann            $parts                     = explode(' ', $changes['name']);
380f4476bd9SJan Schumann            $adchanges['surname']      = array_pop($parts);
381f4476bd9SJan Schumann            $adchanges['firstname']    = join(' ', $parts);
382f4476bd9SJan Schumann            $adchanges['display_name'] = $changes['name'];
383f4476bd9SJan Schumann        }
384f4476bd9SJan Schumann        if(isset($changes['mail'])) {
385f4476bd9SJan Schumann            $adchanges['email'] = $changes['mail'];
386f4476bd9SJan Schumann        }
387f4476bd9SJan Schumann        if(count($adchanges)) {
388f4476bd9SJan Schumann            try {
38993a7873eSAndreas Gohr                $return = $return & $this->adldap->user()->modify($this->_userName($user),$adchanges);
390f4476bd9SJan Schumann            } catch (adLDAPException $e) {
391f4476bd9SJan Schumann                if ($this->cnf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
392f4476bd9SJan Schumann                $return = false;
393f4476bd9SJan Schumann            }
394f4476bd9SJan Schumann        }
395f4476bd9SJan Schumann
396f4476bd9SJan Schumann        return $return;
397f4476bd9SJan Schumann    }
398f4476bd9SJan Schumann
399f4476bd9SJan Schumann    /**
400f4476bd9SJan Schumann     * Initialize the AdLDAP library and connect to the server
40193a7873eSAndreas Gohr     *
40293a7873eSAndreas Gohr     * When you pass null as domain, it will reuse any existing domain.
40393a7873eSAndreas Gohr     * Eg. the one of the logged in user. It falls back to the default
40493a7873eSAndreas Gohr     * domain if no current one is available.
40593a7873eSAndreas Gohr     *
40693a7873eSAndreas Gohr     * @param string|null $domain The AD domain to use
40793a7873eSAndreas Gohr     * @return adLDAP|bool true if a connection was established
408f4476bd9SJan Schumann     */
40993a7873eSAndreas Gohr    protected function _adldap($domain) {
41093a7873eSAndreas Gohr        if(is_null($domain) && is_array($this->opts)) {
41193a7873eSAndreas Gohr            $domain = $this->opts['domain'];
41293a7873eSAndreas Gohr        }
41393a7873eSAndreas Gohr
41493a7873eSAndreas Gohr        $this->opts = $this->_loadServerConfig((string) $domain);
41593a7873eSAndreas Gohr        if(isset($this->adldap[$domain])) return $this->adldap[$domain];
416f4476bd9SJan Schumann
417f4476bd9SJan Schumann        // connect
418f4476bd9SJan Schumann        try {
41993a7873eSAndreas Gohr            $this->adldap[$domain] = new adLDAP($this->opts);
42093a7873eSAndreas Gohr            return $this->adldap[$domain];
421f4476bd9SJan Schumann        } catch(adLDAPException $e) {
422f4476bd9SJan Schumann            if($this->cnf['debug']) {
423f4476bd9SJan Schumann                msg('AD Auth: '.$e->getMessage(), -1);
424f4476bd9SJan Schumann            }
425f4476bd9SJan Schumann            $this->success         = false;
42693a7873eSAndreas Gohr            $this->adldap[$domain] = null;
427f4476bd9SJan Schumann        }
428f4476bd9SJan Schumann        return false;
429f4476bd9SJan Schumann    }
430f4476bd9SJan Schumann
431f4476bd9SJan Schumann    /**
43293a7873eSAndreas Gohr     * Get the domain part from a user
433f4476bd9SJan Schumann     *
43493a7873eSAndreas Gohr     * @param $user
43593a7873eSAndreas Gohr     * @return string
436f4476bd9SJan Schumann     */
43793a7873eSAndreas Gohr    public function _userDomain($user) {
43893a7873eSAndreas Gohr        list(, $domain) = explode('@', $user, 2);
43993a7873eSAndreas Gohr        return $domain;
440f4476bd9SJan Schumann    }
441f4476bd9SJan Schumann
44293a7873eSAndreas Gohr    /**
44393a7873eSAndreas Gohr     * Get the user part from a user
44493a7873eSAndreas Gohr     *
44593a7873eSAndreas Gohr     * @param $user
44693a7873eSAndreas Gohr     * @return string
44793a7873eSAndreas Gohr     */
44893a7873eSAndreas Gohr    public function _userName($user) {
44993a7873eSAndreas Gohr        list($name) = explode('@', $user, 2);
45093a7873eSAndreas Gohr        return $name;
45193a7873eSAndreas Gohr    }
45293a7873eSAndreas Gohr
45393a7873eSAndreas Gohr    /**
45493a7873eSAndreas Gohr     * Fetch the configuration for the given AD domain
45593a7873eSAndreas Gohr     *
45693a7873eSAndreas Gohr     * @param string $domain current AD domain
45793a7873eSAndreas Gohr     * @return array
45893a7873eSAndreas Gohr     */
45993a7873eSAndreas Gohr    protected function _loadServerConfig($domain) {
46093a7873eSAndreas Gohr        // prepare adLDAP standard configuration
46193a7873eSAndreas Gohr        $opts = $this->cnf;
46293a7873eSAndreas Gohr
46393a7873eSAndreas Gohr        $opts['domain'] = $domain;
46493a7873eSAndreas Gohr
46593a7873eSAndreas Gohr        // add possible domain specific configuration
46693a7873eSAndreas Gohr        if($domain && is_array($this->cnf[$domain])) foreach($this->cnf[$domain] as $key => $val) {
46793a7873eSAndreas Gohr            $opts[$key] = $val;
46893a7873eSAndreas Gohr        }
46993a7873eSAndreas Gohr
47093a7873eSAndreas Gohr        // handle multiple AD servers
47193a7873eSAndreas Gohr        $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
47293a7873eSAndreas Gohr        $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
47393a7873eSAndreas Gohr        $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
47493a7873eSAndreas Gohr
47593a7873eSAndreas Gohr        // we can change the password if SSL is set
47693a7873eSAndreas Gohr        if($opts['use_ssl'] || $opts['use_tls']) {
47793a7873eSAndreas Gohr            $this->cando['modPass'] = true;
47893a7873eSAndreas Gohr        } else {
47993a7873eSAndreas Gohr            $this->cando['modPass'] = false;
48093a7873eSAndreas Gohr        }
48193a7873eSAndreas Gohr
48293a7873eSAndreas Gohr        if(isset($opts['ad_username']) && isset($opts['ad_password'])) {
48393a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
48493a7873eSAndreas Gohr        } else {
48593a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
48693a7873eSAndreas Gohr        }
48793a7873eSAndreas Gohr
48893a7873eSAndreas Gohr        return $opts;
48993a7873eSAndreas Gohr    }
49093a7873eSAndreas Gohr
49193a7873eSAndreas Gohr    /**
49293a7873eSAndreas Gohr     * Check provided user and userinfo for matching patterns
49393a7873eSAndreas Gohr     *
49493a7873eSAndreas Gohr     * The patterns are set up with $this->_constructPattern()
49593a7873eSAndreas Gohr     *
49693a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
49793a7873eSAndreas Gohr     * @param string $user
49893a7873eSAndreas Gohr     * @param array  $info
49993a7873eSAndreas Gohr     * @return bool
50093a7873eSAndreas Gohr     */
50193a7873eSAndreas Gohr    protected function _filter($user, $info) {
50293a7873eSAndreas Gohr        foreach($this->_pattern as $item => $pattern) {
50393a7873eSAndreas Gohr            if($item == 'user') {
50493a7873eSAndreas Gohr                if(!preg_match($pattern, $user)) return false;
50593a7873eSAndreas Gohr            } else if($item == 'grps') {
50693a7873eSAndreas Gohr                if(!count(preg_grep($pattern, $info['grps']))) return false;
50793a7873eSAndreas Gohr            } else {
50893a7873eSAndreas Gohr                if(!preg_match($pattern, $info[$item])) return false;
50993a7873eSAndreas Gohr            }
51093a7873eSAndreas Gohr        }
51193a7873eSAndreas Gohr        return true;
51293a7873eSAndreas Gohr    }
51393a7873eSAndreas Gohr
51493a7873eSAndreas Gohr    /**
51593a7873eSAndreas Gohr     * Create a pattern for $this->_filter()
51693a7873eSAndreas Gohr     *
51793a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
51893a7873eSAndreas Gohr     * @param array $filter
51993a7873eSAndreas Gohr     */
52093a7873eSAndreas Gohr    protected function _constructPattern($filter) {
521f4476bd9SJan Schumann        $this->_pattern = array();
522f4476bd9SJan Schumann        foreach($filter as $item => $pattern) {
523f4476bd9SJan Schumann            $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
524f4476bd9SJan Schumann        }
525f4476bd9SJan Schumann    }
526f4476bd9SJan Schumann}
527