1f4476bd9SJan Schumann<?php 2f4476bd9SJan Schumann 3f4476bd9SJan Schumann/** 4f4476bd9SJan Schumann * Active Directory authentication backend for DokuWiki 5f4476bd9SJan Schumann * 6f4476bd9SJan Schumann * This makes authentication with a Active Directory server much easier 7f4476bd9SJan Schumann * than when using the normal LDAP backend by utilizing the adLDAP library 8f4476bd9SJan Schumann * 9f4476bd9SJan Schumann * Usage: 10f4476bd9SJan Schumann * Set DokuWiki's local.protected.php auth setting to read 11f4476bd9SJan Schumann * 12f4476bd9SJan Schumann * $conf['authtype'] = 'authad'; 13f4476bd9SJan Schumann * 1432fd494aSAndreas Gohr * $conf['plugin']['authad']['account_suffix'] = '@my.domain.org'; 1532fd494aSAndreas Gohr * $conf['plugin']['authad']['base_dn'] = 'DC=my,DC=domain,DC=org'; 1632fd494aSAndreas Gohr * $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org'; 17f4476bd9SJan Schumann * 18f4476bd9SJan Schumann * //optional: 1932fd494aSAndreas Gohr * $conf['plugin']['authad']['sso'] = 1; 203002d731SAndreas Gohr * $conf['plugin']['authad']['admin_username'] = 'root'; 213002d731SAndreas Gohr * $conf['plugin']['authad']['admin_password'] = 'pass'; 2232fd494aSAndreas Gohr * $conf['plugin']['authad']['real_primarygroup'] = 1; 2332fd494aSAndreas Gohr * $conf['plugin']['authad']['use_ssl'] = 1; 2432fd494aSAndreas Gohr * $conf['plugin']['authad']['use_tls'] = 1; 2532fd494aSAndreas Gohr * $conf['plugin']['authad']['debug'] = 1; 2693a7873eSAndreas Gohr * // warn user about expiring password this many days in advance: 2732fd494aSAndreas Gohr * $conf['plugin']['authad']['expirywarn'] = 5; 28f4476bd9SJan Schumann * 29f4476bd9SJan Schumann * // get additional information to the userinfo array 30f4476bd9SJan Schumann * // add a list of comma separated ldap contact fields. 31f4476bd9SJan Schumann * $conf['plugin']['authad']['additional'] = 'field1,field2'; 32f4476bd9SJan Schumann * 33f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 34f4476bd9SJan Schumann * @author James Van Lommel <jamesvl@gmail.com> 35f4476bd9SJan Schumann * @link http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/ 36f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 37f4476bd9SJan Schumann * @author Jan Schumann <js@schumann-it.com> 38f4476bd9SJan Schumann */ 39a4337320SAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin 40a4337320SAndreas Gohr{ 4132fd494aSAndreas Gohr 4293a7873eSAndreas Gohr /** 4393a7873eSAndreas Gohr * @var array hold connection data for a specific AD domain 4493a7873eSAndreas Gohr */ 4593a7873eSAndreas Gohr protected $opts = array(); 4632fd494aSAndreas Gohr 4793a7873eSAndreas Gohr /** 4893a7873eSAndreas Gohr * @var array open connections for each AD domain, as adLDAP objects 4993a7873eSAndreas Gohr */ 5093a7873eSAndreas Gohr protected $adldap = array(); 5193a7873eSAndreas Gohr 5293a7873eSAndreas Gohr /** 5393a7873eSAndreas Gohr * @var bool message state 5493a7873eSAndreas Gohr */ 5593a7873eSAndreas Gohr protected $msgshown = false; 5693a7873eSAndreas Gohr 5793a7873eSAndreas Gohr /** 5893a7873eSAndreas Gohr * @var array user listing cache 5993a7873eSAndreas Gohr */ 6093a7873eSAndreas Gohr protected $users = array(); 6193a7873eSAndreas Gohr 6293a7873eSAndreas Gohr /** 6393a7873eSAndreas Gohr * @var array filter patterns for listing users 6493a7873eSAndreas Gohr */ 65a4337320SAndreas Gohr protected $pattern = array(); 66f4476bd9SJan Schumann 67a4337320SAndreas Gohr protected $actualstart = 0; 68c52f6cd2SMichael Große 69a4337320SAndreas Gohr protected $grpsusers = array(); 70c52f6cd2SMichael Große 71f4476bd9SJan Schumann /** 72f4476bd9SJan Schumann * Constructor 73f4476bd9SJan Schumann */ 74a4337320SAndreas Gohr public function __construct() 75a4337320SAndreas Gohr { 7600d58927SMichael Hamann global $INPUT; 77454d868bSAndreas Gohr parent::__construct(); 78454d868bSAndreas Gohr 79a4337320SAndreas Gohr require_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php'); 80a4337320SAndreas Gohr require_once(DOKU_PLUGIN.'authad/adLDAP/classes/adLDAPUtils.php'); 81a4337320SAndreas Gohr 8232fd494aSAndreas Gohr // we load the config early to modify it a bit here 8332fd494aSAndreas Gohr $this->loadConfig(); 84f4476bd9SJan Schumann 85f4476bd9SJan Schumann // additional information fields 8632fd494aSAndreas Gohr if (isset($this->conf['additional'])) { 8732fd494aSAndreas Gohr $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']); 8832fd494aSAndreas Gohr $this->conf['additional'] = explode(',', $this->conf['additional']); 8932fd494aSAndreas Gohr } else $this->conf['additional'] = array(); 90f4476bd9SJan Schumann 91f4476bd9SJan Schumann // ldap extension is needed 92f4476bd9SJan Schumann if (!function_exists('ldap_connect')) { 9332fd494aSAndreas Gohr if ($this->conf['debug']) 94f4476bd9SJan Schumann msg("AD Auth: PHP LDAP extension not found.", -1); 95f4476bd9SJan Schumann $this->success = false; 96f4476bd9SJan Schumann return; 97f4476bd9SJan Schumann } 98f4476bd9SJan Schumann 99f4476bd9SJan Schumann // Prepare SSO 100d34a2a38SAndreas Gohr if (!empty($_SERVER['REMOTE_USER'])) { 101d34a2a38SAndreas Gohr // make sure the right encoding is used 102d34a2a38SAndreas Gohr if ($this->getConf('sso_charset')) { 103d34a2a38SAndreas Gohr $_SERVER['REMOTE_USER'] = iconv($this->getConf('sso_charset'), 'UTF-8', $_SERVER['REMOTE_USER']); 104*8cbc5ee8SAndreas Gohr } elseif (!\dokuwiki\Utf8\Clean::isUtf8($_SERVER['REMOTE_USER'])) { 10593a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']); 10693a7873eSAndreas Gohr } 107d34a2a38SAndreas Gohr 108d34a2a38SAndreas Gohr // trust the incoming user 109d34a2a38SAndreas Gohr if ($this->conf['sso']) { 11093a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']); 111f4476bd9SJan Schumann 112f4476bd9SJan Schumann // we need to simulate a login 113f4476bd9SJan Schumann if (empty($_COOKIE[DOKU_COOKIE])) { 11400d58927SMichael Hamann $INPUT->set('u', $_SERVER['REMOTE_USER']); 11500d58927SMichael Hamann $INPUT->set('p', 'sso_only'); 116f4476bd9SJan Schumann } 117f4476bd9SJan Schumann } 118d34a2a38SAndreas Gohr } 119f4476bd9SJan Schumann 12093a7873eSAndreas Gohr // other can do's are changed in $this->_loadServerConfig() base on domain setup 121bb30445dSMichael Wilmes $this->cando['modName'] = (bool)$this->conf['update_name']; 122bb30445dSMichael Wilmes $this->cando['modMail'] = (bool)$this->conf['update_mail']; 12325f80763SMichael Große $this->cando['getUserCount'] = true; 124f4476bd9SJan Schumann } 125f4476bd9SJan Schumann 126f4476bd9SJan Schumann /** 127a154806fSAndreas Gohr * Load domain config on capability check 128a154806fSAndreas Gohr * 129a154806fSAndreas Gohr * @param string $cap 130a154806fSAndreas Gohr * @return bool 131a154806fSAndreas Gohr */ 132a4337320SAndreas Gohr public function canDo($cap) 133a4337320SAndreas Gohr { 134a154806fSAndreas Gohr //capabilities depend on config, which may change depending on domain 135a4337320SAndreas Gohr $domain = $this->getUserDomain($_SERVER['REMOTE_USER']); 136a4337320SAndreas Gohr $this->loadServerConfig($domain); 137a154806fSAndreas Gohr return parent::canDo($cap); 138a154806fSAndreas Gohr } 139a154806fSAndreas Gohr 140a154806fSAndreas Gohr /** 141f4476bd9SJan Schumann * Check user+password [required auth function] 142f4476bd9SJan Schumann * 143f4476bd9SJan Schumann * Checks if the given user exists and the given 144f4476bd9SJan Schumann * plaintext password is correct by trying to bind 145f4476bd9SJan Schumann * to the LDAP server 146f4476bd9SJan Schumann * 147f4476bd9SJan Schumann * @author James Van Lommel <james@nosq.com> 14893a7873eSAndreas Gohr * @param string $user 14993a7873eSAndreas Gohr * @param string $pass 150f4476bd9SJan Schumann * @return bool 151f4476bd9SJan Schumann */ 152a4337320SAndreas Gohr public function checkPass($user, $pass) 153a4337320SAndreas Gohr { 154f4476bd9SJan Schumann if ($_SERVER['REMOTE_USER'] && 155f4476bd9SJan Schumann $_SERVER['REMOTE_USER'] == $user && 15632fd494aSAndreas Gohr $this->conf['sso'] 15793a7873eSAndreas Gohr ) return true; 158f4476bd9SJan Schumann 159a4337320SAndreas Gohr $adldap = $this->initAdLdap($this->getUserDomain($user)); 16093a7873eSAndreas Gohr if (!$adldap) return false; 16193a7873eSAndreas Gohr 162a4337320SAndreas Gohr try { 163a4337320SAndreas Gohr return $adldap->authenticate($this->getUserName($user), $pass); 164a4337320SAndreas Gohr } catch (adLDAPException $e) { 165a4337320SAndreas Gohr // shouldn't really happen 166a4337320SAndreas Gohr return false; 167a4337320SAndreas Gohr } 168f4476bd9SJan Schumann } 169f4476bd9SJan Schumann 170f4476bd9SJan Schumann /** 171f4476bd9SJan Schumann * Return user info [required auth function] 172f4476bd9SJan Schumann * 173f4476bd9SJan Schumann * Returns info about the given user needs to contain 174f4476bd9SJan Schumann * at least these fields: 175f4476bd9SJan Schumann * 176f4476bd9SJan Schumann * name string full name of the user 177f4476bd9SJan Schumann * mail string email address of the user 178f4476bd9SJan Schumann * grps array list of groups the user is in 179f4476bd9SJan Schumann * 18093a7873eSAndreas Gohr * This AD specific function returns the following 181f4476bd9SJan Schumann * addional fields: 182f4476bd9SJan Schumann * 183f4476bd9SJan Schumann * dn string distinguished name (DN) 18493a7873eSAndreas Gohr * uid string samaccountname 18593a7873eSAndreas Gohr * lastpwd int timestamp of the date when the password was set 18693a7873eSAndreas Gohr * expires true if the password expires 18793a7873eSAndreas Gohr * expiresin int seconds until the password expires 18893a7873eSAndreas Gohr * any fields specified in the 'additional' config option 189f4476bd9SJan Schumann * 190f4476bd9SJan Schumann * @author James Van Lommel <james@nosq.com> 19193a7873eSAndreas Gohr * @param string $user 1922046a654SChristopher Smith * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin 19393a7873eSAndreas Gohr * @return array 194f4476bd9SJan Schumann */ 195a4337320SAndreas Gohr public function getUserData($user, $requireGroups = true) 196a4337320SAndreas Gohr { 197f4476bd9SJan Schumann global $conf; 19893a7873eSAndreas Gohr global $lang; 19993a7873eSAndreas Gohr global $ID; 200a4337320SAndreas Gohr $adldap = $this->initAdLdap($this->getUserDomain($user)); 201a4337320SAndreas Gohr if (!$adldap) return array(); 202f4476bd9SJan Schumann 20393a7873eSAndreas Gohr if ($user == '') return array(); 20493a7873eSAndreas Gohr 20593a7873eSAndreas Gohr $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol'); 206f4476bd9SJan Schumann 207f4476bd9SJan Schumann // add additional fields to read 20832fd494aSAndreas Gohr $fields = array_merge($fields, $this->conf['additional']); 209f4476bd9SJan Schumann $fields = array_unique($fields); 21014642325SAndreas Gohr $fields = array_filter($fields); 211f4476bd9SJan Schumann 212f4476bd9SJan Schumann //get info for given user 213a4337320SAndreas Gohr $result = $adldap->user()->info($this->getUserName($user), $fields); 21493a7873eSAndreas Gohr if ($result == false) { 21593a7873eSAndreas Gohr return array(); 21693a7873eSAndreas Gohr } 21793a7873eSAndreas Gohr 218f4476bd9SJan Schumann //general user info 21959bc3b48SGerrit Uitslag $info = array(); 220f4476bd9SJan Schumann $info['name'] = $result[0]['displayname'][0]; 221f4476bd9SJan Schumann $info['mail'] = $result[0]['mail'][0]; 222f4476bd9SJan Schumann $info['uid'] = $result[0]['samaccountname'][0]; 223f4476bd9SJan Schumann $info['dn'] = $result[0]['dn']; 22493a7873eSAndreas Gohr //last password set (Windows counts from January 1st 1601) 22593a7873eSAndreas Gohr $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600; 22693a7873eSAndreas Gohr //will it expire? 22793a7873eSAndreas Gohr $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD 228f4476bd9SJan Schumann 229f4476bd9SJan Schumann // additional information 23032fd494aSAndreas Gohr foreach ($this->conf['additional'] as $field) { 231f4476bd9SJan Schumann if (isset($result[0][strtolower($field)])) { 232f4476bd9SJan Schumann $info[$field] = $result[0][strtolower($field)][0]; 233f4476bd9SJan Schumann } 234f4476bd9SJan Schumann } 235f4476bd9SJan Schumann 236f4476bd9SJan Schumann // handle ActiveDirectory memberOf 237a4337320SAndreas Gohr $info['grps'] = $adldap->user()->groups($this->getUserName($user), (bool) $this->opts['recursive_groups']); 238f4476bd9SJan Schumann 239f4476bd9SJan Schumann if (is_array($info['grps'])) { 240f4476bd9SJan Schumann foreach ($info['grps'] as $ndx => $group) { 241f4476bd9SJan Schumann $info['grps'][$ndx] = $this->cleanGroup($group); 242f4476bd9SJan Schumann } 243f4476bd9SJan Schumann } 244f4476bd9SJan Schumann 245f4476bd9SJan Schumann // always add the default group to the list of groups 246f4476bd9SJan Schumann if (!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) { 247f4476bd9SJan Schumann $info['grps'][] = $conf['defaultgroup']; 248f4476bd9SJan Schumann } 249f4476bd9SJan Schumann 25093a7873eSAndreas Gohr // add the user's domain to the groups 251a4337320SAndreas Gohr $domain = $this->getUserDomain($user); 25293a7873eSAndreas Gohr if ($domain && !in_array("domain-$domain", (array) $info['grps'])) { 25393a7873eSAndreas Gohr $info['grps'][] = $this->cleanGroup("domain-$domain"); 25493a7873eSAndreas Gohr } 25593a7873eSAndreas Gohr 25693a7873eSAndreas Gohr // check expiry time 25732fd494aSAndreas Gohr if ($info['expires'] && $this->conf['expirywarn']) { 258a4337320SAndreas Gohr try { 2591e52e72aSAndreas Gohr $expiry = $adldap->user()->passwordExpiry($user); 2601e52e72aSAndreas Gohr if (is_array($expiry)) { 2611e52e72aSAndreas Gohr $info['expiresat'] = $expiry['expiryts']; 2621e52e72aSAndreas Gohr $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60)); 26393a7873eSAndreas Gohr 26493a7873eSAndreas Gohr // if this is the current user, warn him (once per request only) 26593a7873eSAndreas Gohr if (($_SERVER['REMOTE_USER'] == $user) && 2661e52e72aSAndreas Gohr ($info['expiresin'] <= $this->conf['expirywarn']) && 26793a7873eSAndreas Gohr !$this->msgshown 26893a7873eSAndreas Gohr ) { 2695b795a65SPatrick Brown $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']); 27093a7873eSAndreas Gohr if ($this->canDo('modPass')) { 27193a7873eSAndreas Gohr $url = wl($ID, array('do'=> 'profile')); 27293a7873eSAndreas Gohr $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>'; 27393a7873eSAndreas Gohr } 27493a7873eSAndreas Gohr msg($msg); 27593a7873eSAndreas Gohr $this->msgshown = true; 27693a7873eSAndreas Gohr } 27793a7873eSAndreas Gohr } 278a4337320SAndreas Gohr } catch (adLDAPException $e) { 279a4337320SAndreas Gohr // ignore. should usually not happen 280a4337320SAndreas Gohr } 2811e52e72aSAndreas Gohr } 28293a7873eSAndreas Gohr 283f4476bd9SJan Schumann return $info; 284f4476bd9SJan Schumann } 285f4476bd9SJan Schumann 286f4476bd9SJan Schumann /** 287f4476bd9SJan Schumann * Make AD group names usable by DokuWiki. 288f4476bd9SJan Schumann * 289f4476bd9SJan Schumann * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores. 290f4476bd9SJan Schumann * 291f4476bd9SJan Schumann * @author James Van Lommel (jamesvl@gmail.com) 29293a7873eSAndreas Gohr * @param string $group 29393a7873eSAndreas Gohr * @return string 294f4476bd9SJan Schumann */ 295a4337320SAndreas Gohr public function cleanGroup($group) 296a4337320SAndreas Gohr { 29793a7873eSAndreas Gohr $group = str_replace('\\', '', $group); 29893a7873eSAndreas Gohr $group = str_replace('#', '', $group); 29993a7873eSAndreas Gohr $group = preg_replace('[\s]', '_', $group); 300*8cbc5ee8SAndreas Gohr $group = \dokuwiki\Utf8\PhpString::strtolower(trim($group)); 30193a7873eSAndreas Gohr return $group; 302f4476bd9SJan Schumann } 303f4476bd9SJan Schumann 304f4476bd9SJan Schumann /** 305f4476bd9SJan Schumann * Sanitize user names 30693a7873eSAndreas Gohr * 30793a7873eSAndreas Gohr * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup) 30893a7873eSAndreas Gohr * 30993a7873eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 31093a7873eSAndreas Gohr * @param string $user 31193a7873eSAndreas Gohr * @return string 312f4476bd9SJan Schumann */ 313a4337320SAndreas Gohr public function cleanUser($user) 314a4337320SAndreas Gohr { 31593a7873eSAndreas Gohr $domain = ''; 31693a7873eSAndreas Gohr 31793a7873eSAndreas Gohr // get NTLM or Kerberos domain part 31893a7873eSAndreas Gohr list($dom, $user) = explode('\\', $user, 2); 31993a7873eSAndreas Gohr if (!$user) $user = $dom; 32093a7873eSAndreas Gohr if ($dom) $domain = $dom; 32193a7873eSAndreas Gohr list($user, $dom) = explode('@', $user, 2); 32293a7873eSAndreas Gohr if ($dom) $domain = $dom; 32393a7873eSAndreas Gohr 32493a7873eSAndreas Gohr // clean up both 325*8cbc5ee8SAndreas Gohr $domain = \dokuwiki\Utf8\PhpString::strtolower(trim($domain)); 326*8cbc5ee8SAndreas Gohr $user = \dokuwiki\Utf8\PhpString::strtolower(trim($user)); 32793a7873eSAndreas Gohr 32893a7873eSAndreas Gohr // is this a known, valid domain? if not discard 32932fd494aSAndreas Gohr if (!is_array($this->conf[$domain])) { 33093a7873eSAndreas Gohr $domain = ''; 33193a7873eSAndreas Gohr } 33293a7873eSAndreas Gohr 33393a7873eSAndreas Gohr // reattach domain 33493a7873eSAndreas Gohr if ($domain) $user = "$user@$domain"; 33593a7873eSAndreas Gohr return $user; 336f4476bd9SJan Schumann } 337f4476bd9SJan Schumann 338f4476bd9SJan Schumann /** 339f4476bd9SJan Schumann * Most values in LDAP are case-insensitive 34093a7873eSAndreas Gohr * 34193a7873eSAndreas Gohr * @return bool 342f4476bd9SJan Schumann */ 343a4337320SAndreas Gohr public function isCaseSensitive() 344a4337320SAndreas Gohr { 345f4476bd9SJan Schumann return false; 346f4476bd9SJan Schumann } 347f4476bd9SJan Schumann 3486fcf992cSMichael Große /** 3497910cbbbSMichael Große * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true) 3507910cbbbSMichael Große * 3516fcf992cSMichael Große * @param array $filter 3526fcf992cSMichael Große * @return string 3536fcf992cSMichael Große */ 354a4337320SAndreas Gohr protected function constructSearchString($filter) 355a4337320SAndreas Gohr { 35667a31a83SMichael Große if (!$filter) { 35767a31a83SMichael Große return '*'; 35867a31a83SMichael Große } 359a4337320SAndreas Gohr $adldapUtils = new adLDAPUtils($this->initAdLdap(null)); 36067a31a83SMichael Große $result = '*'; 36167a31a83SMichael Große if (isset($filter['name'])) { 36207aec029SMichael Große $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*'; 36367a31a83SMichael Große unset($filter['name']); 36467a31a83SMichael Große } 3657910cbbbSMichael Große 36667a31a83SMichael Große if (isset($filter['user'])) { 36707aec029SMichael Große $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*'; 36867a31a83SMichael Große unset($filter['user']); 36967a31a83SMichael Große } 37067a31a83SMichael Große 37167a31a83SMichael Große if (isset($filter['mail'])) { 37207aec029SMichael Große $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*'; 37367a31a83SMichael Große unset($filter['mail']); 37467a31a83SMichael Große } 37567a31a83SMichael Große return $result; 37667a31a83SMichael Große } 37767a31a83SMichael Große 378f4476bd9SJan Schumann /** 3797910cbbbSMichael Große * Return a count of the number of user which meet $filter criteria 3807910cbbbSMichael Große * 3817910cbbbSMichael Große * @param array $filter $filter array of field/pattern pairs, empty array for no filter 3827910cbbbSMichael Große * @return int number of users 38325f80763SMichael Große */ 384a4337320SAndreas Gohr public function getUserCount($filter = array()) 385a4337320SAndreas Gohr { 386a4337320SAndreas Gohr $adldap = $this->initAdLdap(null); 3876fcf992cSMichael Große if (!$adldap) { 3886fcf992cSMichael Große dbglog("authad/auth.php getUserCount(): _adldap not set."); 3896fcf992cSMichael Große return -1; 3906fcf992cSMichael Große } 39167a31a83SMichael Große if ($filter == array()) { 39225f80763SMichael Große $result = $adldap->user()->all(); 39367a31a83SMichael Große } else { 394a4337320SAndreas Gohr $searchString = $this->constructSearchString($filter); 39567a31a83SMichael Große $result = $adldap->user()->all(false, $searchString); 396c52f6cd2SMichael Große if (isset($filter['grps'])) { 397c52f6cd2SMichael Große $this->users = array_fill_keys($result, false); 398a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 399c52f6cd2SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 400462e9e37SMichael Große $usermanager->setLastdisabled(true); 401a4337320SAndreas Gohr if (!isset($this->grpsusers[$this->filterToString($filter)])) { 402a4337320SAndreas Gohr $this->fillGroupUserArray($filter, $usermanager->getStart() + 3*$usermanager->getPagesize()); 403a4337320SAndreas Gohr } elseif (count($this->grpsusers[$this->filterToString($filter)]) < 40464159a61SAndreas Gohr $usermanager->getStart() + 3*$usermanager->getPagesize() 40564159a61SAndreas Gohr ) { 406a4337320SAndreas Gohr $this->fillGroupUserArray( 40764159a61SAndreas Gohr $filter, 40864159a61SAndreas Gohr $usermanager->getStart() + 40964159a61SAndreas Gohr 3*$usermanager->getPagesize() - 410a4337320SAndreas Gohr count($this->grpsusers[$this->filterToString($filter)]) 41164159a61SAndreas Gohr ); 412c52f6cd2SMichael Große } 413a4337320SAndreas Gohr $result = $this->grpsusers[$this->filterToString($filter)]; 414462e9e37SMichael Große } else { 415a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 416462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 417462e9e37SMichael Große $usermanager->setLastdisabled(false); 418c52f6cd2SMichael Große } 41967a31a83SMichael Große } 42067a31a83SMichael Große 42125f80763SMichael Große if (!$result) { 4226fcf992cSMichael Große return 0; 42325f80763SMichael Große } 42425f80763SMichael Große return count($result); 42525f80763SMichael Große } 42625f80763SMichael Große 4276fcf992cSMichael Große /** 4286fcf992cSMichael Große * 4296fcf992cSMichael Große * create a unique string for each filter used with a group 4306fcf992cSMichael Große * 4316fcf992cSMichael Große * @param array $filter 4326fcf992cSMichael Große * @return string 4336fcf992cSMichael Große */ 434a4337320SAndreas Gohr protected function filterToString($filter) 435a4337320SAndreas Gohr { 436c52f6cd2SMichael Große $result = ''; 437c52f6cd2SMichael Große if (isset($filter['user'])) { 438c52f6cd2SMichael Große $result .= 'user-' . $filter['user']; 439c52f6cd2SMichael Große } 440c52f6cd2SMichael Große if (isset($filter['name'])) { 441c52f6cd2SMichael Große $result .= 'name-' . $filter['name']; 442c52f6cd2SMichael Große } 443c52f6cd2SMichael Große if (isset($filter['mail'])) { 444c52f6cd2SMichael Große $result .= 'mail-' . $filter['mail']; 445c52f6cd2SMichael Große } 446c52f6cd2SMichael Große if (isset($filter['grps'])) { 447c52f6cd2SMichael Große $result .= 'grps-' . $filter['grps']; 448c52f6cd2SMichael Große } 449c52f6cd2SMichael Große return $result; 450c52f6cd2SMichael Große } 451c52f6cd2SMichael Große 4526fcf992cSMichael Große /** 4537910cbbbSMichael Große * Create an array of $numberOfAdds users passing a certain $filter, including belonging 4547910cbbbSMichael Große * to a certain group and save them to a object-wide array. If the array 4557910cbbbSMichael Große * already exists try to add $numberOfAdds further users to it. 4567910cbbbSMichael Große * 4576fcf992cSMichael Große * @param array $filter 4586fcf992cSMichael Große * @param int $numberOfAdds additional number of users requested 4596fcf992cSMichael Große * @return int number of Users actually add to Array 4606fcf992cSMichael Große */ 461a4337320SAndreas Gohr protected function fillGroupUserArray($filter, $numberOfAdds) 462a4337320SAndreas Gohr { 463a4337320SAndreas Gohr $this->grpsusers[$this->filterToString($filter)]; 464c52f6cd2SMichael Große $i = 0; 465c52f6cd2SMichael Große $count = 0; 466a4337320SAndreas Gohr $this->constructPattern($filter); 467c52f6cd2SMichael Große foreach ($this->users as $user => &$info) { 468a4337320SAndreas Gohr if ($i++ < $this->actualstart) { 469c52f6cd2SMichael Große continue; 470c52f6cd2SMichael Große } 471c52f6cd2SMichael Große if ($info === false) { 472c52f6cd2SMichael Große $info = $this->getUserData($user); 473c52f6cd2SMichael Große } 474a4337320SAndreas Gohr if ($this->filter($user, $info)) { 475a4337320SAndreas Gohr $this->grpsusers[$this->filterToString($filter)][$user] = $info; 476c52f6cd2SMichael Große if (($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break; 477c52f6cd2SMichael Große } 478c52f6cd2SMichael Große } 479a4337320SAndreas Gohr $this->actualstart = $i; 480c52f6cd2SMichael Große return $count; 481c52f6cd2SMichael Große } 482c52f6cd2SMichael Große 48325f80763SMichael Große /** 484f4476bd9SJan Schumann * Bulk retrieval of user data 485f4476bd9SJan Schumann * 486f4476bd9SJan Schumann * @author Dominik Eckelmann <dokuwiki@cosmocode.de> 487253d4b48SGerrit Uitslag * 48893a7873eSAndreas Gohr * @param int $start index of first user to be returned 48993a7873eSAndreas Gohr * @param int $limit max number of users to be returned 49093a7873eSAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 49193a7873eSAndreas Gohr * @return array userinfo (refer getUserData for internal userinfo details) 492f4476bd9SJan Schumann */ 493a4337320SAndreas Gohr public function retrieveUsers($start = 0, $limit = 0, $filter = array()) 494a4337320SAndreas Gohr { 495a4337320SAndreas Gohr $adldap = $this->initAdLdap(null); 496a4337320SAndreas Gohr if (!$adldap) return array(); 497f4476bd9SJan Schumann 4980ba750c0SAndreas Gohr if (!$this->users) { 499f4476bd9SJan Schumann //get info for given user 500a4337320SAndreas Gohr $result = $adldap->user()->all(false, $this->constructSearchString($filter)); 501f4476bd9SJan Schumann if (!$result) return array(); 502f4476bd9SJan Schumann $this->users = array_fill_keys($result, false); 503f4476bd9SJan Schumann } 504f4476bd9SJan Schumann 505f4476bd9SJan Schumann $i = 0; 506f4476bd9SJan Schumann $count = 0; 507f4476bd9SJan Schumann $result = array(); 508f4476bd9SJan Schumann 509c52f6cd2SMichael Große if (!isset($filter['grps'])) { 510a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 511462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 512462e9e37SMichael Große $usermanager->setLastdisabled(false); 513a4337320SAndreas Gohr $this->constructPattern($filter); 514f4476bd9SJan Schumann foreach ($this->users as $user => &$info) { 515f4476bd9SJan Schumann if ($i++ < $start) { 516f4476bd9SJan Schumann continue; 517f4476bd9SJan Schumann } 518f4476bd9SJan Schumann if ($info === false) { 519f4476bd9SJan Schumann $info = $this->getUserData($user); 520f4476bd9SJan Schumann } 521f4476bd9SJan Schumann $result[$user] = $info; 5229a2c73e8SAndreas Gohr if (($limit > 0) && (++$count >= $limit)) break; 523f4476bd9SJan Schumann } 524c52f6cd2SMichael Große } else { 525a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 526462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 527462e9e37SMichael Große $usermanager->setLastdisabled(true); 528a4337320SAndreas Gohr if (!isset($this->grpsusers[$this->filterToString($filter)]) || 529a4337320SAndreas Gohr count($this->grpsusers[$this->filterToString($filter)]) < ($start+$limit) 53064159a61SAndreas Gohr ) { 531a4337320SAndreas Gohr $this->fillGroupUserArray( 53264159a61SAndreas Gohr $filter, 533a4337320SAndreas Gohr $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1 53464159a61SAndreas Gohr ); 535c52f6cd2SMichael Große } 536a4337320SAndreas Gohr if (!$this->grpsusers[$this->filterToString($filter)]) return array(); 537a4337320SAndreas Gohr foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) { 538c52f6cd2SMichael Große if ($i++ < $start) { 539c52f6cd2SMichael Große continue; 540c52f6cd2SMichael Große } 541c52f6cd2SMichael Große $result[$user] = $info; 542c52f6cd2SMichael Große if (($limit > 0) && (++$count >= $limit)) break; 543c52f6cd2SMichael Große } 544c52f6cd2SMichael Große } 545f4476bd9SJan Schumann return $result; 546f4476bd9SJan Schumann } 547f4476bd9SJan Schumann 548f4476bd9SJan Schumann /** 549f4476bd9SJan Schumann * Modify user data 550f4476bd9SJan Schumann * 55193a7873eSAndreas Gohr * @param string $user nick of the user to be changed 55293a7873eSAndreas Gohr * @param array $changes array of field/value pairs to be changed 553f4476bd9SJan Schumann * @return bool 554f4476bd9SJan Schumann */ 555a4337320SAndreas Gohr public function modifyUser($user, $changes) 556a4337320SAndreas Gohr { 557f4476bd9SJan Schumann $return = true; 558a4337320SAndreas Gohr $adldap = $this->initAdLdap($this->getUserDomain($user)); 5598f03c311SPatrick Brown if (!$adldap) { 5608f03c311SPatrick Brown msg($this->getLang('connectfail'), -1); 5618f03c311SPatrick Brown return false; 5628f03c311SPatrick Brown } 563f4476bd9SJan Schumann 564f4476bd9SJan Schumann // password changing 565f4476bd9SJan Schumann if (isset($changes['pass'])) { 566f4476bd9SJan Schumann try { 567a4337320SAndreas Gohr $return = $adldap->user()->password($this->getUserName($user), $changes['pass']); 568f4476bd9SJan Schumann } catch (adLDAPException $e) { 56932fd494aSAndreas Gohr if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); 570f4476bd9SJan Schumann $return = false; 571f4476bd9SJan Schumann } 5728f03c311SPatrick Brown if (!$return) msg($this->getLang('passchangefail'), -1); 573f4476bd9SJan Schumann } 574f4476bd9SJan Schumann 575f4476bd9SJan Schumann // changing user data 576f4476bd9SJan Schumann $adchanges = array(); 577f4476bd9SJan Schumann if (isset($changes['name'])) { 578f4476bd9SJan Schumann // get first and last name 579f4476bd9SJan Schumann $parts = explode(' ', $changes['name']); 580f4476bd9SJan Schumann $adchanges['surname'] = array_pop($parts); 581f4476bd9SJan Schumann $adchanges['firstname'] = join(' ', $parts); 582f4476bd9SJan Schumann $adchanges['display_name'] = $changes['name']; 583f4476bd9SJan Schumann } 584f4476bd9SJan Schumann if (isset($changes['mail'])) { 585f4476bd9SJan Schumann $adchanges['email'] = $changes['mail']; 586f4476bd9SJan Schumann } 587f4476bd9SJan Schumann if (count($adchanges)) { 588f4476bd9SJan Schumann try { 589a4337320SAndreas Gohr $return = $return & $adldap->user()->modify($this->getUserName($user), $adchanges); 590f4476bd9SJan Schumann } catch (adLDAPException $e) { 59132fd494aSAndreas Gohr if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); 592f4476bd9SJan Schumann $return = false; 593f4476bd9SJan Schumann } 5948f03c311SPatrick Brown if (!$return) msg($this->getLang('userchangefail'), -1); 595f4476bd9SJan Schumann } 596f4476bd9SJan Schumann 597f4476bd9SJan Schumann return $return; 598f4476bd9SJan Schumann } 599f4476bd9SJan Schumann 600f4476bd9SJan Schumann /** 601f4476bd9SJan Schumann * Initialize the AdLDAP library and connect to the server 60293a7873eSAndreas Gohr * 60393a7873eSAndreas Gohr * When you pass null as domain, it will reuse any existing domain. 60493a7873eSAndreas Gohr * Eg. the one of the logged in user. It falls back to the default 60593a7873eSAndreas Gohr * domain if no current one is available. 60693a7873eSAndreas Gohr * 60793a7873eSAndreas Gohr * @param string|null $domain The AD domain to use 60893a7873eSAndreas Gohr * @return adLDAP|bool true if a connection was established 609f4476bd9SJan Schumann */ 610a4337320SAndreas Gohr protected function initAdLdap($domain) 611a4337320SAndreas Gohr { 61293a7873eSAndreas Gohr if (is_null($domain) && is_array($this->opts)) { 61393a7873eSAndreas Gohr $domain = $this->opts['domain']; 61493a7873eSAndreas Gohr } 61593a7873eSAndreas Gohr 616a4337320SAndreas Gohr $this->opts = $this->loadServerConfig((string) $domain); 61793a7873eSAndreas Gohr if (isset($this->adldap[$domain])) return $this->adldap[$domain]; 618f4476bd9SJan Schumann 619f4476bd9SJan Schumann // connect 620f4476bd9SJan Schumann try { 62193a7873eSAndreas Gohr $this->adldap[$domain] = new adLDAP($this->opts); 62293a7873eSAndreas Gohr return $this->adldap[$domain]; 623a4337320SAndreas Gohr } catch (Exception $e) { 62432fd494aSAndreas Gohr if ($this->conf['debug']) { 625f4476bd9SJan Schumann msg('AD Auth: '.$e->getMessage(), -1); 626f4476bd9SJan Schumann } 627f4476bd9SJan Schumann $this->success = false; 62893a7873eSAndreas Gohr $this->adldap[$domain] = null; 629f4476bd9SJan Schumann } 630f4476bd9SJan Schumann return false; 631f4476bd9SJan Schumann } 632f4476bd9SJan Schumann 633f4476bd9SJan Schumann /** 63493a7873eSAndreas Gohr * Get the domain part from a user 635f4476bd9SJan Schumann * 636253d4b48SGerrit Uitslag * @param string $user 63793a7873eSAndreas Gohr * @return string 638f4476bd9SJan Schumann */ 639a4337320SAndreas Gohr public function getUserDomain($user) 640a4337320SAndreas Gohr { 64193a7873eSAndreas Gohr list(, $domain) = explode('@', $user, 2); 64293a7873eSAndreas Gohr return $domain; 643f4476bd9SJan Schumann } 644f4476bd9SJan Schumann 64593a7873eSAndreas Gohr /** 64693a7873eSAndreas Gohr * Get the user part from a user 64793a7873eSAndreas Gohr * 648253d4b48SGerrit Uitslag * @param string $user 64993a7873eSAndreas Gohr * @return string 65093a7873eSAndreas Gohr */ 651a4337320SAndreas Gohr public function getUserName($user) 652a4337320SAndreas Gohr { 65393a7873eSAndreas Gohr list($name) = explode('@', $user, 2); 65493a7873eSAndreas Gohr return $name; 65593a7873eSAndreas Gohr } 65693a7873eSAndreas Gohr 65793a7873eSAndreas Gohr /** 65893a7873eSAndreas Gohr * Fetch the configuration for the given AD domain 65993a7873eSAndreas Gohr * 66093a7873eSAndreas Gohr * @param string $domain current AD domain 66193a7873eSAndreas Gohr * @return array 66293a7873eSAndreas Gohr */ 663a4337320SAndreas Gohr protected function loadServerConfig($domain) 664a4337320SAndreas Gohr { 66593a7873eSAndreas Gohr // prepare adLDAP standard configuration 66632fd494aSAndreas Gohr $opts = $this->conf; 66793a7873eSAndreas Gohr 66893a7873eSAndreas Gohr $opts['domain'] = $domain; 66993a7873eSAndreas Gohr 67093a7873eSAndreas Gohr // add possible domain specific configuration 67132fd494aSAndreas Gohr if ($domain && is_array($this->conf[$domain])) foreach ($this->conf[$domain] as $key => $val) { 67293a7873eSAndreas Gohr $opts[$key] = $val; 67393a7873eSAndreas Gohr } 67493a7873eSAndreas Gohr 67593a7873eSAndreas Gohr // handle multiple AD servers 67693a7873eSAndreas Gohr $opts['domain_controllers'] = explode(',', $opts['domain_controllers']); 67793a7873eSAndreas Gohr $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']); 67893a7873eSAndreas Gohr $opts['domain_controllers'] = array_filter($opts['domain_controllers']); 67993a7873eSAndreas Gohr 6808257d713SAndreas Gohr // compatibility with old option name 68164159a61SAndreas Gohr if (empty($opts['admin_username']) && !empty($opts['ad_username'])) { 68264159a61SAndreas Gohr $opts['admin_username'] = $opts['ad_username']; 68364159a61SAndreas Gohr } 68464159a61SAndreas Gohr if (empty($opts['admin_password']) && !empty($opts['ad_password'])) { 68564159a61SAndreas Gohr $opts['admin_password'] = $opts['ad_password']; 68664159a61SAndreas Gohr } 687342753d2SAndreas Gohr $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate 6888257d713SAndreas Gohr 68993a7873eSAndreas Gohr // we can change the password if SSL is set 69093a7873eSAndreas Gohr if ($opts['use_ssl'] || $opts['use_tls']) { 69193a7873eSAndreas Gohr $this->cando['modPass'] = true; 69293a7873eSAndreas Gohr } else { 69393a7873eSAndreas Gohr $this->cando['modPass'] = false; 69493a7873eSAndreas Gohr } 69593a7873eSAndreas Gohr 69612d195abSAndreas Gohr // adLDAP expects empty user/pass as NULL, we're less strict FS#2781 69712d195abSAndreas Gohr if (empty($opts['admin_username'])) $opts['admin_username'] = null; 69812d195abSAndreas Gohr if (empty($opts['admin_password'])) $opts['admin_password'] = null; 69912d195abSAndreas Gohr 70012d195abSAndreas Gohr // user listing needs admin priviledges 7018257d713SAndreas Gohr if (!empty($opts['admin_username']) && !empty($opts['admin_password'])) { 70293a7873eSAndreas Gohr $this->cando['getUsers'] = true; 70393a7873eSAndreas Gohr } else { 7041b228d28SKlap-in $this->cando['getUsers'] = false; 70593a7873eSAndreas Gohr } 70693a7873eSAndreas Gohr 70793a7873eSAndreas Gohr return $opts; 70893a7873eSAndreas Gohr } 70993a7873eSAndreas Gohr 71093a7873eSAndreas Gohr /** 711741b8a48SAndreas Gohr * Returns a list of configured domains 712741b8a48SAndreas Gohr * 713741b8a48SAndreas Gohr * The default domain has an empty string as key 714741b8a48SAndreas Gohr * 715741b8a48SAndreas Gohr * @return array associative array(key => domain) 716741b8a48SAndreas Gohr */ 717a4337320SAndreas Gohr public function getConfiguredDomains() 718a4337320SAndreas Gohr { 719741b8a48SAndreas Gohr $domains = array(); 720741b8a48SAndreas Gohr if (empty($this->conf['account_suffix'])) return $domains; // not configured yet 721741b8a48SAndreas Gohr 722741b8a48SAndreas Gohr // add default domain, using the name from account suffix 723741b8a48SAndreas Gohr $domains[''] = ltrim($this->conf['account_suffix'], '@'); 724741b8a48SAndreas Gohr 725741b8a48SAndreas Gohr // find additional domains 726741b8a48SAndreas Gohr foreach ($this->conf as $key => $val) { 727741b8a48SAndreas Gohr if (is_array($val) && isset($val['account_suffix'])) { 728741b8a48SAndreas Gohr $domains[$key] = ltrim($val['account_suffix'], '@'); 729741b8a48SAndreas Gohr } 730741b8a48SAndreas Gohr } 731741b8a48SAndreas Gohr ksort($domains); 732741b8a48SAndreas Gohr 733741b8a48SAndreas Gohr return $domains; 734741b8a48SAndreas Gohr } 735741b8a48SAndreas Gohr 736741b8a48SAndreas Gohr /** 73793a7873eSAndreas Gohr * Check provided user and userinfo for matching patterns 73893a7873eSAndreas Gohr * 73993a7873eSAndreas Gohr * The patterns are set up with $this->_constructPattern() 74093a7873eSAndreas Gohr * 74193a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 742253d4b48SGerrit Uitslag * 74393a7873eSAndreas Gohr * @param string $user 74493a7873eSAndreas Gohr * @param array $info 74593a7873eSAndreas Gohr * @return bool 74693a7873eSAndreas Gohr */ 747a4337320SAndreas Gohr protected function filter($user, $info) 748a4337320SAndreas Gohr { 749a4337320SAndreas Gohr foreach ($this->pattern as $item => $pattern) { 75093a7873eSAndreas Gohr if ($item == 'user') { 75193a7873eSAndreas Gohr if (!preg_match($pattern, $user)) return false; 75293a7873eSAndreas Gohr } elseif ($item == 'grps') { 75393a7873eSAndreas Gohr if (!count(preg_grep($pattern, $info['grps']))) return false; 75493a7873eSAndreas Gohr } else { 75593a7873eSAndreas Gohr if (!preg_match($pattern, $info[$item])) return false; 75693a7873eSAndreas Gohr } 75793a7873eSAndreas Gohr } 75893a7873eSAndreas Gohr return true; 75993a7873eSAndreas Gohr } 76093a7873eSAndreas Gohr 76193a7873eSAndreas Gohr /** 76293a7873eSAndreas Gohr * Create a pattern for $this->_filter() 76393a7873eSAndreas Gohr * 76493a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 765253d4b48SGerrit Uitslag * 76693a7873eSAndreas Gohr * @param array $filter 76793a7873eSAndreas Gohr */ 768a4337320SAndreas Gohr protected function constructPattern($filter) 769a4337320SAndreas Gohr { 770a4337320SAndreas Gohr $this->pattern = array(); 771f4476bd9SJan Schumann foreach ($filter as $item => $pattern) { 772a4337320SAndreas Gohr $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters 773f4476bd9SJan Schumann } 774f4476bd9SJan Schumann } 775f4476bd9SJan Schumann} 776