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 $grpsusers = array(); 68c52f6cd2SMichael Große 69f4476bd9SJan Schumann /** 70f4476bd9SJan Schumann * Constructor 71f4476bd9SJan Schumann */ 72a4337320SAndreas Gohr public function __construct() 73a4337320SAndreas Gohr { 7400d58927SMichael Hamann global $INPUT; 75454d868bSAndreas Gohr parent::__construct(); 76454d868bSAndreas Gohr 77a4337320SAndreas Gohr require_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php'); 78a4337320SAndreas Gohr require_once(DOKU_PLUGIN.'authad/adLDAP/classes/adLDAPUtils.php'); 79a4337320SAndreas Gohr 8032fd494aSAndreas Gohr // we load the config early to modify it a bit here 8132fd494aSAndreas Gohr $this->loadConfig(); 82f4476bd9SJan Schumann 83f4476bd9SJan Schumann // additional information fields 8432fd494aSAndreas Gohr if (isset($this->conf['additional'])) { 8532fd494aSAndreas Gohr $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']); 8632fd494aSAndreas Gohr $this->conf['additional'] = explode(',', $this->conf['additional']); 8732fd494aSAndreas Gohr } else $this->conf['additional'] = array(); 88f4476bd9SJan Schumann 89f4476bd9SJan Schumann // ldap extension is needed 90f4476bd9SJan Schumann if (!function_exists('ldap_connect')) { 9132fd494aSAndreas Gohr if ($this->conf['debug']) 92f4476bd9SJan Schumann msg("AD Auth: PHP LDAP extension not found.", -1); 93f4476bd9SJan Schumann $this->success = false; 94f4476bd9SJan Schumann return; 95f4476bd9SJan Schumann } 96f4476bd9SJan Schumann 97f4476bd9SJan Schumann // Prepare SSO 98d34a2a38SAndreas Gohr if (!empty($_SERVER['REMOTE_USER'])) { 99d34a2a38SAndreas Gohr // make sure the right encoding is used 100d34a2a38SAndreas Gohr if ($this->getConf('sso_charset')) { 101d34a2a38SAndreas Gohr $_SERVER['REMOTE_USER'] = iconv($this->getConf('sso_charset'), 'UTF-8', $_SERVER['REMOTE_USER']); 1028cbc5ee8SAndreas Gohr } elseif (!\dokuwiki\Utf8\Clean::isUtf8($_SERVER['REMOTE_USER'])) { 10393a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']); 10493a7873eSAndreas Gohr } 105d34a2a38SAndreas Gohr 106d34a2a38SAndreas Gohr // trust the incoming user 107d34a2a38SAndreas Gohr if ($this->conf['sso']) { 10893a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']); 109f4476bd9SJan Schumann 110f4476bd9SJan Schumann // we need to simulate a login 111f4476bd9SJan Schumann if (empty($_COOKIE[DOKU_COOKIE])) { 11200d58927SMichael Hamann $INPUT->set('u', $_SERVER['REMOTE_USER']); 11300d58927SMichael Hamann $INPUT->set('p', 'sso_only'); 114f4476bd9SJan Schumann } 115f4476bd9SJan Schumann } 116d34a2a38SAndreas Gohr } 117f4476bd9SJan Schumann 11893a7873eSAndreas Gohr // other can do's are changed in $this->_loadServerConfig() base on domain setup 119bb30445dSMichael Wilmes $this->cando['modName'] = (bool)$this->conf['update_name']; 120bb30445dSMichael Wilmes $this->cando['modMail'] = (bool)$this->conf['update_mail']; 12125f80763SMichael Große $this->cando['getUserCount'] = true; 122f4476bd9SJan Schumann } 123f4476bd9SJan Schumann 124f4476bd9SJan Schumann /** 125a154806fSAndreas Gohr * Load domain config on capability check 126a154806fSAndreas Gohr * 127a154806fSAndreas Gohr * @param string $cap 128a154806fSAndreas Gohr * @return bool 129a154806fSAndreas Gohr */ 130a4337320SAndreas Gohr public function canDo($cap) 131a4337320SAndreas Gohr { 132a154806fSAndreas Gohr //capabilities depend on config, which may change depending on domain 133a4337320SAndreas Gohr $domain = $this->getUserDomain($_SERVER['REMOTE_USER']); 134a4337320SAndreas Gohr $this->loadServerConfig($domain); 135a154806fSAndreas Gohr return parent::canDo($cap); 136a154806fSAndreas Gohr } 137a154806fSAndreas Gohr 138a154806fSAndreas Gohr /** 139f4476bd9SJan Schumann * Check user+password [required auth function] 140f4476bd9SJan Schumann * 141f4476bd9SJan Schumann * Checks if the given user exists and the given 142f4476bd9SJan Schumann * plaintext password is correct by trying to bind 143f4476bd9SJan Schumann * to the LDAP server 144f4476bd9SJan Schumann * 145f4476bd9SJan Schumann * @author James Van Lommel <james@nosq.com> 14693a7873eSAndreas Gohr * @param string $user 14793a7873eSAndreas Gohr * @param string $pass 148f4476bd9SJan Schumann * @return bool 149f4476bd9SJan Schumann */ 150a4337320SAndreas Gohr public function checkPass($user, $pass) 151a4337320SAndreas Gohr { 152f4476bd9SJan Schumann if ($_SERVER['REMOTE_USER'] && 153f4476bd9SJan Schumann $_SERVER['REMOTE_USER'] == $user && 15432fd494aSAndreas Gohr $this->conf['sso'] 15593a7873eSAndreas Gohr ) return true; 156f4476bd9SJan Schumann 157a4337320SAndreas Gohr $adldap = $this->initAdLdap($this->getUserDomain($user)); 15893a7873eSAndreas Gohr if (!$adldap) return false; 15993a7873eSAndreas Gohr 160a4337320SAndreas Gohr try { 161a4337320SAndreas Gohr return $adldap->authenticate($this->getUserName($user), $pass); 162a4337320SAndreas Gohr } catch (adLDAPException $e) { 163a4337320SAndreas Gohr // shouldn't really happen 164a4337320SAndreas Gohr return false; 165a4337320SAndreas Gohr } 166f4476bd9SJan Schumann } 167f4476bd9SJan Schumann 168f4476bd9SJan Schumann /** 169f4476bd9SJan Schumann * Return user info [required auth function] 170f4476bd9SJan Schumann * 171f4476bd9SJan Schumann * Returns info about the given user needs to contain 172f4476bd9SJan Schumann * at least these fields: 173f4476bd9SJan Schumann * 174f4476bd9SJan Schumann * name string full name of the user 175f4476bd9SJan Schumann * mail string email address of the user 176f4476bd9SJan Schumann * grps array list of groups the user is in 177f4476bd9SJan Schumann * 17893a7873eSAndreas Gohr * This AD specific function returns the following 179f4476bd9SJan Schumann * addional fields: 180f4476bd9SJan Schumann * 181f4476bd9SJan Schumann * dn string distinguished name (DN) 18293a7873eSAndreas Gohr * uid string samaccountname 18393a7873eSAndreas Gohr * lastpwd int timestamp of the date when the password was set 18493a7873eSAndreas Gohr * expires true if the password expires 18593a7873eSAndreas Gohr * expiresin int seconds until the password expires 18693a7873eSAndreas Gohr * any fields specified in the 'additional' config option 187f4476bd9SJan Schumann * 188f4476bd9SJan Schumann * @author James Van Lommel <james@nosq.com> 18993a7873eSAndreas Gohr * @param string $user 1902046a654SChristopher Smith * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin 19193a7873eSAndreas Gohr * @return array 192f4476bd9SJan Schumann */ 193a4337320SAndreas Gohr public function getUserData($user, $requireGroups = true) 194a4337320SAndreas Gohr { 195f4476bd9SJan Schumann global $conf; 19693a7873eSAndreas Gohr global $lang; 19793a7873eSAndreas Gohr global $ID; 198a4337320SAndreas Gohr $adldap = $this->initAdLdap($this->getUserDomain($user)); 199a4337320SAndreas Gohr if (!$adldap) return array(); 200f4476bd9SJan Schumann 20193a7873eSAndreas Gohr if ($user == '') return array(); 20293a7873eSAndreas Gohr 20393a7873eSAndreas Gohr $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol'); 204f4476bd9SJan Schumann 205f4476bd9SJan Schumann // add additional fields to read 20632fd494aSAndreas Gohr $fields = array_merge($fields, $this->conf['additional']); 207f4476bd9SJan Schumann $fields = array_unique($fields); 20814642325SAndreas Gohr $fields = array_filter($fields); 209f4476bd9SJan Schumann 210f4476bd9SJan Schumann //get info for given user 211a4337320SAndreas Gohr $result = $adldap->user()->info($this->getUserName($user), $fields); 21293a7873eSAndreas Gohr if ($result == false) { 21393a7873eSAndreas Gohr return array(); 21493a7873eSAndreas Gohr } 21593a7873eSAndreas Gohr 216f4476bd9SJan Schumann //general user info 21759bc3b48SGerrit Uitslag $info = array(); 218f4476bd9SJan Schumann $info['name'] = $result[0]['displayname'][0]; 219f4476bd9SJan Schumann $info['mail'] = $result[0]['mail'][0]; 220f4476bd9SJan Schumann $info['uid'] = $result[0]['samaccountname'][0]; 221f4476bd9SJan Schumann $info['dn'] = $result[0]['dn']; 22293a7873eSAndreas Gohr //last password set (Windows counts from January 1st 1601) 22393a7873eSAndreas Gohr $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600; 22493a7873eSAndreas Gohr //will it expire? 22593a7873eSAndreas Gohr $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD 226f4476bd9SJan Schumann 227f4476bd9SJan Schumann // additional information 22832fd494aSAndreas Gohr foreach ($this->conf['additional'] as $field) { 229f4476bd9SJan Schumann if (isset($result[0][strtolower($field)])) { 230f4476bd9SJan Schumann $info[$field] = $result[0][strtolower($field)][0]; 231f4476bd9SJan Schumann } 232f4476bd9SJan Schumann } 233f4476bd9SJan Schumann 234f4476bd9SJan Schumann // handle ActiveDirectory memberOf 235a4337320SAndreas Gohr $info['grps'] = $adldap->user()->groups($this->getUserName($user), (bool) $this->opts['recursive_groups']); 236f4476bd9SJan Schumann 237f4476bd9SJan Schumann if (is_array($info['grps'])) { 238f4476bd9SJan Schumann foreach ($info['grps'] as $ndx => $group) { 239f4476bd9SJan Schumann $info['grps'][$ndx] = $this->cleanGroup($group); 240f4476bd9SJan Schumann } 241f4476bd9SJan Schumann } 242f4476bd9SJan Schumann 243f4476bd9SJan Schumann // always add the default group to the list of groups 244f4476bd9SJan Schumann if (!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) { 245f4476bd9SJan Schumann $info['grps'][] = $conf['defaultgroup']; 246f4476bd9SJan Schumann } 247f4476bd9SJan Schumann 24893a7873eSAndreas Gohr // add the user's domain to the groups 249a4337320SAndreas Gohr $domain = $this->getUserDomain($user); 25093a7873eSAndreas Gohr if ($domain && !in_array("domain-$domain", (array) $info['grps'])) { 25193a7873eSAndreas Gohr $info['grps'][] = $this->cleanGroup("domain-$domain"); 25293a7873eSAndreas Gohr } 25393a7873eSAndreas Gohr 25493a7873eSAndreas Gohr // check expiry time 25532fd494aSAndreas Gohr if ($info['expires'] && $this->conf['expirywarn']) { 256a4337320SAndreas Gohr try { 2571e52e72aSAndreas Gohr $expiry = $adldap->user()->passwordExpiry($user); 2581e52e72aSAndreas Gohr if (is_array($expiry)) { 2591e52e72aSAndreas Gohr $info['expiresat'] = $expiry['expiryts']; 2601e52e72aSAndreas Gohr $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60)); 26193a7873eSAndreas Gohr 26293a7873eSAndreas Gohr // if this is the current user, warn him (once per request only) 26393a7873eSAndreas Gohr if (($_SERVER['REMOTE_USER'] == $user) && 2641e52e72aSAndreas Gohr ($info['expiresin'] <= $this->conf['expirywarn']) && 26593a7873eSAndreas Gohr !$this->msgshown 26693a7873eSAndreas Gohr ) { 2675b795a65SPatrick Brown $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']); 26893a7873eSAndreas Gohr if ($this->canDo('modPass')) { 26993a7873eSAndreas Gohr $url = wl($ID, array('do'=> 'profile')); 27093a7873eSAndreas Gohr $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>'; 27193a7873eSAndreas Gohr } 27293a7873eSAndreas Gohr msg($msg); 27393a7873eSAndreas Gohr $this->msgshown = true; 27493a7873eSAndreas Gohr } 27593a7873eSAndreas Gohr } 276a4337320SAndreas Gohr } catch (adLDAPException $e) { 277a4337320SAndreas Gohr // ignore. should usually not happen 278a4337320SAndreas Gohr } 2791e52e72aSAndreas Gohr } 28093a7873eSAndreas Gohr 281f4476bd9SJan Schumann return $info; 282f4476bd9SJan Schumann } 283f4476bd9SJan Schumann 284f4476bd9SJan Schumann /** 285f4476bd9SJan Schumann * Make AD group names usable by DokuWiki. 286f4476bd9SJan Schumann * 287f4476bd9SJan Schumann * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores. 288f4476bd9SJan Schumann * 289f4476bd9SJan Schumann * @author James Van Lommel (jamesvl@gmail.com) 29093a7873eSAndreas Gohr * @param string $group 29193a7873eSAndreas Gohr * @return string 292f4476bd9SJan Schumann */ 293a4337320SAndreas Gohr public function cleanGroup($group) 294a4337320SAndreas Gohr { 29593a7873eSAndreas Gohr $group = str_replace('\\', '', $group); 29693a7873eSAndreas Gohr $group = str_replace('#', '', $group); 29793a7873eSAndreas Gohr $group = preg_replace('[\s]', '_', $group); 2988cbc5ee8SAndreas Gohr $group = \dokuwiki\Utf8\PhpString::strtolower(trim($group)); 29993a7873eSAndreas Gohr return $group; 300f4476bd9SJan Schumann } 301f4476bd9SJan Schumann 302f4476bd9SJan Schumann /** 303f4476bd9SJan Schumann * Sanitize user names 30493a7873eSAndreas Gohr * 30593a7873eSAndreas Gohr * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup) 30693a7873eSAndreas Gohr * 30793a7873eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 30893a7873eSAndreas Gohr * @param string $user 30993a7873eSAndreas Gohr * @return string 310f4476bd9SJan Schumann */ 311a4337320SAndreas Gohr public function cleanUser($user) 312a4337320SAndreas Gohr { 31393a7873eSAndreas Gohr $domain = ''; 31493a7873eSAndreas Gohr 31593a7873eSAndreas Gohr // get NTLM or Kerberos domain part 31693a7873eSAndreas Gohr list($dom, $user) = explode('\\', $user, 2); 31793a7873eSAndreas Gohr if (!$user) $user = $dom; 31893a7873eSAndreas Gohr if ($dom) $domain = $dom; 31993a7873eSAndreas Gohr list($user, $dom) = explode('@', $user, 2); 32093a7873eSAndreas Gohr if ($dom) $domain = $dom; 32193a7873eSAndreas Gohr 32293a7873eSAndreas Gohr // clean up both 3238cbc5ee8SAndreas Gohr $domain = \dokuwiki\Utf8\PhpString::strtolower(trim($domain)); 3248cbc5ee8SAndreas Gohr $user = \dokuwiki\Utf8\PhpString::strtolower(trim($user)); 32593a7873eSAndreas Gohr 326916ef7cfSAndreas Gohr // is this a known, valid domain or do we work without account suffix? if not discard 327916ef7cfSAndreas Gohr if (!is_array($this->conf[$domain]) && $this->conf['account_suffix'] !== '') { 32893a7873eSAndreas Gohr $domain = ''; 32993a7873eSAndreas Gohr } 33093a7873eSAndreas Gohr 33193a7873eSAndreas Gohr // reattach domain 33293a7873eSAndreas Gohr if ($domain) $user = "$user@$domain"; 33393a7873eSAndreas Gohr return $user; 334f4476bd9SJan Schumann } 335f4476bd9SJan Schumann 336f4476bd9SJan Schumann /** 337f4476bd9SJan Schumann * Most values in LDAP are case-insensitive 33893a7873eSAndreas Gohr * 33993a7873eSAndreas Gohr * @return bool 340f4476bd9SJan Schumann */ 341a4337320SAndreas Gohr public function isCaseSensitive() 342a4337320SAndreas Gohr { 343f4476bd9SJan Schumann return false; 344f4476bd9SJan Schumann } 345f4476bd9SJan Schumann 3466fcf992cSMichael Große /** 3477910cbbbSMichael Große * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true) 3487910cbbbSMichael Große * 3496fcf992cSMichael Große * @param array $filter 3506fcf992cSMichael Große * @return string 3516fcf992cSMichael Große */ 352a4337320SAndreas Gohr protected function constructSearchString($filter) 353a4337320SAndreas Gohr { 35467a31a83SMichael Große if (!$filter) { 35567a31a83SMichael Große return '*'; 35667a31a83SMichael Große } 357a4337320SAndreas Gohr $adldapUtils = new adLDAPUtils($this->initAdLdap(null)); 35867a31a83SMichael Große $result = '*'; 35967a31a83SMichael Große if (isset($filter['name'])) { 36007aec029SMichael Große $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*'; 36167a31a83SMichael Große unset($filter['name']); 36267a31a83SMichael Große } 3637910cbbbSMichael Große 36467a31a83SMichael Große if (isset($filter['user'])) { 36507aec029SMichael Große $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*'; 36667a31a83SMichael Große unset($filter['user']); 36767a31a83SMichael Große } 36867a31a83SMichael Große 36967a31a83SMichael Große if (isset($filter['mail'])) { 37007aec029SMichael Große $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*'; 37167a31a83SMichael Große unset($filter['mail']); 37267a31a83SMichael Große } 37367a31a83SMichael Große return $result; 37467a31a83SMichael Große } 37567a31a83SMichael Große 376f4476bd9SJan Schumann /** 3777910cbbbSMichael Große * Return a count of the number of user which meet $filter criteria 3787910cbbbSMichael Große * 3797910cbbbSMichael Große * @param array $filter $filter array of field/pattern pairs, empty array for no filter 3807910cbbbSMichael Große * @return int number of users 38125f80763SMichael Große */ 382a4337320SAndreas Gohr public function getUserCount($filter = array()) 383a4337320SAndreas Gohr { 384a4337320SAndreas Gohr $adldap = $this->initAdLdap(null); 3856fcf992cSMichael Große if (!$adldap) { 3866fcf992cSMichael Große dbglog("authad/auth.php getUserCount(): _adldap not set."); 3876fcf992cSMichael Große return -1; 3886fcf992cSMichael Große } 38967a31a83SMichael Große if ($filter == array()) { 39025f80763SMichael Große $result = $adldap->user()->all(); 39167a31a83SMichael Große } else { 392a4337320SAndreas Gohr $searchString = $this->constructSearchString($filter); 39367a31a83SMichael Große $result = $adldap->user()->all(false, $searchString); 394c52f6cd2SMichael Große if (isset($filter['grps'])) { 395c52f6cd2SMichael Große $this->users = array_fill_keys($result, false); 396a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 397c52f6cd2SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 398462e9e37SMichael Große $usermanager->setLastdisabled(true); 399a4337320SAndreas Gohr if (!isset($this->grpsusers[$this->filterToString($filter)])) { 400a4337320SAndreas Gohr $this->fillGroupUserArray($filter, $usermanager->getStart() + 3*$usermanager->getPagesize()); 401a4337320SAndreas Gohr } elseif (count($this->grpsusers[$this->filterToString($filter)]) < 40264159a61SAndreas Gohr $usermanager->getStart() + 3*$usermanager->getPagesize() 40364159a61SAndreas Gohr ) { 404a4337320SAndreas Gohr $this->fillGroupUserArray( 40564159a61SAndreas Gohr $filter, 40664159a61SAndreas Gohr $usermanager->getStart() + 40764159a61SAndreas Gohr 3*$usermanager->getPagesize() - 408a4337320SAndreas Gohr count($this->grpsusers[$this->filterToString($filter)]) 40964159a61SAndreas Gohr ); 410c52f6cd2SMichael Große } 411a4337320SAndreas Gohr $result = $this->grpsusers[$this->filterToString($filter)]; 412462e9e37SMichael Große } else { 413a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 414462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 415462e9e37SMichael Große $usermanager->setLastdisabled(false); 416c52f6cd2SMichael Große } 41767a31a83SMichael Große } 41867a31a83SMichael Große 41925f80763SMichael Große if (!$result) { 4206fcf992cSMichael Große return 0; 42125f80763SMichael Große } 42225f80763SMichael Große return count($result); 42325f80763SMichael Große } 42425f80763SMichael Große 4256fcf992cSMichael Große /** 4266fcf992cSMichael Große * 4276fcf992cSMichael Große * create a unique string for each filter used with a group 4286fcf992cSMichael Große * 4296fcf992cSMichael Große * @param array $filter 4306fcf992cSMichael Große * @return string 4316fcf992cSMichael Große */ 432a4337320SAndreas Gohr protected function filterToString($filter) 433a4337320SAndreas Gohr { 434c52f6cd2SMichael Große $result = ''; 435c52f6cd2SMichael Große if (isset($filter['user'])) { 436c52f6cd2SMichael Große $result .= 'user-' . $filter['user']; 437c52f6cd2SMichael Große } 438c52f6cd2SMichael Große if (isset($filter['name'])) { 439c52f6cd2SMichael Große $result .= 'name-' . $filter['name']; 440c52f6cd2SMichael Große } 441c52f6cd2SMichael Große if (isset($filter['mail'])) { 442c52f6cd2SMichael Große $result .= 'mail-' . $filter['mail']; 443c52f6cd2SMichael Große } 444c52f6cd2SMichael Große if (isset($filter['grps'])) { 445c52f6cd2SMichael Große $result .= 'grps-' . $filter['grps']; 446c52f6cd2SMichael Große } 447c52f6cd2SMichael Große return $result; 448c52f6cd2SMichael Große } 449c52f6cd2SMichael Große 4506fcf992cSMichael Große /** 4517910cbbbSMichael Große * Create an array of $numberOfAdds users passing a certain $filter, including belonging 4527910cbbbSMichael Große * to a certain group and save them to a object-wide array. If the array 4537910cbbbSMichael Große * already exists try to add $numberOfAdds further users to it. 4547910cbbbSMichael Große * 4556fcf992cSMichael Große * @param array $filter 4566fcf992cSMichael Große * @param int $numberOfAdds additional number of users requested 4576fcf992cSMichael Große * @return int number of Users actually add to Array 4586fcf992cSMichael Große */ 459a4337320SAndreas Gohr protected function fillGroupUserArray($filter, $numberOfAdds) 460a4337320SAndreas Gohr { 461*fdd649a2SAndreas Gohr if (isset($this->grpsusers[$this->filterToString($filter)])) { 462*fdd649a2SAndreas Gohr $actualstart = count($this->grpsusers[$this->filterToString($filter)]); 463*fdd649a2SAndreas Gohr } else { 464*fdd649a2SAndreas Gohr $this->grpsusers[$this->filterToString($filter)] = []; 465*fdd649a2SAndreas Gohr $actualstart = 0; 466*fdd649a2SAndreas Gohr } 467*fdd649a2SAndreas Gohr 468c52f6cd2SMichael Große $i=0; 469c52f6cd2SMichael Große $count = 0; 470a4337320SAndreas Gohr $this->constructPattern($filter); 471c52f6cd2SMichael Große foreach ($this->users as $user => &$info) { 472*fdd649a2SAndreas Gohr if ($i++ < $actualstart) { 473c52f6cd2SMichael Große continue; 474c52f6cd2SMichael Große } 475c52f6cd2SMichael Große if ($info === false) { 476c52f6cd2SMichael Große $info = $this->getUserData($user); 477c52f6cd2SMichael Große } 478a4337320SAndreas Gohr if ($this->filter($user, $info)) { 479a4337320SAndreas Gohr $this->grpsusers[$this->filterToString($filter)][$user] = $info; 480c52f6cd2SMichael Große if (($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break; 481c52f6cd2SMichael Große } 482c52f6cd2SMichael Große } 483c52f6cd2SMichael Große return $count; 484c52f6cd2SMichael Große } 485c52f6cd2SMichael Große 48625f80763SMichael Große /** 487f4476bd9SJan Schumann * Bulk retrieval of user data 488f4476bd9SJan Schumann * 489f4476bd9SJan Schumann * @author Dominik Eckelmann <dokuwiki@cosmocode.de> 490253d4b48SGerrit Uitslag * 49193a7873eSAndreas Gohr * @param int $start index of first user to be returned 49293a7873eSAndreas Gohr * @param int $limit max number of users to be returned 49393a7873eSAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 49493a7873eSAndreas Gohr * @return array userinfo (refer getUserData for internal userinfo details) 495f4476bd9SJan Schumann */ 496a4337320SAndreas Gohr public function retrieveUsers($start = 0, $limit = 0, $filter = array()) 497a4337320SAndreas Gohr { 498a4337320SAndreas Gohr $adldap = $this->initAdLdap(null); 499a4337320SAndreas Gohr if (!$adldap) return array(); 500f4476bd9SJan Schumann 501*fdd649a2SAndreas Gohr //if (!$this->users) { 502f4476bd9SJan Schumann //get info for given user 503a4337320SAndreas Gohr $result = $adldap->user()->all(false, $this->constructSearchString($filter)); 504f4476bd9SJan Schumann if (!$result) return array(); 505f4476bd9SJan Schumann $this->users = array_fill_keys($result, false); 506*fdd649a2SAndreas Gohr //} 507f4476bd9SJan Schumann 508f4476bd9SJan Schumann $i = 0; 509f4476bd9SJan Schumann $count = 0; 510f4476bd9SJan Schumann $result = array(); 511f4476bd9SJan Schumann 512c52f6cd2SMichael Große if (!isset($filter['grps'])) { 513a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 514462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 515462e9e37SMichael Große $usermanager->setLastdisabled(false); 516a4337320SAndreas Gohr $this->constructPattern($filter); 517f4476bd9SJan Schumann foreach ($this->users as $user => &$info) { 518f4476bd9SJan Schumann if ($i++ < $start) { 519f4476bd9SJan Schumann continue; 520f4476bd9SJan Schumann } 521f4476bd9SJan Schumann if ($info === false) { 522f4476bd9SJan Schumann $info = $this->getUserData($user); 523f4476bd9SJan Schumann } 524f4476bd9SJan Schumann $result[$user] = $info; 5259a2c73e8SAndreas Gohr if (($limit > 0) && (++$count >= $limit)) break; 526f4476bd9SJan Schumann } 527c52f6cd2SMichael Große } else { 528a4337320SAndreas Gohr /** @var admin_plugin_usermanager $usermanager */ 529462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 530462e9e37SMichael Große $usermanager->setLastdisabled(true); 531a4337320SAndreas Gohr if (!isset($this->grpsusers[$this->filterToString($filter)]) || 532a4337320SAndreas Gohr count($this->grpsusers[$this->filterToString($filter)]) < ($start+$limit) 53364159a61SAndreas Gohr ) { 534*fdd649a2SAndreas Gohr if(!isset($this->grpsusers[$this->filterToString($filter)])) { 535*fdd649a2SAndreas Gohr $this->grpsusers[$this->filterToString($filter)] = []; 536*fdd649a2SAndreas Gohr } 537*fdd649a2SAndreas Gohr 538a4337320SAndreas Gohr $this->fillGroupUserArray( 53964159a61SAndreas Gohr $filter, 540a4337320SAndreas Gohr $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1 54164159a61SAndreas Gohr ); 542c52f6cd2SMichael Große } 543a4337320SAndreas Gohr if (!$this->grpsusers[$this->filterToString($filter)]) return array(); 544a4337320SAndreas Gohr foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) { 545c52f6cd2SMichael Große if ($i++ < $start) { 546c52f6cd2SMichael Große continue; 547c52f6cd2SMichael Große } 548c52f6cd2SMichael Große $result[$user] = $info; 549c52f6cd2SMichael Große if (($limit > 0) && (++$count >= $limit)) break; 550c52f6cd2SMichael Große } 551c52f6cd2SMichael Große } 552f4476bd9SJan Schumann return $result; 553f4476bd9SJan Schumann } 554f4476bd9SJan Schumann 555f4476bd9SJan Schumann /** 556f4476bd9SJan Schumann * Modify user data 557f4476bd9SJan Schumann * 55893a7873eSAndreas Gohr * @param string $user nick of the user to be changed 55993a7873eSAndreas Gohr * @param array $changes array of field/value pairs to be changed 560f4476bd9SJan Schumann * @return bool 561f4476bd9SJan Schumann */ 562a4337320SAndreas Gohr public function modifyUser($user, $changes) 563a4337320SAndreas Gohr { 564f4476bd9SJan Schumann $return = true; 565a4337320SAndreas Gohr $adldap = $this->initAdLdap($this->getUserDomain($user)); 5668f03c311SPatrick Brown if (!$adldap) { 5678f03c311SPatrick Brown msg($this->getLang('connectfail'), -1); 5688f03c311SPatrick Brown return false; 5698f03c311SPatrick Brown } 570f4476bd9SJan Schumann 571f4476bd9SJan Schumann // password changing 572f4476bd9SJan Schumann if (isset($changes['pass'])) { 573f4476bd9SJan Schumann try { 574a4337320SAndreas Gohr $return = $adldap->user()->password($this->getUserName($user), $changes['pass']); 575f4476bd9SJan Schumann } catch (adLDAPException $e) { 57632fd494aSAndreas Gohr if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); 577f4476bd9SJan Schumann $return = false; 578f4476bd9SJan Schumann } 5798f03c311SPatrick Brown if (!$return) msg($this->getLang('passchangefail'), -1); 580f4476bd9SJan Schumann } 581f4476bd9SJan Schumann 582f4476bd9SJan Schumann // changing user data 583f4476bd9SJan Schumann $adchanges = array(); 584f4476bd9SJan Schumann if (isset($changes['name'])) { 585f4476bd9SJan Schumann // get first and last name 586f4476bd9SJan Schumann $parts = explode(' ', $changes['name']); 587f4476bd9SJan Schumann $adchanges['surname'] = array_pop($parts); 588f4476bd9SJan Schumann $adchanges['firstname'] = join(' ', $parts); 589f4476bd9SJan Schumann $adchanges['display_name'] = $changes['name']; 590f4476bd9SJan Schumann } 591f4476bd9SJan Schumann if (isset($changes['mail'])) { 592f4476bd9SJan Schumann $adchanges['email'] = $changes['mail']; 593f4476bd9SJan Schumann } 594f4476bd9SJan Schumann if (count($adchanges)) { 595f4476bd9SJan Schumann try { 596a4337320SAndreas Gohr $return = $return & $adldap->user()->modify($this->getUserName($user), $adchanges); 597f4476bd9SJan Schumann } catch (adLDAPException $e) { 59832fd494aSAndreas Gohr if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); 599f4476bd9SJan Schumann $return = false; 600f4476bd9SJan Schumann } 6018f03c311SPatrick Brown if (!$return) msg($this->getLang('userchangefail'), -1); 602f4476bd9SJan Schumann } 603f4476bd9SJan Schumann 604f4476bd9SJan Schumann return $return; 605f4476bd9SJan Schumann } 606f4476bd9SJan Schumann 607f4476bd9SJan Schumann /** 608f4476bd9SJan Schumann * Initialize the AdLDAP library and connect to the server 60993a7873eSAndreas Gohr * 61093a7873eSAndreas Gohr * When you pass null as domain, it will reuse any existing domain. 61193a7873eSAndreas Gohr * Eg. the one of the logged in user. It falls back to the default 61293a7873eSAndreas Gohr * domain if no current one is available. 61393a7873eSAndreas Gohr * 61493a7873eSAndreas Gohr * @param string|null $domain The AD domain to use 61593a7873eSAndreas Gohr * @return adLDAP|bool true if a connection was established 616f4476bd9SJan Schumann */ 617a4337320SAndreas Gohr protected function initAdLdap($domain) 618a4337320SAndreas Gohr { 61993a7873eSAndreas Gohr if (is_null($domain) && is_array($this->opts)) { 62093a7873eSAndreas Gohr $domain = $this->opts['domain']; 62193a7873eSAndreas Gohr } 62293a7873eSAndreas Gohr 623a4337320SAndreas Gohr $this->opts = $this->loadServerConfig((string) $domain); 62493a7873eSAndreas Gohr if (isset($this->adldap[$domain])) return $this->adldap[$domain]; 625f4476bd9SJan Schumann 626f4476bd9SJan Schumann // connect 627f4476bd9SJan Schumann try { 62893a7873eSAndreas Gohr $this->adldap[$domain] = new adLDAP($this->opts); 62993a7873eSAndreas Gohr return $this->adldap[$domain]; 630a4337320SAndreas Gohr } catch (Exception $e) { 63132fd494aSAndreas Gohr if ($this->conf['debug']) { 632f4476bd9SJan Schumann msg('AD Auth: '.$e->getMessage(), -1); 633f4476bd9SJan Schumann } 634f4476bd9SJan Schumann $this->success = false; 63593a7873eSAndreas Gohr $this->adldap[$domain] = null; 636f4476bd9SJan Schumann } 637f4476bd9SJan Schumann return false; 638f4476bd9SJan Schumann } 639f4476bd9SJan Schumann 640f4476bd9SJan Schumann /** 64193a7873eSAndreas Gohr * Get the domain part from a user 642f4476bd9SJan Schumann * 643253d4b48SGerrit Uitslag * @param string $user 64493a7873eSAndreas Gohr * @return string 645f4476bd9SJan Schumann */ 646a4337320SAndreas Gohr public function getUserDomain($user) 647a4337320SAndreas Gohr { 64893a7873eSAndreas Gohr list(, $domain) = explode('@', $user, 2); 64993a7873eSAndreas Gohr return $domain; 650f4476bd9SJan Schumann } 651f4476bd9SJan Schumann 65293a7873eSAndreas Gohr /** 65393a7873eSAndreas Gohr * Get the user part from a user 65493a7873eSAndreas Gohr * 655916ef7cfSAndreas Gohr * When an account suffix is set, we strip the domain part from the user 656916ef7cfSAndreas Gohr * 657253d4b48SGerrit Uitslag * @param string $user 65893a7873eSAndreas Gohr * @return string 65993a7873eSAndreas Gohr */ 660a4337320SAndreas Gohr public function getUserName($user) 661a4337320SAndreas Gohr { 662916ef7cfSAndreas Gohr if ($this->conf['account_suffix'] !== '') { 663916ef7cfSAndreas Gohr list($user) = explode('@', $user, 2); 664916ef7cfSAndreas Gohr } 665916ef7cfSAndreas Gohr return $user; 66693a7873eSAndreas Gohr } 66793a7873eSAndreas Gohr 66893a7873eSAndreas Gohr /** 66993a7873eSAndreas Gohr * Fetch the configuration for the given AD domain 67093a7873eSAndreas Gohr * 67193a7873eSAndreas Gohr * @param string $domain current AD domain 67293a7873eSAndreas Gohr * @return array 67393a7873eSAndreas Gohr */ 674a4337320SAndreas Gohr protected function loadServerConfig($domain) 675a4337320SAndreas Gohr { 67693a7873eSAndreas Gohr // prepare adLDAP standard configuration 67732fd494aSAndreas Gohr $opts = $this->conf; 67893a7873eSAndreas Gohr 67993a7873eSAndreas Gohr $opts['domain'] = $domain; 68093a7873eSAndreas Gohr 68193a7873eSAndreas Gohr // add possible domain specific configuration 68232fd494aSAndreas Gohr if ($domain && is_array($this->conf[$domain])) foreach ($this->conf[$domain] as $key => $val) { 68393a7873eSAndreas Gohr $opts[$key] = $val; 68493a7873eSAndreas Gohr } 68593a7873eSAndreas Gohr 68693a7873eSAndreas Gohr // handle multiple AD servers 68793a7873eSAndreas Gohr $opts['domain_controllers'] = explode(',', $opts['domain_controllers']); 68893a7873eSAndreas Gohr $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']); 68993a7873eSAndreas Gohr $opts['domain_controllers'] = array_filter($opts['domain_controllers']); 69093a7873eSAndreas Gohr 6918257d713SAndreas Gohr // compatibility with old option name 69264159a61SAndreas Gohr if (empty($opts['admin_username']) && !empty($opts['ad_username'])) { 69364159a61SAndreas Gohr $opts['admin_username'] = $opts['ad_username']; 69464159a61SAndreas Gohr } 69564159a61SAndreas Gohr if (empty($opts['admin_password']) && !empty($opts['ad_password'])) { 69664159a61SAndreas Gohr $opts['admin_password'] = $opts['ad_password']; 69764159a61SAndreas Gohr } 698342753d2SAndreas Gohr $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate 6998257d713SAndreas Gohr 70093a7873eSAndreas Gohr // we can change the password if SSL is set 70193a7873eSAndreas Gohr if ($opts['use_ssl'] || $opts['use_tls']) { 70293a7873eSAndreas Gohr $this->cando['modPass'] = true; 70393a7873eSAndreas Gohr } else { 70493a7873eSAndreas Gohr $this->cando['modPass'] = false; 70593a7873eSAndreas Gohr } 70693a7873eSAndreas Gohr 70712d195abSAndreas Gohr // adLDAP expects empty user/pass as NULL, we're less strict FS#2781 70812d195abSAndreas Gohr if (empty($opts['admin_username'])) $opts['admin_username'] = null; 70912d195abSAndreas Gohr if (empty($opts['admin_password'])) $opts['admin_password'] = null; 71012d195abSAndreas Gohr 71112d195abSAndreas Gohr // user listing needs admin priviledges 7128257d713SAndreas Gohr if (!empty($opts['admin_username']) && !empty($opts['admin_password'])) { 71393a7873eSAndreas Gohr $this->cando['getUsers'] = true; 71493a7873eSAndreas Gohr } else { 7151b228d28SKlap-in $this->cando['getUsers'] = false; 71693a7873eSAndreas Gohr } 71793a7873eSAndreas Gohr 71893a7873eSAndreas Gohr return $opts; 71993a7873eSAndreas Gohr } 72093a7873eSAndreas Gohr 72193a7873eSAndreas Gohr /** 722741b8a48SAndreas Gohr * Returns a list of configured domains 723741b8a48SAndreas Gohr * 724741b8a48SAndreas Gohr * The default domain has an empty string as key 725741b8a48SAndreas Gohr * 726741b8a48SAndreas Gohr * @return array associative array(key => domain) 727741b8a48SAndreas Gohr */ 728a4337320SAndreas Gohr public function getConfiguredDomains() 729a4337320SAndreas Gohr { 730741b8a48SAndreas Gohr $domains = array(); 731741b8a48SAndreas Gohr if (empty($this->conf['account_suffix'])) return $domains; // not configured yet 732741b8a48SAndreas Gohr 733741b8a48SAndreas Gohr // add default domain, using the name from account suffix 734741b8a48SAndreas Gohr $domains[''] = ltrim($this->conf['account_suffix'], '@'); 735741b8a48SAndreas Gohr 736741b8a48SAndreas Gohr // find additional domains 737741b8a48SAndreas Gohr foreach ($this->conf as $key => $val) { 738741b8a48SAndreas Gohr if (is_array($val) && isset($val['account_suffix'])) { 739741b8a48SAndreas Gohr $domains[$key] = ltrim($val['account_suffix'], '@'); 740741b8a48SAndreas Gohr } 741741b8a48SAndreas Gohr } 742741b8a48SAndreas Gohr ksort($domains); 743741b8a48SAndreas Gohr 744741b8a48SAndreas Gohr return $domains; 745741b8a48SAndreas Gohr } 746741b8a48SAndreas Gohr 747741b8a48SAndreas Gohr /** 74893a7873eSAndreas Gohr * Check provided user and userinfo for matching patterns 74993a7873eSAndreas Gohr * 75093a7873eSAndreas Gohr * The patterns are set up with $this->_constructPattern() 75193a7873eSAndreas Gohr * 75293a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 753253d4b48SGerrit Uitslag * 75493a7873eSAndreas Gohr * @param string $user 75593a7873eSAndreas Gohr * @param array $info 75693a7873eSAndreas Gohr * @return bool 75793a7873eSAndreas Gohr */ 758a4337320SAndreas Gohr protected function filter($user, $info) 759a4337320SAndreas Gohr { 760a4337320SAndreas Gohr foreach ($this->pattern as $item => $pattern) { 76193a7873eSAndreas Gohr if ($item == 'user') { 76293a7873eSAndreas Gohr if (!preg_match($pattern, $user)) return false; 76393a7873eSAndreas Gohr } elseif ($item == 'grps') { 76493a7873eSAndreas Gohr if (!count(preg_grep($pattern, $info['grps']))) return false; 76593a7873eSAndreas Gohr } else { 76693a7873eSAndreas Gohr if (!preg_match($pattern, $info[$item])) return false; 76793a7873eSAndreas Gohr } 76893a7873eSAndreas Gohr } 76993a7873eSAndreas Gohr return true; 77093a7873eSAndreas Gohr } 77193a7873eSAndreas Gohr 77293a7873eSAndreas Gohr /** 77393a7873eSAndreas Gohr * Create a pattern for $this->_filter() 77493a7873eSAndreas Gohr * 77593a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 776253d4b48SGerrit Uitslag * 77793a7873eSAndreas Gohr * @param array $filter 77893a7873eSAndreas Gohr */ 779a4337320SAndreas Gohr protected function constructPattern($filter) 780a4337320SAndreas Gohr { 781a4337320SAndreas Gohr $this->pattern = array(); 782f4476bd9SJan Schumann foreach ($filter as $item => $pattern) { 783a4337320SAndreas Gohr $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters 784f4476bd9SJan Schumann } 785f4476bd9SJan Schumann } 786f4476bd9SJan Schumann} 787