xref: /dokuwiki/lib/plugins/authad/auth.php (revision 93a7873eb0646b5712d75b031cd8b8b143968ba2)
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 *
22*93a7873eSAndreas Gohr *   $conf['auth']['ad']['account_suffix']     = '
23*93a7873eSAndreas Gohr *
24*93a7873eSAndreas Gohr * @my.domain.org';
25*93a7873eSAndreas Gohr *   $conf['auth']['ad']['base_dn']            = 'DC=my,DC=domain,DC=org';
26*93a7873eSAndreas Gohr *   $conf['auth']['ad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org';
27f4476bd9SJan Schumann *
28f4476bd9SJan Schumann *   //optional:
29*93a7873eSAndreas Gohr *   $conf['auth']['ad']['sso']                = 1;
30*93a7873eSAndreas Gohr *   $conf['auth']['ad']['ad_username']        = 'root';
31*93a7873eSAndreas Gohr *   $conf['auth']['ad']['ad_password']        = 'pass';
32*93a7873eSAndreas Gohr *   $conf['auth']['ad']['real_primarygroup']  = 1;
33*93a7873eSAndreas Gohr *   $conf['auth']['ad']['use_ssl']            = 1;
34*93a7873eSAndreas Gohr *   $conf['auth']['ad']['use_tls']            = 1;
35*93a7873eSAndreas Gohr *   $conf['auth']['ad']['debug']              = 1;
36*93a7873eSAndreas Gohr *   // warn user about expiring password this many days in advance:
37*93a7873eSAndreas 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 */
49*93a7873eSAndreas Gohr
50*93a7873eSAndreas Gohrrequire_once(DOKU_INC.'inc/adLDAP/adLDAP.php');
51*93a7873eSAndreas Gohr
52*93a7873eSAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin {
53*93a7873eSAndreas Gohr    /**
54*93a7873eSAndreas Gohr     * @var array copy of the auth backend configuration
55*93a7873eSAndreas Gohr     */
56*93a7873eSAndreas Gohr    protected $cnf = array();
57*93a7873eSAndreas Gohr    /**
58*93a7873eSAndreas Gohr     * @var array hold connection data for a specific AD domain
59*93a7873eSAndreas Gohr     */
60*93a7873eSAndreas Gohr    protected $opts = array();
61*93a7873eSAndreas Gohr    /**
62*93a7873eSAndreas Gohr     * @var array open connections for each AD domain, as adLDAP objects
63*93a7873eSAndreas Gohr     */
64*93a7873eSAndreas Gohr    protected $adldap = array();
65*93a7873eSAndreas Gohr
66*93a7873eSAndreas Gohr    /**
67*93a7873eSAndreas Gohr     * @var bool message state
68*93a7873eSAndreas Gohr     */
69*93a7873eSAndreas Gohr    protected $msgshown = false;
70*93a7873eSAndreas Gohr
71*93a7873eSAndreas Gohr    /**
72*93a7873eSAndreas Gohr     * @var array user listing cache
73*93a7873eSAndreas Gohr     */
74*93a7873eSAndreas Gohr    protected $users = array();
75*93a7873eSAndreas Gohr
76*93a7873eSAndreas Gohr    /**
77*93a7873eSAndreas Gohr     * @var array filter patterns for listing users
78*93a7873eSAndreas Gohr     */
79*93a7873eSAndreas Gohr    protected $_pattern = array();
80f4476bd9SJan Schumann
81f4476bd9SJan Schumann    /**
82f4476bd9SJan Schumann     * Constructor
83f4476bd9SJan Schumann     */
84*93a7873eSAndreas Gohr    public function __construct() {
85f4476bd9SJan Schumann        global $conf;
86f4476bd9SJan Schumann        $this->cnf = $conf['auth']['ad'];
87f4476bd9SJan Schumann
88f4476bd9SJan Schumann        // additional information fields
89f4476bd9SJan Schumann        if(isset($this->cnf['additional'])) {
90f4476bd9SJan Schumann            $this->cnf['additional'] = str_replace(' ', '', $this->cnf['additional']);
91f4476bd9SJan Schumann            $this->cnf['additional'] = explode(',', $this->cnf['additional']);
92f4476bd9SJan Schumann        } else $this->cnf['additional'] = array();
93f4476bd9SJan Schumann
94f4476bd9SJan Schumann        // ldap extension is needed
95f4476bd9SJan Schumann        if(!function_exists('ldap_connect')) {
96f4476bd9SJan Schumann            if($this->cnf['debug'])
97f4476bd9SJan Schumann                msg("AD Auth: PHP LDAP extension not found.", -1);
98f4476bd9SJan Schumann            $this->success = false;
99f4476bd9SJan Schumann            return;
100f4476bd9SJan Schumann        }
101f4476bd9SJan Schumann
102f4476bd9SJan Schumann        // Prepare SSO
103*93a7873eSAndreas Gohr        if(!utf8_check($_SERVER['REMOTE_USER'])) {
104*93a7873eSAndreas Gohr            $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']);
105*93a7873eSAndreas Gohr        }
106f4476bd9SJan Schumann        if($_SERVER['REMOTE_USER'] && $this->cnf['sso']) {
107*93a7873eSAndreas Gohr            $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']);
108f4476bd9SJan Schumann
109f4476bd9SJan Schumann            // we need to simulate a login
110f4476bd9SJan Schumann            if(empty($_COOKIE[DOKU_COOKIE])) {
111f4476bd9SJan Schumann                $_REQUEST['u'] = $_SERVER['REMOTE_USER'];
112f4476bd9SJan Schumann                $_REQUEST['p'] = 'sso_only';
113f4476bd9SJan Schumann            }
114f4476bd9SJan Schumann        }
115f4476bd9SJan Schumann
116*93a7873eSAndreas Gohr        // other can do's are changed in $this->_loadServerConfig() base on domain setup
117f4476bd9SJan Schumann        $this->cando['modName'] = true;
118f4476bd9SJan Schumann        $this->cando['modMail'] = true;
119f4476bd9SJan Schumann    }
120f4476bd9SJan Schumann
121f4476bd9SJan Schumann    /**
122f4476bd9SJan Schumann     * Check user+password [required auth function]
123f4476bd9SJan Schumann     *
124f4476bd9SJan Schumann     * Checks if the given user exists and the given
125f4476bd9SJan Schumann     * plaintext password is correct by trying to bind
126f4476bd9SJan Schumann     * to the LDAP server
127f4476bd9SJan Schumann     *
128f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
129*93a7873eSAndreas Gohr     * @param string $user
130*93a7873eSAndreas Gohr     * @param string $pass
131f4476bd9SJan Schumann     * @return  bool
132f4476bd9SJan Schumann     */
133*93a7873eSAndreas Gohr    public function checkPass($user, $pass) {
134f4476bd9SJan Schumann        if($_SERVER['REMOTE_USER'] &&
135f4476bd9SJan Schumann            $_SERVER['REMOTE_USER'] == $user &&
136*93a7873eSAndreas Gohr            $this->cnf['sso']
137*93a7873eSAndreas Gohr        ) return true;
138f4476bd9SJan Schumann
139*93a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
140*93a7873eSAndreas Gohr        if(!$adldap) return false;
141*93a7873eSAndreas Gohr
142*93a7873eSAndreas Gohr        return $adldap->authenticate($this->_userName($user), $pass);
143f4476bd9SJan Schumann    }
144f4476bd9SJan Schumann
145f4476bd9SJan Schumann    /**
146f4476bd9SJan Schumann     * Return user info [required auth function]
147f4476bd9SJan Schumann     *
148f4476bd9SJan Schumann     * Returns info about the given user needs to contain
149f4476bd9SJan Schumann     * at least these fields:
150f4476bd9SJan Schumann     *
151f4476bd9SJan Schumann     * name    string  full name of the user
152f4476bd9SJan Schumann     * mail    string  email address of the user
153f4476bd9SJan Schumann     * grps    array   list of groups the user is in
154f4476bd9SJan Schumann     *
155*93a7873eSAndreas Gohr     * This AD specific function returns the following
156f4476bd9SJan Schumann     * addional fields:
157f4476bd9SJan Schumann     *
158f4476bd9SJan Schumann     * dn         string    distinguished name (DN)
159*93a7873eSAndreas Gohr     * uid        string    samaccountname
160*93a7873eSAndreas Gohr     * lastpwd    int       timestamp of the date when the password was set
161*93a7873eSAndreas Gohr     * expires    true      if the password expires
162*93a7873eSAndreas Gohr     * expiresin  int       seconds until the password expires
163*93a7873eSAndreas Gohr     * any fields specified in the 'additional' config option
164f4476bd9SJan Schumann     *
165f4476bd9SJan Schumann     * @author  James Van Lommel <james@nosq.com>
166*93a7873eSAndreas Gohr     * @param string $user
167*93a7873eSAndreas Gohr     * @return array
168f4476bd9SJan Schumann     */
169*93a7873eSAndreas Gohr    public function getUserData($user) {
170f4476bd9SJan Schumann        global $conf;
171*93a7873eSAndreas Gohr        global $lang;
172*93a7873eSAndreas Gohr        global $ID;
173*93a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
174*93a7873eSAndreas Gohr        if(!$adldap) return false;
175f4476bd9SJan Schumann
176*93a7873eSAndreas Gohr        if($user == '') return array();
177*93a7873eSAndreas Gohr
178*93a7873eSAndreas Gohr        $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol');
179f4476bd9SJan Schumann
180f4476bd9SJan Schumann        // add additional fields to read
181f4476bd9SJan Schumann        $fields = array_merge($fields, $this->cnf['additional']);
182f4476bd9SJan Schumann        $fields = array_unique($fields);
183f4476bd9SJan Schumann
184f4476bd9SJan Schumann        //get info for given user
185*93a7873eSAndreas Gohr        $result = $this->adldap->user()->info($this->_userName($user), $fields);
186*93a7873eSAndreas Gohr        if($result == false){
187*93a7873eSAndreas Gohr            return array();
188*93a7873eSAndreas Gohr        }
189*93a7873eSAndreas Gohr
190f4476bd9SJan Schumann        //general user info
191f4476bd9SJan Schumann        $info['name'] = $result[0]['displayname'][0];
192f4476bd9SJan Schumann        $info['mail'] = $result[0]['mail'][0];
193f4476bd9SJan Schumann        $info['uid']  = $result[0]['samaccountname'][0];
194f4476bd9SJan Schumann        $info['dn']   = $result[0]['dn'];
195*93a7873eSAndreas Gohr        //last password set (Windows counts from January 1st 1601)
196*93a7873eSAndreas Gohr        $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600;
197*93a7873eSAndreas Gohr        //will it expire?
198*93a7873eSAndreas Gohr        $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
199f4476bd9SJan Schumann
200f4476bd9SJan Schumann        // additional information
201f4476bd9SJan Schumann        foreach($this->cnf['additional'] as $field) {
202f4476bd9SJan Schumann            if(isset($result[0][strtolower($field)])) {
203f4476bd9SJan Schumann                $info[$field] = $result[0][strtolower($field)][0];
204f4476bd9SJan Schumann            }
205f4476bd9SJan Schumann        }
206f4476bd9SJan Schumann
207f4476bd9SJan Schumann        // handle ActiveDirectory memberOf
208*93a7873eSAndreas Gohr        $info['grps'] = $this->adldap->user()->groups($this->_userName($user),(bool) $this->opts['recursive_groups']);
209f4476bd9SJan Schumann
210f4476bd9SJan Schumann        if(is_array($info['grps'])) {
211f4476bd9SJan Schumann            foreach($info['grps'] as $ndx => $group) {
212f4476bd9SJan Schumann                $info['grps'][$ndx] = $this->cleanGroup($group);
213f4476bd9SJan Schumann            }
214f4476bd9SJan Schumann        }
215f4476bd9SJan Schumann
216f4476bd9SJan Schumann        // always add the default group to the list of groups
217f4476bd9SJan Schumann        if(!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
218f4476bd9SJan Schumann            $info['grps'][] = $conf['defaultgroup'];
219f4476bd9SJan Schumann        }
220f4476bd9SJan Schumann
221*93a7873eSAndreas Gohr        // add the user's domain to the groups
222*93a7873eSAndreas Gohr        $domain = $this->_userDomain($user);
223*93a7873eSAndreas Gohr        if($domain && !in_array("domain-$domain", (array) $info['grps'])) {
224*93a7873eSAndreas Gohr            $info['grps'][] = $this->cleanGroup("domain-$domain");
225*93a7873eSAndreas Gohr        }
226*93a7873eSAndreas Gohr
227*93a7873eSAndreas Gohr        // check expiry time
228*93a7873eSAndreas Gohr        if($info['expires'] && $this->cnf['expirywarn']){
229*93a7873eSAndreas Gohr            $timeleft = $this->adldap->user()->passwordExpiry($user); // returns unixtime
230*93a7873eSAndreas Gohr            $timeleft = round($timeleft/(24*60*60));
231*93a7873eSAndreas Gohr            $info['expiresin'] = $timeleft;
232*93a7873eSAndreas Gohr
233*93a7873eSAndreas Gohr            // if this is the current user, warn him (once per request only)
234*93a7873eSAndreas Gohr            if(($_SERVER['REMOTE_USER'] == $user) &&
235*93a7873eSAndreas Gohr                ($timeleft <= $this->cnf['expirywarn']) &&
236*93a7873eSAndreas Gohr                !$this->msgshown
237*93a7873eSAndreas Gohr            ) {
238*93a7873eSAndreas Gohr                $msg = sprintf($lang['authpwdexpire'], $timeleft);
239*93a7873eSAndreas Gohr                if($this->canDo('modPass')) {
240*93a7873eSAndreas Gohr                    $url = wl($ID, array('do'=> 'profile'));
241*93a7873eSAndreas Gohr                    $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
242*93a7873eSAndreas Gohr                }
243*93a7873eSAndreas Gohr                msg($msg);
244*93a7873eSAndreas Gohr                $this->msgshown = true;
245*93a7873eSAndreas Gohr            }
246*93a7873eSAndreas Gohr        }
247*93a7873eSAndreas Gohr
248f4476bd9SJan Schumann        return $info;
249f4476bd9SJan Schumann    }
250f4476bd9SJan Schumann
251f4476bd9SJan Schumann    /**
252f4476bd9SJan Schumann     * Make AD group names usable by DokuWiki.
253f4476bd9SJan Schumann     *
254f4476bd9SJan Schumann     * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
255f4476bd9SJan Schumann     *
256f4476bd9SJan Schumann     * @author  James Van Lommel (jamesvl@gmail.com)
257*93a7873eSAndreas Gohr     * @param string $group
258*93a7873eSAndreas Gohr     * @return string
259f4476bd9SJan Schumann     */
260*93a7873eSAndreas Gohr    public function cleanGroup($group) {
261*93a7873eSAndreas Gohr        $group = str_replace('\\', '', $group);
262*93a7873eSAndreas Gohr        $group = str_replace('#', '', $group);
263*93a7873eSAndreas Gohr        $group = preg_replace('[\s]', '_', $group);
264*93a7873eSAndreas Gohr        $group = utf8_strtolower(trim($group));
265*93a7873eSAndreas Gohr        return $group;
266f4476bd9SJan Schumann    }
267f4476bd9SJan Schumann
268f4476bd9SJan Schumann    /**
269f4476bd9SJan Schumann     * Sanitize user names
270*93a7873eSAndreas Gohr     *
271*93a7873eSAndreas Gohr     * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
272*93a7873eSAndreas Gohr     *
273*93a7873eSAndreas Gohr     * @author Andreas Gohr <gohr@cosmocode.de>
274*93a7873eSAndreas Gohr     * @param string $user
275*93a7873eSAndreas Gohr     * @return string
276f4476bd9SJan Schumann     */
277*93a7873eSAndreas Gohr    public function cleanUser($user) {
278*93a7873eSAndreas Gohr        $domain = '';
279*93a7873eSAndreas Gohr
280*93a7873eSAndreas Gohr        // get NTLM or Kerberos domain part
281*93a7873eSAndreas Gohr        list($dom, $user) = explode('\\', $user, 2);
282*93a7873eSAndreas Gohr        if(!$user) $user = $dom;
283*93a7873eSAndreas Gohr        if($dom) $domain = $dom;
284*93a7873eSAndreas Gohr        list($user, $dom) = explode('@', $user, 2);
285*93a7873eSAndreas Gohr        if($dom) $domain = $dom;
286*93a7873eSAndreas Gohr
287*93a7873eSAndreas Gohr        // clean up both
288*93a7873eSAndreas Gohr        $domain = utf8_strtolower(trim($domain));
289*93a7873eSAndreas Gohr        $user   = utf8_strtolower(trim($user));
290*93a7873eSAndreas Gohr
291*93a7873eSAndreas Gohr        // is this a known, valid domain? if not discard
292*93a7873eSAndreas Gohr        if(!is_array($this->cnf[$domain])) {
293*93a7873eSAndreas Gohr            $domain = '';
294*93a7873eSAndreas Gohr        }
295*93a7873eSAndreas Gohr
296*93a7873eSAndreas Gohr        // reattach domain
297*93a7873eSAndreas Gohr        if($domain) $user = "$user@$domain";
298*93a7873eSAndreas Gohr        return $user;
299f4476bd9SJan Schumann    }
300f4476bd9SJan Schumann
301f4476bd9SJan Schumann    /**
302f4476bd9SJan Schumann     * Most values in LDAP are case-insensitive
303*93a7873eSAndreas Gohr     *
304*93a7873eSAndreas Gohr     * @return bool
305f4476bd9SJan Schumann     */
306*93a7873eSAndreas Gohr    public function isCaseSensitive() {
307f4476bd9SJan Schumann        return false;
308f4476bd9SJan Schumann    }
309f4476bd9SJan Schumann
310f4476bd9SJan Schumann    /**
311f4476bd9SJan Schumann     * Bulk retrieval of user data
312f4476bd9SJan Schumann     *
313f4476bd9SJan Schumann     * @author  Dominik Eckelmann <dokuwiki@cosmocode.de>
314*93a7873eSAndreas Gohr     * @param   int   $start     index of first user to be returned
315*93a7873eSAndreas Gohr     * @param   int   $limit     max number of users to be returned
316*93a7873eSAndreas Gohr     * @param   array $filter    array of field/pattern pairs, null for no filter
317*93a7873eSAndreas Gohr     * @return  array userinfo (refer getUserData for internal userinfo details)
318f4476bd9SJan Schumann     */
319*93a7873eSAndreas Gohr    public function retrieveUsers($start = 0, $limit = -1, $filter = array()) {
320*93a7873eSAndreas Gohr        $adldap = $this->_adldap(null);
321*93a7873eSAndreas Gohr        if(!$adldap) return false;
322f4476bd9SJan Schumann
323f4476bd9SJan Schumann        if($this->users === null) {
324f4476bd9SJan Schumann            //get info for given user
325*93a7873eSAndreas Gohr            $result = $this->adldap->user()->all();
326f4476bd9SJan Schumann            if (!$result) return array();
327f4476bd9SJan Schumann            $this->users = array_fill_keys($result, false);
328f4476bd9SJan Schumann        }
329f4476bd9SJan Schumann
330f4476bd9SJan Schumann        $i     = 0;
331f4476bd9SJan Schumann        $count = 0;
332f4476bd9SJan Schumann        $this->_constructPattern($filter);
333f4476bd9SJan Schumann        $result = array();
334f4476bd9SJan Schumann
335f4476bd9SJan Schumann        foreach($this->users as $user => &$info) {
336f4476bd9SJan Schumann            if($i++ < $start) {
337f4476bd9SJan Schumann                continue;
338f4476bd9SJan Schumann            }
339f4476bd9SJan Schumann            if($info === false) {
340f4476bd9SJan Schumann                $info = $this->getUserData($user);
341f4476bd9SJan Schumann            }
342f4476bd9SJan Schumann            if($this->_filter($user, $info)) {
343f4476bd9SJan Schumann                $result[$user] = $info;
344f4476bd9SJan Schumann                if(($limit >= 0) && (++$count >= $limit)) break;
345f4476bd9SJan Schumann            }
346f4476bd9SJan Schumann        }
347f4476bd9SJan Schumann        return $result;
348f4476bd9SJan Schumann    }
349f4476bd9SJan Schumann
350f4476bd9SJan Schumann    /**
351f4476bd9SJan Schumann     * Modify user data
352f4476bd9SJan Schumann     *
353*93a7873eSAndreas Gohr     * @param   string $user      nick of the user to be changed
354*93a7873eSAndreas Gohr     * @param   array  $changes   array of field/value pairs to be changed
355f4476bd9SJan Schumann     * @return  bool
356f4476bd9SJan Schumann     */
357*93a7873eSAndreas Gohr    public function modifyUser($user, $changes) {
358f4476bd9SJan Schumann        $return = true;
359*93a7873eSAndreas Gohr        $adldap = $this->_adldap($this->_userDomain($user));
360*93a7873eSAndreas Gohr        if(!$adldap) return false;
361f4476bd9SJan Schumann
362f4476bd9SJan Schumann        // password changing
363f4476bd9SJan Schumann        if(isset($changes['pass'])) {
364f4476bd9SJan Schumann            try {
365*93a7873eSAndreas Gohr                $return = $this->adldap->user()->password($this->_userName($user),$changes['pass']);
366f4476bd9SJan Schumann            } catch (adLDAPException $e) {
367f4476bd9SJan Schumann                if ($this->cnf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
368f4476bd9SJan Schumann                $return = false;
369f4476bd9SJan Schumann            }
370f4476bd9SJan Schumann            if(!$return) msg('AD Auth: failed to change the password. Maybe the password policy was not met?', -1);
371f4476bd9SJan Schumann        }
372f4476bd9SJan Schumann
373f4476bd9SJan Schumann        // changing user data
374f4476bd9SJan Schumann        $adchanges = array();
375f4476bd9SJan Schumann        if(isset($changes['name'])) {
376f4476bd9SJan Schumann            // get first and last name
377f4476bd9SJan Schumann            $parts                     = explode(' ', $changes['name']);
378f4476bd9SJan Schumann            $adchanges['surname']      = array_pop($parts);
379f4476bd9SJan Schumann            $adchanges['firstname']    = join(' ', $parts);
380f4476bd9SJan Schumann            $adchanges['display_name'] = $changes['name'];
381f4476bd9SJan Schumann        }
382f4476bd9SJan Schumann        if(isset($changes['mail'])) {
383f4476bd9SJan Schumann            $adchanges['email'] = $changes['mail'];
384f4476bd9SJan Schumann        }
385f4476bd9SJan Schumann        if(count($adchanges)) {
386f4476bd9SJan Schumann            try {
387*93a7873eSAndreas Gohr                $return = $return & $this->adldap->user()->modify($this->_userName($user),$adchanges);
388f4476bd9SJan Schumann            } catch (adLDAPException $e) {
389f4476bd9SJan Schumann                if ($this->cnf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
390f4476bd9SJan Schumann                $return = false;
391f4476bd9SJan Schumann            }
392f4476bd9SJan Schumann        }
393f4476bd9SJan Schumann
394f4476bd9SJan Schumann        return $return;
395f4476bd9SJan Schumann    }
396f4476bd9SJan Schumann
397f4476bd9SJan Schumann    /**
398f4476bd9SJan Schumann     * Initialize the AdLDAP library and connect to the server
399*93a7873eSAndreas Gohr     *
400*93a7873eSAndreas Gohr     * When you pass null as domain, it will reuse any existing domain.
401*93a7873eSAndreas Gohr     * Eg. the one of the logged in user. It falls back to the default
402*93a7873eSAndreas Gohr     * domain if no current one is available.
403*93a7873eSAndreas Gohr     *
404*93a7873eSAndreas Gohr     * @param string|null $domain The AD domain to use
405*93a7873eSAndreas Gohr     * @return adLDAP|bool true if a connection was established
406f4476bd9SJan Schumann     */
407*93a7873eSAndreas Gohr    protected function _adldap($domain) {
408*93a7873eSAndreas Gohr        if(is_null($domain) && is_array($this->opts)) {
409*93a7873eSAndreas Gohr            $domain = $this->opts['domain'];
410*93a7873eSAndreas Gohr        }
411*93a7873eSAndreas Gohr
412*93a7873eSAndreas Gohr        $this->opts = $this->_loadServerConfig((string) $domain);
413*93a7873eSAndreas Gohr        if(isset($this->adldap[$domain])) return $this->adldap[$domain];
414f4476bd9SJan Schumann
415f4476bd9SJan Schumann        // connect
416f4476bd9SJan Schumann        try {
417*93a7873eSAndreas Gohr            $this->adldap[$domain] = new adLDAP($this->opts);
418*93a7873eSAndreas Gohr            return $this->adldap[$domain];
419f4476bd9SJan Schumann        } catch(adLDAPException $e) {
420f4476bd9SJan Schumann            if($this->cnf['debug']) {
421f4476bd9SJan Schumann                msg('AD Auth: '.$e->getMessage(), -1);
422f4476bd9SJan Schumann            }
423f4476bd9SJan Schumann            $this->success         = false;
424*93a7873eSAndreas Gohr            $this->adldap[$domain] = null;
425f4476bd9SJan Schumann        }
426f4476bd9SJan Schumann        return false;
427f4476bd9SJan Schumann    }
428f4476bd9SJan Schumann
429f4476bd9SJan Schumann    /**
430*93a7873eSAndreas Gohr     * Get the domain part from a user
431f4476bd9SJan Schumann     *
432*93a7873eSAndreas Gohr     * @param $user
433*93a7873eSAndreas Gohr     * @return string
434f4476bd9SJan Schumann     */
435*93a7873eSAndreas Gohr    public function _userDomain($user) {
436*93a7873eSAndreas Gohr        list(, $domain) = explode('@', $user, 2);
437*93a7873eSAndreas Gohr        return $domain;
438f4476bd9SJan Schumann    }
439f4476bd9SJan Schumann
440*93a7873eSAndreas Gohr    /**
441*93a7873eSAndreas Gohr     * Get the user part from a user
442*93a7873eSAndreas Gohr     *
443*93a7873eSAndreas Gohr     * @param $user
444*93a7873eSAndreas Gohr     * @return string
445*93a7873eSAndreas Gohr     */
446*93a7873eSAndreas Gohr    public function _userName($user) {
447*93a7873eSAndreas Gohr        list($name) = explode('@', $user, 2);
448*93a7873eSAndreas Gohr        return $name;
449*93a7873eSAndreas Gohr    }
450*93a7873eSAndreas Gohr
451*93a7873eSAndreas Gohr    /**
452*93a7873eSAndreas Gohr     * Fetch the configuration for the given AD domain
453*93a7873eSAndreas Gohr     *
454*93a7873eSAndreas Gohr     * @param string $domain current AD domain
455*93a7873eSAndreas Gohr     * @return array
456*93a7873eSAndreas Gohr     */
457*93a7873eSAndreas Gohr    protected function _loadServerConfig($domain) {
458*93a7873eSAndreas Gohr        // prepare adLDAP standard configuration
459*93a7873eSAndreas Gohr        $opts = $this->cnf;
460*93a7873eSAndreas Gohr
461*93a7873eSAndreas Gohr        $opts['domain'] = $domain;
462*93a7873eSAndreas Gohr
463*93a7873eSAndreas Gohr        // add possible domain specific configuration
464*93a7873eSAndreas Gohr        if($domain && is_array($this->cnf[$domain])) foreach($this->cnf[$domain] as $key => $val) {
465*93a7873eSAndreas Gohr            $opts[$key] = $val;
466*93a7873eSAndreas Gohr        }
467*93a7873eSAndreas Gohr
468*93a7873eSAndreas Gohr        // handle multiple AD servers
469*93a7873eSAndreas Gohr        $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
470*93a7873eSAndreas Gohr        $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
471*93a7873eSAndreas Gohr        $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
472*93a7873eSAndreas Gohr
473*93a7873eSAndreas Gohr        // we can change the password if SSL is set
474*93a7873eSAndreas Gohr        if($opts['use_ssl'] || $opts['use_tls']) {
475*93a7873eSAndreas Gohr            $this->cando['modPass'] = true;
476*93a7873eSAndreas Gohr        } else {
477*93a7873eSAndreas Gohr            $this->cando['modPass'] = false;
478*93a7873eSAndreas Gohr        }
479*93a7873eSAndreas Gohr
480*93a7873eSAndreas Gohr        if(isset($opts['ad_username']) && isset($opts['ad_password'])) {
481*93a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
482*93a7873eSAndreas Gohr        } else {
483*93a7873eSAndreas Gohr            $this->cando['getUsers'] = true;
484*93a7873eSAndreas Gohr        }
485*93a7873eSAndreas Gohr
486*93a7873eSAndreas Gohr        return $opts;
487*93a7873eSAndreas Gohr    }
488*93a7873eSAndreas Gohr
489*93a7873eSAndreas Gohr    /**
490*93a7873eSAndreas Gohr     * Check provided user and userinfo for matching patterns
491*93a7873eSAndreas Gohr     *
492*93a7873eSAndreas Gohr     * The patterns are set up with $this->_constructPattern()
493*93a7873eSAndreas Gohr     *
494*93a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
495*93a7873eSAndreas Gohr     * @param string $user
496*93a7873eSAndreas Gohr     * @param array  $info
497*93a7873eSAndreas Gohr     * @return bool
498*93a7873eSAndreas Gohr     */
499*93a7873eSAndreas Gohr    protected function _filter($user, $info) {
500*93a7873eSAndreas Gohr        foreach($this->_pattern as $item => $pattern) {
501*93a7873eSAndreas Gohr            if($item == 'user') {
502*93a7873eSAndreas Gohr                if(!preg_match($pattern, $user)) return false;
503*93a7873eSAndreas Gohr            } else if($item == 'grps') {
504*93a7873eSAndreas Gohr                if(!count(preg_grep($pattern, $info['grps']))) return false;
505*93a7873eSAndreas Gohr            } else {
506*93a7873eSAndreas Gohr                if(!preg_match($pattern, $info[$item])) return false;
507*93a7873eSAndreas Gohr            }
508*93a7873eSAndreas Gohr        }
509*93a7873eSAndreas Gohr        return true;
510*93a7873eSAndreas Gohr    }
511*93a7873eSAndreas Gohr
512*93a7873eSAndreas Gohr    /**
513*93a7873eSAndreas Gohr     * Create a pattern for $this->_filter()
514*93a7873eSAndreas Gohr     *
515*93a7873eSAndreas Gohr     * @author Chris Smith <chris@jalakai.co.uk>
516*93a7873eSAndreas Gohr     * @param array $filter
517*93a7873eSAndreas Gohr     */
518*93a7873eSAndreas Gohr    protected function _constructPattern($filter) {
519f4476bd9SJan Schumann        $this->_pattern = array();
520f4476bd9SJan Schumann        foreach($filter as $item => $pattern) {
521f4476bd9SJan Schumann            $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
522f4476bd9SJan Schumann        }
523f4476bd9SJan Schumann    }
524f4476bd9SJan Schumann}
525