xref: /dokuwiki/lib/plugins/authad/auth.php (revision 32fd494aaeb28179d8823354e07c1399e7443c90)
1f4476bd9SJan Schumann<?php
2f4476bd9SJan Schumann// must be run within Dokuwiki
3f4476bd9SJan Schumannif(!defined('DOKU_INC')) die();
4f4476bd9SJan Schumann
5*32fd494aSAndreas Gohrrequire_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php');
6*32fd494aSAndreas Gohr
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['authtype']       = 'authad';
17f4476bd9SJan Schumann *
18*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['account_suffix']     = '@my.domain.org';
19*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['base_dn']            = 'DC=my,DC=domain,DC=org';
20*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org';
21f4476bd9SJan Schumann *
22f4476bd9SJan Schumann *   //optional:
23*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['sso']                = 1;
24*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['ad_username']        = 'root';
25*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['ad_password']        = 'pass';
26*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['real_primarygroup']  = 1;
27*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_ssl']            = 1;
28*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['use_tls']            = 1;
29*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['debug']              = 1;
3093a7873eSAndreas Gohr *   // warn user about expiring password this many days in advance:
31*32fd494aSAndreas Gohr *   $conf['plugin']['authad']['expirywarn']         = 5;
32f4476bd9SJan Schumann *
33f4476bd9SJan Schumann *   // get additional information to the userinfo array
34f4476bd9SJan Schumann *   // add a list of comma separated ldap contact fields.
35f4476bd9SJan Schumann *   $conf['plugin']['authad']['additional'] = 'field1,field2';
36f4476bd9SJan Schumann *
37f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
38f4476bd9SJan Schumann * @author  James Van Lommel <jamesvl@gmail.com>
39f4476bd9SJan Schumann * @link    http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/
40f4476bd9SJan Schumann * @author  Andreas Gohr <andi@splitbrain.org>
41f4476bd9SJan Schumann * @author  Jan Schumann <js@schumann-it.com>
42f4476bd9SJan Schumann */
4393a7873eSAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin {
44*32fd494aSAndreas Gohr
4593a7873eSAndreas Gohr    /**
4693a7873eSAndreas Gohr     * @var array hold connection data for a specific AD domain
4793a7873eSAndreas Gohr     */
4893a7873eSAndreas Gohr    protected $opts = array();
49*32fd494aSAndreas Gohr
5093a7873eSAndreas Gohr    /**
5193a7873eSAndreas Gohr     * @var array open connections for each AD domain, as adLDAP objects
5293a7873eSAndreas Gohr     */
5393a7873eSAndreas Gohr    protected $adldap = array();
5493a7873eSAndreas Gohr
5593a7873eSAndreas Gohr    /**
5693a7873eSAndreas Gohr     * @var bool message state
5793a7873eSAndreas Gohr     */
5893a7873eSAndreas Gohr    protected $msgshown = false;
5993a7873eSAndreas Gohr
6093a7873eSAndreas Gohr    /**
6193a7873eSAndreas Gohr     * @var array user listing cache
6293a7873eSAndreas Gohr     */
6393a7873eSAndreas Gohr    protected $users = array();
6493a7873eSAndreas Gohr
6593a7873eSAndreas Gohr    /**
6693a7873eSAndreas Gohr     * @var array filter patterns for listing users
6793a7873eSAndreas Gohr     */
6893a7873eSAndreas Gohr    protected $_pattern = array();
69f4476bd9SJan Schumann
70f4476bd9SJan Schumann    /**
71f4476bd9SJan Schumann     * Constructor
72f4476bd9SJan Schumann     */
7393a7873eSAndreas Gohr    public function __construct() {
74454d868bSAndreas Gohr        parent::__construct();
75454d868bSAndreas Gohr
76*32fd494aSAndreas Gohr        // we load the config early to modify it a bit here
77*32fd494aSAndreas Gohr        $this->loadConfig();
78f4476bd9SJan Schumann
79f4476bd9SJan Schumann        // additional information fields
80*32fd494aSAndreas Gohr        if(isset($this->conf['additional'])) {
81*32fd494aSAndreas Gohr            $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']);
82*32fd494aSAndreas Gohr            $this->conf['additional'] = explode(',', $this->conf['additional']);
83*32fd494aSAndreas Gohr        } else $this->conf['additional'] = array();
84f4476bd9SJan Schumann
85f4476bd9SJan Schumann        // ldap extension is needed
86f4476bd9SJan Schumann        if(!function_exists('ldap_connect')) {
87*32fd494aSAndreas Gohr            if($this->conf['debug'])
88f4476bd9SJan Schumann                msg("AD Auth: PHP LDAP extension not found.", -1);
89f4476bd9SJan Schumann            $this->success = false;
90f4476bd9SJan Schumann            return;
91f4476bd9SJan Schumann        }
92f4476bd9SJan Schumann
93f4476bd9SJan Schumann        // Prepare SSO
9493a7873eSAndreas Gohr        if(!utf8_check($_SERVER['REMOTE_USER'])) {
9593a7873eSAndreas Gohr            $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']);
9693a7873eSAndreas Gohr        }
97*32fd494aSAndreas Gohr        if($_SERVER['REMOTE_USER'] && $this->conf['sso']) {
9893a7873eSAndreas Gohr            $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']);
99f4476bd9SJan Schumann
100f4476bd9SJan Schumann            // we need to simulate a login
101f4476bd9SJan Schumann            if(empty($_COOKIE[DOKU_COOKIE])) {
102f4476bd9SJan Schumann                $_REQUEST['u'] = $_SERVER['REMOTE_USER'];
103f4476bd9SJan Schumann                $_REQUEST['p'] = 'sso_only';
104f4476bd9SJan Schumann            }
105f4476bd9SJan Schumann        }
106f4476bd9SJan Schumann
10793a7873eSAndreas Gohr        // other can do's are changed in $this->_loadServerConfig() base on domain setup
108f4476bd9SJan Schumann        $this->cando['modName'] = true;
109f4476bd9SJan Schumann        $this->cando['modMail'] = true;
110f4476bd9SJan Schumann    }
111f4476bd9SJan Schumann
112f4476bd9SJan Schumann    /**
113f4476bd9SJan Schumann     * Check user+password [required auth function]
114f4476bd9SJan Schumann     *
115f4476bd9SJan Schumann     * Checks if the given user exists and the given
116f4476bd9SJan Schumann     * plaintext password is correct by trying to bind
117f4476bd9SJan Schumann     * to the LDAP server
118f4476bd9SJan Schumann     *
119f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
12093a7873eSAndreas Gohr     * @param string $user
12193a7873eSAndreas Gohr     * @param string $pass
122f4476bd9SJan Schumann     * @return  bool
123f4476bd9SJan Schumann     */
12493a7873eSAndreas Gohr    public function checkPass($user, $pass) {
125f4476bd9SJan Schumann        if($_SERVER['REMOTE_USER'] &&
126f4476bd9SJan Schumann            $_SERVER['REMOTE_USER'] == $user &&
127*32fd494aSAndreas Gohr            $this->conf['sso']
12893a7873eSAndreas Gohr        ) return true;
129f4476bd9SJan Schumann
13093a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
13193a7873eSAndreas Gohr        if(!$adldap) return false;
13293a7873eSAndreas Gohr
13393a7873eSAndreas Gohr        return $adldap->authenticate($this->_userName($user), $pass);
134f4476bd9SJan Schumann    }
135f4476bd9SJan Schumann
136f4476bd9SJan Schumann    /**
137f4476bd9SJan Schumann     * Return user info [required auth function]
138f4476bd9SJan Schumann     *
139f4476bd9SJan Schumann     * Returns info about the given user needs to contain
140f4476bd9SJan Schumann     * at least these fields:
141f4476bd9SJan Schumann     *
142f4476bd9SJan Schumann     * name    string  full name of the user
143f4476bd9SJan Schumann     * mail    string  email address of the user
144f4476bd9SJan Schumann     * grps    array   list of groups the user is in
145f4476bd9SJan Schumann     *
14693a7873eSAndreas Gohr     * This AD specific function returns the following
147f4476bd9SJan Schumann     * addional fields:
148f4476bd9SJan Schumann     *
149f4476bd9SJan Schumann     * dn         string    distinguished name (DN)
15093a7873eSAndreas Gohr     * uid        string    samaccountname
15193a7873eSAndreas Gohr     * lastpwd    int       timestamp of the date when the password was set
15293a7873eSAndreas Gohr     * expires    true      if the password expires
15393a7873eSAndreas Gohr     * expiresin  int       seconds until the password expires
15493a7873eSAndreas Gohr     * any fields specified in the 'additional' config option
155f4476bd9SJan Schumann     *
156f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
15793a7873eSAndreas Gohr     * @param string $user
15893a7873eSAndreas Gohr     * @return array
159f4476bd9SJan Schumann     */
16093a7873eSAndreas Gohr    public function getUserData($user) {
161f4476bd9SJan Schumann        global $conf;
16293a7873eSAndreas Gohr        global $lang;
16393a7873eSAndreas Gohr        global $ID;
16493a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
16593a7873eSAndreas Gohr        if(!$adldap) return false;
166f4476bd9SJan Schumann
16793a7873eSAndreas Gohr        if($user == '') return array();
16893a7873eSAndreas Gohr
16993a7873eSAndreas Gohr        $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol');
170f4476bd9SJan Schumann
171f4476bd9SJan Schumann        // add additional fields to read
172*32fd494aSAndreas Gohr        $fields = array_merge($fields, $this->conf['additional']);
173f4476bd9SJan Schumann        $fields = array_unique($fields);
174f4476bd9SJan Schumann
175f4476bd9SJan Schumann        //get info for given user
176*32fd494aSAndreas Gohr        $result = $adldap->user()->info($this->_userName($user), $fields);
17793a7873eSAndreas Gohr        if($result == false){
17893a7873eSAndreas Gohr            return array();
17993a7873eSAndreas Gohr        }
18093a7873eSAndreas Gohr
181f4476bd9SJan Schumann        //general user info
182f4476bd9SJan Schumann        $info['name'] = $result[0]['displayname'][0];
183f4476bd9SJan Schumann        $info['mail'] = $result[0]['mail'][0];
184f4476bd9SJan Schumann        $info['uid']  = $result[0]['samaccountname'][0];
185f4476bd9SJan Schumann        $info['dn']   = $result[0]['dn'];
18693a7873eSAndreas Gohr        //last password set (Windows counts from January 1st 1601)
18793a7873eSAndreas Gohr        $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600;
18893a7873eSAndreas Gohr        //will it expire?
18993a7873eSAndreas Gohr        $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
190f4476bd9SJan Schumann
191f4476bd9SJan Schumann        // additional information
192*32fd494aSAndreas Gohr        foreach($this->conf['additional'] as $field) {
193f4476bd9SJan Schumann            if(isset($result[0][strtolower($field)])) {
194f4476bd9SJan Schumann                $info[$field] = $result[0][strtolower($field)][0];
195f4476bd9SJan Schumann            }
196f4476bd9SJan Schumann        }
197f4476bd9SJan Schumann
198f4476bd9SJan Schumann        // handle ActiveDirectory memberOf
199*32fd494aSAndreas Gohr        $info['grps'] = $adldap->user()->groups($this->_userName($user),(bool) $this->opts['recursive_groups']);
200f4476bd9SJan Schumann
201f4476bd9SJan Schumann        if(is_array($info['grps'])) {
202f4476bd9SJan Schumann            foreach($info['grps'] as $ndx => $group) {
203f4476bd9SJan Schumann                $info['grps'][$ndx] = $this->cleanGroup($group);
204f4476bd9SJan Schumann            }
205f4476bd9SJan Schumann        }
206f4476bd9SJan Schumann
207f4476bd9SJan Schumann        // always add the default group to the list of groups
208f4476bd9SJan Schumann        if(!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
209f4476bd9SJan Schumann            $info['grps'][] = $conf['defaultgroup'];
210f4476bd9SJan Schumann        }
211f4476bd9SJan Schumann
21293a7873eSAndreas Gohr        // add the user's domain to the groups
21393a7873eSAndreas Gohr        $domain = $this->_userDomain($user);
21493a7873eSAndreas Gohr        if($domain && !in_array("domain-$domain", (array) $info['grps'])) {
21593a7873eSAndreas Gohr            $info['grps'][] = $this->cleanGroup("domain-$domain");
21693a7873eSAndreas Gohr        }
21793a7873eSAndreas Gohr
21893a7873eSAndreas Gohr        // check expiry time
219*32fd494aSAndreas Gohr        if($info['expires'] && $this->conf['expirywarn']){
220*32fd494aSAndreas Gohr            $timeleft = $adldap->user()->passwordExpiry($user); // returns unixtime
22193a7873eSAndreas Gohr            $timeleft = round($timeleft/(24*60*60));
22293a7873eSAndreas Gohr            $info['expiresin'] = $timeleft;
22393a7873eSAndreas Gohr
22493a7873eSAndreas Gohr            // if this is the current user, warn him (once per request only)
22593a7873eSAndreas Gohr            if(($_SERVER['REMOTE_USER'] == $user) &&
226*32fd494aSAndreas Gohr                ($timeleft <= $this->conf['expirywarn']) &&
22793a7873eSAndreas Gohr                !$this->msgshown
22893a7873eSAndreas Gohr            ) {
22993a7873eSAndreas Gohr                $msg = sprintf($lang['authpwdexpire'], $timeleft);
23093a7873eSAndreas Gohr                if($this->canDo('modPass')) {
23193a7873eSAndreas Gohr                    $url = wl($ID, array('do'=> 'profile'));
23293a7873eSAndreas Gohr                    $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
23393a7873eSAndreas Gohr                }
23493a7873eSAndreas Gohr                msg($msg);
23593a7873eSAndreas Gohr                $this->msgshown = true;
23693a7873eSAndreas Gohr            }
23793a7873eSAndreas Gohr        }
23893a7873eSAndreas Gohr
239f4476bd9SJan Schumann        return $info;
240f4476bd9SJan Schumann    }
241f4476bd9SJan Schumann
242f4476bd9SJan Schumann    /**
243f4476bd9SJan Schumann     * Make AD group names usable by DokuWiki.
244f4476bd9SJan Schumann     *
245f4476bd9SJan Schumann     * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
246f4476bd9SJan Schumann     *
247f4476bd9SJan Schumann     * @author  James Van Lommel (jamesvl@gmail.com)
24893a7873eSAndreas Gohr     * @param string $group
24993a7873eSAndreas Gohr     * @return string
250f4476bd9SJan Schumann     */
25193a7873eSAndreas Gohr    public function cleanGroup($group) {
25293a7873eSAndreas Gohr        $group = str_replace('\\', '', $group);
25393a7873eSAndreas Gohr        $group = str_replace('#', '', $group);
25493a7873eSAndreas Gohr        $group = preg_replace('[\s]', '_', $group);
25593a7873eSAndreas Gohr        $group = utf8_strtolower(trim($group));
25693a7873eSAndreas Gohr        return $group;
257f4476bd9SJan Schumann    }
258f4476bd9SJan Schumann
259f4476bd9SJan Schumann    /**
260f4476bd9SJan Schumann     * Sanitize user names
26193a7873eSAndreas Gohr     *
26293a7873eSAndreas Gohr     * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
26393a7873eSAndreas Gohr     *
26493a7873eSAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
26593a7873eSAndreas Gohr     * @param string $user
26693a7873eSAndreas Gohr     * @return string
267f4476bd9SJan Schumann     */
26893a7873eSAndreas Gohr    public function cleanUser($user) {
26993a7873eSAndreas Gohr        $domain = '';
27093a7873eSAndreas Gohr
27193a7873eSAndreas Gohr        // get NTLM or Kerberos domain part
27293a7873eSAndreas Gohr        list($dom, $user) = explode('\\', $user, 2);
27393a7873eSAndreas Gohr        if(!$user) $user = $dom;
27493a7873eSAndreas Gohr        if($dom) $domain = $dom;
27593a7873eSAndreas Gohr        list($user, $dom) = explode('@', $user, 2);
27693a7873eSAndreas Gohr        if($dom) $domain = $dom;
27793a7873eSAndreas Gohr
27893a7873eSAndreas Gohr        // clean up both
27993a7873eSAndreas Gohr        $domain = utf8_strtolower(trim($domain));
28093a7873eSAndreas Gohr        $user   = utf8_strtolower(trim($user));
28193a7873eSAndreas Gohr
28293a7873eSAndreas Gohr        // is this a known, valid domain? if not discard
283*32fd494aSAndreas Gohr        if(!is_array($this->conf[$domain])) {
28493a7873eSAndreas Gohr            $domain = '';
28593a7873eSAndreas Gohr        }
28693a7873eSAndreas Gohr
28793a7873eSAndreas Gohr        // reattach domain
28893a7873eSAndreas Gohr        if($domain) $user = "$user@$domain";
28993a7873eSAndreas Gohr        return $user;
290f4476bd9SJan Schumann    }
291f4476bd9SJan Schumann
292f4476bd9SJan Schumann    /**
293f4476bd9SJan Schumann     * Most values in LDAP are case-insensitive
29493a7873eSAndreas Gohr     *
29593a7873eSAndreas Gohr     * @return bool
296f4476bd9SJan Schumann     */
29793a7873eSAndreas Gohr    public function isCaseSensitive() {
298f4476bd9SJan Schumann        return false;
299f4476bd9SJan Schumann    }
300f4476bd9SJan Schumann
301f4476bd9SJan Schumann    /**
302f4476bd9SJan Schumann     * Bulk retrieval of user data
303f4476bd9SJan Schumann     *
304f4476bd9SJan Schumann     * @author  Dominik Eckelmann <dokuwiki@cosmocode.de>
30593a7873eSAndreas Gohr     * @param   int   $start     index of first user to be returned
30693a7873eSAndreas Gohr     * @param   int   $limit     max number of users to be returned
30793a7873eSAndreas Gohr     * @param   array $filter    array of field/pattern pairs, null for no filter
30893a7873eSAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
309f4476bd9SJan Schumann     */
31093a7873eSAndreas Gohr    public function retrieveUsers($start = 0, $limit = -1, $filter = array()) {
31193a7873eSAndreas Gohr        $adldap = $this->_adldap(null);
31293a7873eSAndreas Gohr        if(!$adldap) return false;
313f4476bd9SJan Schumann
314f4476bd9SJan Schumann        if($this->users === null) {
315f4476bd9SJan Schumann            //get info for given user
316*32fd494aSAndreas Gohr            $result = $adldap->user()->all();
317f4476bd9SJan Schumann            if (!$result) return array();
318f4476bd9SJan Schumann            $this->users = array_fill_keys($result, false);
319f4476bd9SJan Schumann        }
320f4476bd9SJan Schumann
321f4476bd9SJan Schumann        $i     = 0;
322f4476bd9SJan Schumann        $count = 0;
323f4476bd9SJan Schumann        $this->_constructPattern($filter);
324f4476bd9SJan Schumann        $result = array();
325f4476bd9SJan Schumann
326f4476bd9SJan Schumann        foreach($this->users as $user => &$info) {
327f4476bd9SJan Schumann            if($i++ < $start) {
328f4476bd9SJan Schumann                continue;
329f4476bd9SJan Schumann            }
330f4476bd9SJan Schumann            if($info === false) {
331f4476bd9SJan Schumann                $info = $this->getUserData($user);
332f4476bd9SJan Schumann            }
333f4476bd9SJan Schumann            if($this->_filter($user, $info)) {
334f4476bd9SJan Schumann                $result[$user] = $info;
335f4476bd9SJan Schumann                if(($limit >= 0) && (++$count >= $limit)) break;
336f4476bd9SJan Schumann            }
337f4476bd9SJan Schumann        }
338f4476bd9SJan Schumann        return $result;
339f4476bd9SJan Schumann    }
340f4476bd9SJan Schumann
341f4476bd9SJan Schumann    /**
342f4476bd9SJan Schumann     * Modify user data
343f4476bd9SJan Schumann     *
34493a7873eSAndreas Gohr     * @param   string $user      nick of the user to be changed
34593a7873eSAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed
346f4476bd9SJan Schumann     * @return  bool
347f4476bd9SJan Schumann     */
34893a7873eSAndreas Gohr    public function modifyUser($user, $changes) {
349f4476bd9SJan Schumann        $return = true;
35093a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
35193a7873eSAndreas Gohr        if(!$adldap) return false;
352f4476bd9SJan Schumann
353f4476bd9SJan Schumann        // password changing
354f4476bd9SJan Schumann        if(isset($changes['pass'])) {
355f4476bd9SJan Schumann            try {
356*32fd494aSAndreas Gohr                $return = $adldap->user()->password($this->_userName($user),$changes['pass']);
357f4476bd9SJan Schumann            } catch (adLDAPException $e) {
358*32fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
359f4476bd9SJan Schumann                $return = false;
360f4476bd9SJan Schumann            }
361f4476bd9SJan Schumann            if(!$return) msg('AD Auth: failed to change the password. Maybe the password policy was not met?', -1);
362f4476bd9SJan Schumann        }
363f4476bd9SJan Schumann
364f4476bd9SJan Schumann        // changing user data
365f4476bd9SJan Schumann        $adchanges = array();
366f4476bd9SJan Schumann        if(isset($changes['name'])) {
367f4476bd9SJan Schumann            // get first and last name
368f4476bd9SJan Schumann            $parts                     = explode(' ', $changes['name']);
369f4476bd9SJan Schumann            $adchanges['surname']      = array_pop($parts);
370f4476bd9SJan Schumann            $adchanges['firstname']    = join(' ', $parts);
371f4476bd9SJan Schumann            $adchanges['display_name'] = $changes['name'];
372f4476bd9SJan Schumann        }
373f4476bd9SJan Schumann        if(isset($changes['mail'])) {
374f4476bd9SJan Schumann            $adchanges['email'] = $changes['mail'];
375f4476bd9SJan Schumann        }
376f4476bd9SJan Schumann        if(count($adchanges)) {
377f4476bd9SJan Schumann            try {
378*32fd494aSAndreas Gohr                $return = $return & $adldap->user()->modify($this->_userName($user),$adchanges);
379f4476bd9SJan Schumann            } catch (adLDAPException $e) {
380*32fd494aSAndreas Gohr                if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
381f4476bd9SJan Schumann                $return = false;
382f4476bd9SJan Schumann            }
383f4476bd9SJan Schumann        }
384f4476bd9SJan Schumann
385f4476bd9SJan Schumann        return $return;
386f4476bd9SJan Schumann    }
387f4476bd9SJan Schumann
388f4476bd9SJan Schumann    /**
389f4476bd9SJan Schumann     * Initialize the AdLDAP library and connect to the server
39093a7873eSAndreas Gohr     *
39193a7873eSAndreas Gohr     * When you pass null as domain, it will reuse any existing domain.
39293a7873eSAndreas Gohr     * Eg. the one of the logged in user. It falls back to the default
39393a7873eSAndreas Gohr     * domain if no current one is available.
39493a7873eSAndreas Gohr     *
39593a7873eSAndreas Gohr     * @param string|null $domain The AD domain to use
39693a7873eSAndreas Gohr     * @return adLDAP|bool true if a connection was established
397f4476bd9SJan Schumann     */
39893a7873eSAndreas Gohr    protected function _adldap($domain) {
39993a7873eSAndreas Gohr        if(is_null($domain) && is_array($this->opts)) {
40093a7873eSAndreas Gohr            $domain = $this->opts['domain'];
40193a7873eSAndreas Gohr        }
40293a7873eSAndreas Gohr
40393a7873eSAndreas Gohr        $this->opts = $this->_loadServerConfig((string) $domain);
40493a7873eSAndreas Gohr        if(isset($this->adldap[$domain])) return $this->adldap[$domain];
405f4476bd9SJan Schumann
406f4476bd9SJan Schumann        // connect
407f4476bd9SJan Schumann        try {
40893a7873eSAndreas Gohr            $this->adldap[$domain] = new adLDAP($this->opts);
40993a7873eSAndreas Gohr            return $this->adldap[$domain];
410f4476bd9SJan Schumann        } catch(adLDAPException $e) {
411*32fd494aSAndreas Gohr            if($this->conf['debug']) {
412f4476bd9SJan Schumann                msg('AD Auth: '.$e->getMessage(), -1);
413f4476bd9SJan Schumann            }
414f4476bd9SJan Schumann            $this->success         = false;
41593a7873eSAndreas Gohr            $this->adldap[$domain] = null;
416f4476bd9SJan Schumann        }
417f4476bd9SJan Schumann        return false;
418f4476bd9SJan Schumann    }
419f4476bd9SJan Schumann
420f4476bd9SJan Schumann    /**
42193a7873eSAndreas Gohr     * Get the domain part from a user
422f4476bd9SJan Schumann     *
42393a7873eSAndreas Gohr     * @param $user
42493a7873eSAndreas Gohr     * @return string
425f4476bd9SJan Schumann     */
42693a7873eSAndreas Gohr    public function _userDomain($user) {
42793a7873eSAndreas Gohr        list(, $domain) = explode('@', $user, 2);
42893a7873eSAndreas Gohr        return $domain;
429f4476bd9SJan Schumann    }
430f4476bd9SJan Schumann
43193a7873eSAndreas Gohr    /**
43293a7873eSAndreas Gohr     * Get the user part from a user
43393a7873eSAndreas Gohr     *
43493a7873eSAndreas Gohr     * @param $user
43593a7873eSAndreas Gohr     * @return string
43693a7873eSAndreas Gohr     */
43793a7873eSAndreas Gohr    public function _userName($user) {
43893a7873eSAndreas Gohr        list($name) = explode('@', $user, 2);
43993a7873eSAndreas Gohr        return $name;
44093a7873eSAndreas Gohr    }
44193a7873eSAndreas Gohr
44293a7873eSAndreas Gohr    /**
44393a7873eSAndreas Gohr     * Fetch the configuration for the given AD domain
44493a7873eSAndreas Gohr     *
44593a7873eSAndreas Gohr     * @param string $domain current AD domain
44693a7873eSAndreas Gohr     * @return array
44793a7873eSAndreas Gohr     */
44893a7873eSAndreas Gohr    protected function _loadServerConfig($domain) {
44993a7873eSAndreas Gohr        // prepare adLDAP standard configuration
450*32fd494aSAndreas Gohr        $opts = $this->conf;
45193a7873eSAndreas Gohr
45293a7873eSAndreas Gohr        $opts['domain'] = $domain;
45393a7873eSAndreas Gohr
45493a7873eSAndreas Gohr        // add possible domain specific configuration
455*32fd494aSAndreas Gohr        if($domain && is_array($this->conf[$domain])) foreach($this->conf[$domain] as $key => $val) {
45693a7873eSAndreas Gohr            $opts[$key] = $val;
45793a7873eSAndreas Gohr        }
45893a7873eSAndreas Gohr
45993a7873eSAndreas Gohr        // handle multiple AD servers
46093a7873eSAndreas Gohr        $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
46193a7873eSAndreas Gohr        $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
46293a7873eSAndreas Gohr        $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
46393a7873eSAndreas Gohr
46493a7873eSAndreas Gohr        // we can change the password if SSL is set
46593a7873eSAndreas Gohr        if($opts['use_ssl'] || $opts['use_tls']) {
46693a7873eSAndreas Gohr            $this->cando['modPass'] = true;
46793a7873eSAndreas Gohr        } else {
46893a7873eSAndreas Gohr            $this->cando['modPass'] = false;
46993a7873eSAndreas Gohr        }
47093a7873eSAndreas Gohr
47193a7873eSAndreas Gohr        if(isset($opts['ad_username']) && isset($opts['ad_password'])) {
47293a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
47393a7873eSAndreas Gohr        } else {
47493a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
47593a7873eSAndreas Gohr        }
47693a7873eSAndreas Gohr
47793a7873eSAndreas Gohr        return $opts;
47893a7873eSAndreas Gohr    }
47993a7873eSAndreas Gohr
48093a7873eSAndreas Gohr    /**
48193a7873eSAndreas Gohr     * Check provided user and userinfo for matching patterns
48293a7873eSAndreas Gohr     *
48393a7873eSAndreas Gohr     * The patterns are set up with $this->_constructPattern()
48493a7873eSAndreas Gohr     *
48593a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
48693a7873eSAndreas Gohr     * @param string $user
48793a7873eSAndreas Gohr     * @param array  $info
48893a7873eSAndreas Gohr     * @return bool
48993a7873eSAndreas Gohr     */
49093a7873eSAndreas Gohr    protected function _filter($user, $info) {
49193a7873eSAndreas Gohr        foreach($this->_pattern as $item => $pattern) {
49293a7873eSAndreas Gohr            if($item == 'user') {
49393a7873eSAndreas Gohr                if(!preg_match($pattern, $user)) return false;
49493a7873eSAndreas Gohr            } else if($item == 'grps') {
49593a7873eSAndreas Gohr                if(!count(preg_grep($pattern, $info['grps']))) return false;
49693a7873eSAndreas Gohr            } else {
49793a7873eSAndreas Gohr                if(!preg_match($pattern, $info[$item])) return false;
49893a7873eSAndreas Gohr            }
49993a7873eSAndreas Gohr        }
50093a7873eSAndreas Gohr        return true;
50193a7873eSAndreas Gohr    }
50293a7873eSAndreas Gohr
50393a7873eSAndreas Gohr    /**
50493a7873eSAndreas Gohr     * Create a pattern for $this->_filter()
50593a7873eSAndreas Gohr     *
50693a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
50793a7873eSAndreas Gohr     * @param array $filter
50893a7873eSAndreas Gohr     */
50993a7873eSAndreas Gohr    protected function _constructPattern($filter) {
510f4476bd9SJan Schumann        $this->_pattern = array();
511f4476bd9SJan Schumann        foreach($filter as $item => $pattern) {
512f4476bd9SJan Schumann            $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
513f4476bd9SJan Schumann        }
514f4476bd9SJan Schumann    }
515f4476bd9SJan Schumann}
516