1f4476bd9SJan Schumann<?php 2f4476bd9SJan Schumann 332fd494aSAndreas Gohrrequire_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php'); 407aec029SMichael Großerequire_once(DOKU_PLUGIN.'authad/adLDAP/classes/adLDAPUtils.php'); 532fd494aSAndreas Gohr 6f4476bd9SJan Schumann/** 7f4476bd9SJan Schumann * Active Directory authentication backend for DokuWiki 8f4476bd9SJan Schumann * 9f4476bd9SJan Schumann * This makes authentication with a Active Directory server much easier 10f4476bd9SJan Schumann * than when using the normal LDAP backend by utilizing the adLDAP library 11f4476bd9SJan Schumann * 12f4476bd9SJan Schumann * Usage: 13f4476bd9SJan Schumann * Set DokuWiki's local.protected.php auth setting to read 14f4476bd9SJan Schumann * 15f4476bd9SJan Schumann * $conf['authtype'] = 'authad'; 16f4476bd9SJan Schumann * 1732fd494aSAndreas Gohr * $conf['plugin']['authad']['account_suffix'] = '@my.domain.org'; 1832fd494aSAndreas Gohr * $conf['plugin']['authad']['base_dn'] = 'DC=my,DC=domain,DC=org'; 1932fd494aSAndreas Gohr * $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org'; 20f4476bd9SJan Schumann * 21f4476bd9SJan Schumann * //optional: 2232fd494aSAndreas Gohr * $conf['plugin']['authad']['sso'] = 1; 233002d731SAndreas Gohr * $conf['plugin']['authad']['admin_username'] = 'root'; 243002d731SAndreas Gohr * $conf['plugin']['authad']['admin_password'] = 'pass'; 2532fd494aSAndreas Gohr * $conf['plugin']['authad']['real_primarygroup'] = 1; 2632fd494aSAndreas Gohr * $conf['plugin']['authad']['use_ssl'] = 1; 2732fd494aSAndreas Gohr * $conf['plugin']['authad']['use_tls'] = 1; 2832fd494aSAndreas Gohr * $conf['plugin']['authad']['debug'] = 1; 2993a7873eSAndreas Gohr * // warn user about expiring password this many days in advance: 3032fd494aSAndreas Gohr * $conf['plugin']['authad']['expirywarn'] = 5; 31f4476bd9SJan Schumann * 32f4476bd9SJan Schumann * // get additional information to the userinfo array 33f4476bd9SJan Schumann * // add a list of comma separated ldap contact fields. 34f4476bd9SJan Schumann * $conf['plugin']['authad']['additional'] = 'field1,field2'; 35f4476bd9SJan Schumann * 36f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 37f4476bd9SJan Schumann * @author James Van Lommel <jamesvl@gmail.com> 38f4476bd9SJan Schumann * @link http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/ 39f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 40f4476bd9SJan Schumann * @author Jan Schumann <js@schumann-it.com> 41f4476bd9SJan Schumann */ 4293a7873eSAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin { 4332fd494aSAndreas Gohr 4493a7873eSAndreas Gohr /** 4593a7873eSAndreas Gohr * @var array hold connection data for a specific AD domain 4693a7873eSAndreas Gohr */ 4793a7873eSAndreas Gohr protected $opts = array(); 4832fd494aSAndreas Gohr 4993a7873eSAndreas Gohr /** 5093a7873eSAndreas Gohr * @var array open connections for each AD domain, as adLDAP objects 5193a7873eSAndreas Gohr */ 5293a7873eSAndreas Gohr protected $adldap = array(); 5393a7873eSAndreas Gohr 5493a7873eSAndreas Gohr /** 5593a7873eSAndreas Gohr * @var bool message state 5693a7873eSAndreas Gohr */ 5793a7873eSAndreas Gohr protected $msgshown = false; 5893a7873eSAndreas Gohr 5993a7873eSAndreas Gohr /** 6093a7873eSAndreas Gohr * @var array user listing cache 6193a7873eSAndreas Gohr */ 6293a7873eSAndreas Gohr protected $users = array(); 6393a7873eSAndreas Gohr 6493a7873eSAndreas Gohr /** 6593a7873eSAndreas Gohr * @var array filter patterns for listing users 6693a7873eSAndreas Gohr */ 6793a7873eSAndreas Gohr protected $_pattern = array(); 68f4476bd9SJan Schumann 69c52f6cd2SMichael Große protected $_actualstart = 0; 70c52f6cd2SMichael Große 71c52f6cd2SMichael Große protected $_grpsusers = array(); 72c52f6cd2SMichael Große 73f4476bd9SJan Schumann /** 74f4476bd9SJan Schumann * Constructor 75f4476bd9SJan Schumann */ 7693a7873eSAndreas Gohr public function __construct() { 7700d58927SMichael Hamann global $INPUT; 78454d868bSAndreas Gohr parent::__construct(); 79454d868bSAndreas 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 100d34a2a38SAndreas Gohr // make sure the right encoding is used 101d34a2a38SAndreas Gohr if($this->getConf('sso_charset')) { 102d34a2a38SAndreas Gohr $_SERVER['REMOTE_USER'] = iconv($this->getConf('sso_charset'), 'UTF-8', $_SERVER['REMOTE_USER']); 103d34a2a38SAndreas Gohr } elseif(!utf8_check($_SERVER['REMOTE_USER'])) { 10493a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']); 10593a7873eSAndreas Gohr } 106d34a2a38SAndreas Gohr 107d34a2a38SAndreas Gohr // trust the incoming user 108d34a2a38SAndreas Gohr if($this->conf['sso']) { 10993a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']); 110f4476bd9SJan Schumann 111f4476bd9SJan Schumann // we need to simulate a login 112f4476bd9SJan Schumann if(empty($_COOKIE[DOKU_COOKIE])) { 11300d58927SMichael Hamann $INPUT->set('u', $_SERVER['REMOTE_USER']); 11400d58927SMichael Hamann $INPUT->set('p', 'sso_only'); 115f4476bd9SJan Schumann } 116f4476bd9SJan Schumann } 117d34a2a38SAndreas Gohr } 118f4476bd9SJan Schumann 11993a7873eSAndreas Gohr // other can do's are changed in $this->_loadServerConfig() base on domain setup 120bb30445dSMichael Wilmes $this->cando['modName'] = (bool)$this->conf['update_name']; 121bb30445dSMichael Wilmes $this->cando['modMail'] = (bool)$this->conf['update_mail']; 12225f80763SMichael Große $this->cando['getUserCount'] = true; 123f4476bd9SJan Schumann } 124f4476bd9SJan Schumann 125f4476bd9SJan Schumann /** 126a154806fSAndreas Gohr * Load domain config on capability check 127a154806fSAndreas Gohr * 128a154806fSAndreas Gohr * @param string $cap 129a154806fSAndreas Gohr * @return bool 130a154806fSAndreas Gohr */ 131a154806fSAndreas Gohr public function canDo($cap) { 132a154806fSAndreas Gohr //capabilities depend on config, which may change depending on domain 133a154806fSAndreas Gohr $domain = $this->_userDomain($_SERVER['REMOTE_USER']); 134a154806fSAndreas 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 */ 15093a7873eSAndreas Gohr public function checkPass($user, $pass) { 151f4476bd9SJan Schumann if($_SERVER['REMOTE_USER'] && 152f4476bd9SJan Schumann $_SERVER['REMOTE_USER'] == $user && 15332fd494aSAndreas Gohr $this->conf['sso'] 15493a7873eSAndreas Gohr ) return true; 155f4476bd9SJan Schumann 15693a7873eSAndreas Gohr $adldap = $this->_adldap($this->_userDomain($user)); 15793a7873eSAndreas Gohr if(!$adldap) return false; 15893a7873eSAndreas Gohr 15993a7873eSAndreas Gohr return $adldap->authenticate($this->_userName($user), $pass); 160f4476bd9SJan Schumann } 161f4476bd9SJan Schumann 162f4476bd9SJan Schumann /** 163f4476bd9SJan Schumann * Return user info [required auth function] 164f4476bd9SJan Schumann * 165f4476bd9SJan Schumann * Returns info about the given user needs to contain 166f4476bd9SJan Schumann * at least these fields: 167f4476bd9SJan Schumann * 168f4476bd9SJan Schumann * name string full name of the user 169f4476bd9SJan Schumann * mail string email address of the user 170f4476bd9SJan Schumann * grps array list of groups the user is in 171f4476bd9SJan Schumann * 17293a7873eSAndreas Gohr * This AD specific function returns the following 173f4476bd9SJan Schumann * addional fields: 174f4476bd9SJan Schumann * 175f4476bd9SJan Schumann * dn string distinguished name (DN) 17693a7873eSAndreas Gohr * uid string samaccountname 17793a7873eSAndreas Gohr * lastpwd int timestamp of the date when the password was set 17893a7873eSAndreas Gohr * expires true if the password expires 17993a7873eSAndreas Gohr * expiresin int seconds until the password expires 18093a7873eSAndreas Gohr * any fields specified in the 'additional' config option 181f4476bd9SJan Schumann * 182f4476bd9SJan Schumann * @author James Van Lommel <james@nosq.com> 18393a7873eSAndreas Gohr * @param string $user 1842046a654SChristopher Smith * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin 18593a7873eSAndreas Gohr * @return array 186f4476bd9SJan Schumann */ 1872046a654SChristopher Smith public function getUserData($user, $requireGroups=true) { 188f4476bd9SJan Schumann global $conf; 18993a7873eSAndreas Gohr global $lang; 19093a7873eSAndreas Gohr global $ID; 19193a7873eSAndreas Gohr $adldap = $this->_adldap($this->_userDomain($user)); 19293a7873eSAndreas Gohr if(!$adldap) return false; 193f4476bd9SJan Schumann 19493a7873eSAndreas Gohr if($user == '') return array(); 19593a7873eSAndreas Gohr 19693a7873eSAndreas Gohr $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol'); 197f4476bd9SJan Schumann 198f4476bd9SJan Schumann // add additional fields to read 19932fd494aSAndreas Gohr $fields = array_merge($fields, $this->conf['additional']); 200f4476bd9SJan Schumann $fields = array_unique($fields); 20114642325SAndreas Gohr $fields = array_filter($fields); 202f4476bd9SJan Schumann 203f4476bd9SJan Schumann //get info for given user 20432fd494aSAndreas Gohr $result = $adldap->user()->info($this->_userName($user), $fields); 20593a7873eSAndreas Gohr if($result == false){ 20693a7873eSAndreas Gohr return array(); 20793a7873eSAndreas Gohr } 20893a7873eSAndreas Gohr 209f4476bd9SJan Schumann //general user info 21059bc3b48SGerrit Uitslag $info = array(); 211f4476bd9SJan Schumann $info['name'] = $result[0]['displayname'][0]; 212f4476bd9SJan Schumann $info['mail'] = $result[0]['mail'][0]; 213f4476bd9SJan Schumann $info['uid'] = $result[0]['samaccountname'][0]; 214f4476bd9SJan Schumann $info['dn'] = $result[0]['dn']; 21593a7873eSAndreas Gohr //last password set (Windows counts from January 1st 1601) 21693a7873eSAndreas Gohr $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600; 21793a7873eSAndreas Gohr //will it expire? 21893a7873eSAndreas Gohr $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD 219f4476bd9SJan Schumann 220f4476bd9SJan Schumann // additional information 22132fd494aSAndreas Gohr foreach($this->conf['additional'] as $field) { 222f4476bd9SJan Schumann if(isset($result[0][strtolower($field)])) { 223f4476bd9SJan Schumann $info[$field] = $result[0][strtolower($field)][0]; 224f4476bd9SJan Schumann } 225f4476bd9SJan Schumann } 226f4476bd9SJan Schumann 227f4476bd9SJan Schumann // handle ActiveDirectory memberOf 22832fd494aSAndreas Gohr $info['grps'] = $adldap->user()->groups($this->_userName($user),(bool) $this->opts['recursive_groups']); 229f4476bd9SJan Schumann 230f4476bd9SJan Schumann if(is_array($info['grps'])) { 231f4476bd9SJan Schumann foreach($info['grps'] as $ndx => $group) { 232f4476bd9SJan Schumann $info['grps'][$ndx] = $this->cleanGroup($group); 233f4476bd9SJan Schumann } 234f4476bd9SJan Schumann } 235f4476bd9SJan Schumann 236f4476bd9SJan Schumann // always add the default group to the list of groups 237f4476bd9SJan Schumann if(!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) { 238f4476bd9SJan Schumann $info['grps'][] = $conf['defaultgroup']; 239f4476bd9SJan Schumann } 240f4476bd9SJan Schumann 24193a7873eSAndreas Gohr // add the user's domain to the groups 24293a7873eSAndreas Gohr $domain = $this->_userDomain($user); 24393a7873eSAndreas Gohr if($domain && !in_array("domain-$domain", (array) $info['grps'])) { 24493a7873eSAndreas Gohr $info['grps'][] = $this->cleanGroup("domain-$domain"); 24593a7873eSAndreas Gohr } 24693a7873eSAndreas Gohr 24793a7873eSAndreas Gohr // check expiry time 24832fd494aSAndreas Gohr if($info['expires'] && $this->conf['expirywarn']){ 2491e52e72aSAndreas Gohr $expiry = $adldap->user()->passwordExpiry($user); 2501e52e72aSAndreas Gohr if(is_array($expiry)){ 2511e52e72aSAndreas Gohr $info['expiresat'] = $expiry['expiryts']; 2521e52e72aSAndreas Gohr $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60)); 25393a7873eSAndreas Gohr 25493a7873eSAndreas Gohr // if this is the current user, warn him (once per request only) 25593a7873eSAndreas Gohr if(($_SERVER['REMOTE_USER'] == $user) && 2561e52e72aSAndreas Gohr ($info['expiresin'] <= $this->conf['expirywarn']) && 25793a7873eSAndreas Gohr !$this->msgshown 25893a7873eSAndreas Gohr ) { 2595b795a65SPatrick Brown $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']); 26093a7873eSAndreas Gohr if($this->canDo('modPass')) { 26193a7873eSAndreas Gohr $url = wl($ID, array('do'=> 'profile')); 26293a7873eSAndreas Gohr $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>'; 26393a7873eSAndreas Gohr } 26493a7873eSAndreas Gohr msg($msg); 26593a7873eSAndreas Gohr $this->msgshown = true; 26693a7873eSAndreas Gohr } 26793a7873eSAndreas Gohr } 2681e52e72aSAndreas Gohr } 26993a7873eSAndreas Gohr 270f4476bd9SJan Schumann return $info; 271f4476bd9SJan Schumann } 272f4476bd9SJan Schumann 273f4476bd9SJan Schumann /** 274f4476bd9SJan Schumann * Make AD group names usable by DokuWiki. 275f4476bd9SJan Schumann * 276f4476bd9SJan Schumann * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores. 277f4476bd9SJan Schumann * 278f4476bd9SJan Schumann * @author James Van Lommel (jamesvl@gmail.com) 27993a7873eSAndreas Gohr * @param string $group 28093a7873eSAndreas Gohr * @return string 281f4476bd9SJan Schumann */ 28293a7873eSAndreas Gohr public function cleanGroup($group) { 28393a7873eSAndreas Gohr $group = str_replace('\\', '', $group); 28493a7873eSAndreas Gohr $group = str_replace('#', '', $group); 28593a7873eSAndreas Gohr $group = preg_replace('[\s]', '_', $group); 28693a7873eSAndreas Gohr $group = utf8_strtolower(trim($group)); 28793a7873eSAndreas Gohr return $group; 288f4476bd9SJan Schumann } 289f4476bd9SJan Schumann 290f4476bd9SJan Schumann /** 291f4476bd9SJan Schumann * Sanitize user names 29293a7873eSAndreas Gohr * 29393a7873eSAndreas Gohr * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup) 29493a7873eSAndreas Gohr * 29593a7873eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 29693a7873eSAndreas Gohr * @param string $user 29793a7873eSAndreas Gohr * @return string 298f4476bd9SJan Schumann */ 29993a7873eSAndreas Gohr public function cleanUser($user) { 30093a7873eSAndreas Gohr $domain = ''; 30193a7873eSAndreas Gohr 30293a7873eSAndreas Gohr // get NTLM or Kerberos domain part 30393a7873eSAndreas Gohr list($dom, $user) = explode('\\', $user, 2); 30493a7873eSAndreas Gohr if(!$user) $user = $dom; 30593a7873eSAndreas Gohr if($dom) $domain = $dom; 30693a7873eSAndreas Gohr list($user, $dom) = explode('@', $user, 2); 30793a7873eSAndreas Gohr if($dom) $domain = $dom; 30893a7873eSAndreas Gohr 30993a7873eSAndreas Gohr // clean up both 31093a7873eSAndreas Gohr $domain = utf8_strtolower(trim($domain)); 31193a7873eSAndreas Gohr $user = utf8_strtolower(trim($user)); 31293a7873eSAndreas Gohr 31393a7873eSAndreas Gohr // is this a known, valid domain? if not discard 31432fd494aSAndreas Gohr if(!is_array($this->conf[$domain])) { 31593a7873eSAndreas Gohr $domain = ''; 31693a7873eSAndreas Gohr } 31793a7873eSAndreas Gohr 31893a7873eSAndreas Gohr // reattach domain 31993a7873eSAndreas Gohr if($domain) $user = "$user@$domain"; 32093a7873eSAndreas Gohr return $user; 321f4476bd9SJan Schumann } 322f4476bd9SJan Schumann 323f4476bd9SJan Schumann /** 324f4476bd9SJan Schumann * Most values in LDAP are case-insensitive 32593a7873eSAndreas Gohr * 32693a7873eSAndreas Gohr * @return bool 327f4476bd9SJan Schumann */ 32893a7873eSAndreas Gohr public function isCaseSensitive() { 329f4476bd9SJan Schumann return false; 330f4476bd9SJan Schumann } 331f4476bd9SJan Schumann 3326fcf992cSMichael Große /** 3337910cbbbSMichael Große * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true) 3347910cbbbSMichael Große * 3356fcf992cSMichael Große * @param array $filter 3366fcf992cSMichael Große * @return string 3376fcf992cSMichael Große */ 33867a31a83SMichael Große protected function _constructSearchString($filter){ 33967a31a83SMichael Große if (!$filter){ 34067a31a83SMichael Große return '*'; 34167a31a83SMichael Große } 34207aec029SMichael Große $adldapUtils = new adLDAPUtils($this->_adldap(null)); 34367a31a83SMichael Große $result = '*'; 34467a31a83SMichael Große if (isset($filter['name'])) { 34507aec029SMichael Große $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*'; 34667a31a83SMichael Große unset($filter['name']); 34767a31a83SMichael Große } 3487910cbbbSMichael Große 34967a31a83SMichael Große if (isset($filter['user'])) { 35007aec029SMichael Große $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*'; 35167a31a83SMichael Große unset($filter['user']); 35267a31a83SMichael Große } 35367a31a83SMichael Große 35467a31a83SMichael Große if (isset($filter['mail'])) { 35507aec029SMichael Große $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*'; 35667a31a83SMichael Große unset($filter['mail']); 35767a31a83SMichael Große } 35867a31a83SMichael Große return $result; 35967a31a83SMichael Große } 36067a31a83SMichael Große 361f4476bd9SJan Schumann /** 3627910cbbbSMichael Große * Return a count of the number of user which meet $filter criteria 3637910cbbbSMichael Große * 3647910cbbbSMichael Große * @param array $filter $filter array of field/pattern pairs, empty array for no filter 3657910cbbbSMichael Große * @return int number of users 36625f80763SMichael Große */ 36725f80763SMichael Große public function getUserCount($filter = array()) { 3686fcf992cSMichael Große $adldap = $this->_adldap(null); 3696fcf992cSMichael Große if(!$adldap) { 3706fcf992cSMichael Große dbglog("authad/auth.php getUserCount(): _adldap not set."); 3716fcf992cSMichael Große return -1; 3726fcf992cSMichael Große } 37367a31a83SMichael Große if ($filter == array()) { 37425f80763SMichael Große $result = $adldap->user()->all(); 37567a31a83SMichael Große } else { 37667a31a83SMichael Große $searchString = $this->_constructSearchString($filter); 37767a31a83SMichael Große $result = $adldap->user()->all(false, $searchString); 378c52f6cd2SMichael Große if (isset($filter['grps'])) { 379c52f6cd2SMichael Große $this->users = array_fill_keys($result, false); 380c52f6cd2SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 381462e9e37SMichael Große $usermanager->setLastdisabled(true); 382c52f6cd2SMichael Große if (!isset($this->_grpsusers[$this->_filterToString($filter)])){ 383c52f6cd2SMichael Große $this->_fillGroupUserArray($filter,$usermanager->getStart() + 3*$usermanager->getPagesize()); 384*64159a61SAndreas Gohr } elseif ( 385*64159a61SAndreas Gohr count($this->_grpsusers[$this->_filterToString($filter)]) < 386*64159a61SAndreas Gohr $usermanager->getStart() + 3*$usermanager->getPagesize() 387*64159a61SAndreas Gohr ) { 388*64159a61SAndreas Gohr $this->_fillGroupUserArray( 389*64159a61SAndreas Gohr $filter, 390*64159a61SAndreas Gohr $usermanager->getStart() + 391*64159a61SAndreas Gohr 3*$usermanager->getPagesize() - 392*64159a61SAndreas Gohr count($this->_grpsusers[$this->_filterToString($filter)]) 393*64159a61SAndreas Gohr ); 394c52f6cd2SMichael Große } 395c52f6cd2SMichael Große $result = $this->_grpsusers[$this->_filterToString($filter)]; 396462e9e37SMichael Große } else { 397462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 398462e9e37SMichael Große $usermanager->setLastdisabled(false); 399c52f6cd2SMichael Große } 40067a31a83SMichael Große 40167a31a83SMichael Große } 40267a31a83SMichael Große 40325f80763SMichael Große if (!$result) { 4046fcf992cSMichael Große return 0; 40525f80763SMichael Große } 40625f80763SMichael Große return count($result); 40725f80763SMichael Große } 40825f80763SMichael Große 4096fcf992cSMichael Große /** 4106fcf992cSMichael Große * 4116fcf992cSMichael Große * create a unique string for each filter used with a group 4126fcf992cSMichael Große * 4136fcf992cSMichael Große * @param array $filter 4146fcf992cSMichael Große * @return string 4156fcf992cSMichael Große */ 416c52f6cd2SMichael Große protected function _filterToString ($filter) { 417c52f6cd2SMichael Große $result = ''; 418c52f6cd2SMichael Große if (isset($filter['user'])) { 419c52f6cd2SMichael Große $result .= 'user-' . $filter['user']; 420c52f6cd2SMichael Große } 421c52f6cd2SMichael Große if (isset($filter['name'])) { 422c52f6cd2SMichael Große $result .= 'name-' . $filter['name']; 423c52f6cd2SMichael Große } 424c52f6cd2SMichael Große if (isset($filter['mail'])) { 425c52f6cd2SMichael Große $result .= 'mail-' . $filter['mail']; 426c52f6cd2SMichael Große } 427c52f6cd2SMichael Große if (isset($filter['grps'])) { 428c52f6cd2SMichael Große $result .= 'grps-' . $filter['grps']; 429c52f6cd2SMichael Große } 430c52f6cd2SMichael Große return $result; 431c52f6cd2SMichael Große } 432c52f6cd2SMichael Große 4336fcf992cSMichael Große /** 4347910cbbbSMichael Große * Create an array of $numberOfAdds users passing a certain $filter, including belonging 4357910cbbbSMichael Große * to a certain group and save them to a object-wide array. If the array 4367910cbbbSMichael Große * already exists try to add $numberOfAdds further users to it. 4377910cbbbSMichael Große * 4386fcf992cSMichael Große * @param array $filter 4396fcf992cSMichael Große * @param int $numberOfAdds additional number of users requested 4406fcf992cSMichael Große * @return int number of Users actually add to Array 4416fcf992cSMichael Große */ 442c52f6cd2SMichael Große protected function _fillGroupUserArray($filter, $numberOfAdds){ 443c52f6cd2SMichael Große $this->_grpsusers[$this->_filterToString($filter)]; 444c52f6cd2SMichael Große $i = 0; 445c52f6cd2SMichael Große $count = 0; 446c52f6cd2SMichael Große $this->_constructPattern($filter); 447c52f6cd2SMichael Große foreach ($this->users as $user => &$info) { 448c52f6cd2SMichael Große if($i++ < $this->_actualstart) { 449c52f6cd2SMichael Große continue; 450c52f6cd2SMichael Große } 451c52f6cd2SMichael Große if($info === false) { 452c52f6cd2SMichael Große $info = $this->getUserData($user); 453c52f6cd2SMichael Große } 454c52f6cd2SMichael Große if($this->_filter($user, $info)) { 455c52f6cd2SMichael Große $this->_grpsusers[$this->_filterToString($filter)][$user] = $info; 456c52f6cd2SMichael Große if(($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break; 457c52f6cd2SMichael Große } 458c52f6cd2SMichael Große } 459c52f6cd2SMichael Große $this->_actualstart = $i; 460c52f6cd2SMichael Große return $count; 461c52f6cd2SMichael Große } 462c52f6cd2SMichael Große 46325f80763SMichael Große /** 464f4476bd9SJan Schumann * Bulk retrieval of user data 465f4476bd9SJan Schumann * 466f4476bd9SJan Schumann * @author Dominik Eckelmann <dokuwiki@cosmocode.de> 467253d4b48SGerrit Uitslag * 46893a7873eSAndreas Gohr * @param int $start index of first user to be returned 46993a7873eSAndreas Gohr * @param int $limit max number of users to be returned 47093a7873eSAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 47193a7873eSAndreas Gohr * @return array userinfo (refer getUserData for internal userinfo details) 472f4476bd9SJan Schumann */ 473c52f6cd2SMichael Große public function retrieveUsers($start = 0, $limit = 0, $filter = array()) { 47493a7873eSAndreas Gohr $adldap = $this->_adldap(null); 47593a7873eSAndreas Gohr if(!$adldap) return false; 476f4476bd9SJan Schumann 4770ba750c0SAndreas Gohr if(!$this->users) { 478f4476bd9SJan Schumann //get info for given user 47967a31a83SMichael Große $result = $adldap->user()->all(false, $this->_constructSearchString($filter)); 480f4476bd9SJan Schumann if (!$result) return array(); 481f4476bd9SJan Schumann $this->users = array_fill_keys($result, false); 482f4476bd9SJan Schumann } 483f4476bd9SJan Schumann 484f4476bd9SJan Schumann $i = 0; 485f4476bd9SJan Schumann $count = 0; 486f4476bd9SJan Schumann $result = array(); 487f4476bd9SJan Schumann 488c52f6cd2SMichael Große if (!isset($filter['grps'])) { 489462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 490462e9e37SMichael Große $usermanager->setLastdisabled(false); 4916fcf992cSMichael Große $this->_constructPattern($filter); 492f4476bd9SJan Schumann foreach($this->users as $user => &$info) { 493f4476bd9SJan Schumann if($i++ < $start) { 494f4476bd9SJan Schumann continue; 495f4476bd9SJan Schumann } 496f4476bd9SJan Schumann if($info === false) { 497f4476bd9SJan Schumann $info = $this->getUserData($user); 498f4476bd9SJan Schumann } 499f4476bd9SJan Schumann $result[$user] = $info; 5009a2c73e8SAndreas Gohr if(($limit > 0) && (++$count >= $limit)) break; 501f4476bd9SJan Schumann } 502c52f6cd2SMichael Große } else { 503462e9e37SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 504462e9e37SMichael Große $usermanager->setLastdisabled(true); 505*64159a61SAndreas Gohr if ( 506*64159a61SAndreas Gohr !isset($this->_grpsusers[$this->_filterToString($filter)]) || 507*64159a61SAndreas Gohr count($this->_grpsusers[$this->_filterToString($filter)]) < ($start+$limit) 508*64159a61SAndreas Gohr ) { 509*64159a61SAndreas Gohr $this->_fillGroupUserArray( 510*64159a61SAndreas Gohr $filter, 511*64159a61SAndreas Gohr $start+$limit - count($this->_grpsusers[$this->_filterToString($filter)]) +1 512*64159a61SAndreas Gohr ); 513c52f6cd2SMichael Große } 5146fcf992cSMichael Große if (!$this->_grpsusers[$this->_filterToString($filter)]) return false; 515c52f6cd2SMichael Große foreach($this->_grpsusers[$this->_filterToString($filter)] as $user => &$info) { 516c52f6cd2SMichael Große if($i++ < $start) { 517c52f6cd2SMichael Große continue; 518c52f6cd2SMichael Große } 519c52f6cd2SMichael Große $result[$user] = $info; 520c52f6cd2SMichael Große if(($limit > 0) && (++$count >= $limit)) break; 521c52f6cd2SMichael Große } 522c52f6cd2SMichael Große 523c52f6cd2SMichael Große } 524f4476bd9SJan Schumann return $result; 525f4476bd9SJan Schumann } 526f4476bd9SJan Schumann 527f4476bd9SJan Schumann /** 528f4476bd9SJan Schumann * Modify user data 529f4476bd9SJan Schumann * 53093a7873eSAndreas Gohr * @param string $user nick of the user to be changed 53193a7873eSAndreas Gohr * @param array $changes array of field/value pairs to be changed 532f4476bd9SJan Schumann * @return bool 533f4476bd9SJan Schumann */ 53493a7873eSAndreas Gohr public function modifyUser($user, $changes) { 535f4476bd9SJan Schumann $return = true; 53693a7873eSAndreas Gohr $adldap = $this->_adldap($this->_userDomain($user)); 5378f03c311SPatrick Brown if(!$adldap) { 5388f03c311SPatrick Brown msg($this->getLang('connectfail'), -1); 5398f03c311SPatrick Brown return false; 5408f03c311SPatrick Brown } 541f4476bd9SJan Schumann 542f4476bd9SJan Schumann // password changing 543f4476bd9SJan Schumann if(isset($changes['pass'])) { 544f4476bd9SJan Schumann try { 54532fd494aSAndreas Gohr $return = $adldap->user()->password($this->_userName($user),$changes['pass']); 546f4476bd9SJan Schumann } catch (adLDAPException $e) { 54732fd494aSAndreas Gohr if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); 548f4476bd9SJan Schumann $return = false; 549f4476bd9SJan Schumann } 5508f03c311SPatrick Brown if(!$return) msg($this->getLang('passchangefail'), -1); 551f4476bd9SJan Schumann } 552f4476bd9SJan Schumann 553f4476bd9SJan Schumann // changing user data 554f4476bd9SJan Schumann $adchanges = array(); 555f4476bd9SJan Schumann if(isset($changes['name'])) { 556f4476bd9SJan Schumann // get first and last name 557f4476bd9SJan Schumann $parts = explode(' ', $changes['name']); 558f4476bd9SJan Schumann $adchanges['surname'] = array_pop($parts); 559f4476bd9SJan Schumann $adchanges['firstname'] = join(' ', $parts); 560f4476bd9SJan Schumann $adchanges['display_name'] = $changes['name']; 561f4476bd9SJan Schumann } 562f4476bd9SJan Schumann if(isset($changes['mail'])) { 563f4476bd9SJan Schumann $adchanges['email'] = $changes['mail']; 564f4476bd9SJan Schumann } 565f4476bd9SJan Schumann if(count($adchanges)) { 566f4476bd9SJan Schumann try { 56732fd494aSAndreas Gohr $return = $return & $adldap->user()->modify($this->_userName($user),$adchanges); 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('userchangefail'), -1); 573f4476bd9SJan Schumann } 574f4476bd9SJan Schumann 575f4476bd9SJan Schumann return $return; 576f4476bd9SJan Schumann } 577f4476bd9SJan Schumann 578f4476bd9SJan Schumann /** 579f4476bd9SJan Schumann * Initialize the AdLDAP library and connect to the server 58093a7873eSAndreas Gohr * 58193a7873eSAndreas Gohr * When you pass null as domain, it will reuse any existing domain. 58293a7873eSAndreas Gohr * Eg. the one of the logged in user. It falls back to the default 58393a7873eSAndreas Gohr * domain if no current one is available. 58493a7873eSAndreas Gohr * 58593a7873eSAndreas Gohr * @param string|null $domain The AD domain to use 58693a7873eSAndreas Gohr * @return adLDAP|bool true if a connection was established 587f4476bd9SJan Schumann */ 58893a7873eSAndreas Gohr protected function _adldap($domain) { 58993a7873eSAndreas Gohr if(is_null($domain) && is_array($this->opts)) { 59093a7873eSAndreas Gohr $domain = $this->opts['domain']; 59193a7873eSAndreas Gohr } 59293a7873eSAndreas Gohr 59393a7873eSAndreas Gohr $this->opts = $this->_loadServerConfig((string) $domain); 59493a7873eSAndreas Gohr if(isset($this->adldap[$domain])) return $this->adldap[$domain]; 595f4476bd9SJan Schumann 596f4476bd9SJan Schumann // connect 597f4476bd9SJan Schumann try { 59893a7873eSAndreas Gohr $this->adldap[$domain] = new adLDAP($this->opts); 59993a7873eSAndreas Gohr return $this->adldap[$domain]; 600f4476bd9SJan Schumann } catch(adLDAPException $e) { 60132fd494aSAndreas Gohr if($this->conf['debug']) { 602f4476bd9SJan Schumann msg('AD Auth: '.$e->getMessage(), -1); 603f4476bd9SJan Schumann } 604f4476bd9SJan Schumann $this->success = false; 60593a7873eSAndreas Gohr $this->adldap[$domain] = null; 606f4476bd9SJan Schumann } 607f4476bd9SJan Schumann return false; 608f4476bd9SJan Schumann } 609f4476bd9SJan Schumann 610f4476bd9SJan Schumann /** 61193a7873eSAndreas Gohr * Get the domain part from a user 612f4476bd9SJan Schumann * 613253d4b48SGerrit Uitslag * @param string $user 61493a7873eSAndreas Gohr * @return string 615f4476bd9SJan Schumann */ 61693a7873eSAndreas Gohr public function _userDomain($user) { 61793a7873eSAndreas Gohr list(, $domain) = explode('@', $user, 2); 61893a7873eSAndreas Gohr return $domain; 619f4476bd9SJan Schumann } 620f4476bd9SJan Schumann 62193a7873eSAndreas Gohr /** 62293a7873eSAndreas Gohr * Get the user part from a user 62393a7873eSAndreas Gohr * 624253d4b48SGerrit Uitslag * @param string $user 62593a7873eSAndreas Gohr * @return string 62693a7873eSAndreas Gohr */ 62793a7873eSAndreas Gohr public function _userName($user) { 62893a7873eSAndreas Gohr list($name) = explode('@', $user, 2); 62993a7873eSAndreas Gohr return $name; 63093a7873eSAndreas Gohr } 63193a7873eSAndreas Gohr 63293a7873eSAndreas Gohr /** 63393a7873eSAndreas Gohr * Fetch the configuration for the given AD domain 63493a7873eSAndreas Gohr * 63593a7873eSAndreas Gohr * @param string $domain current AD domain 63693a7873eSAndreas Gohr * @return array 63793a7873eSAndreas Gohr */ 63893a7873eSAndreas Gohr protected function _loadServerConfig($domain) { 63993a7873eSAndreas Gohr // prepare adLDAP standard configuration 64032fd494aSAndreas Gohr $opts = $this->conf; 64193a7873eSAndreas Gohr 64293a7873eSAndreas Gohr $opts['domain'] = $domain; 64393a7873eSAndreas Gohr 64493a7873eSAndreas Gohr // add possible domain specific configuration 64532fd494aSAndreas Gohr if($domain && is_array($this->conf[$domain])) foreach($this->conf[$domain] as $key => $val) { 64693a7873eSAndreas Gohr $opts[$key] = $val; 64793a7873eSAndreas Gohr } 64893a7873eSAndreas Gohr 64993a7873eSAndreas Gohr // handle multiple AD servers 65093a7873eSAndreas Gohr $opts['domain_controllers'] = explode(',', $opts['domain_controllers']); 65193a7873eSAndreas Gohr $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']); 65293a7873eSAndreas Gohr $opts['domain_controllers'] = array_filter($opts['domain_controllers']); 65393a7873eSAndreas Gohr 6548257d713SAndreas Gohr // compatibility with old option name 655*64159a61SAndreas Gohr if(empty($opts['admin_username']) && !empty($opts['ad_username'])) { 656*64159a61SAndreas Gohr $opts['admin_username'] = $opts['ad_username']; 657*64159a61SAndreas Gohr } 658*64159a61SAndreas Gohr if(empty($opts['admin_password']) && !empty($opts['ad_password'])) { 659*64159a61SAndreas Gohr $opts['admin_password'] = $opts['ad_password']; 660*64159a61SAndreas Gohr } 661342753d2SAndreas Gohr $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate 6628257d713SAndreas Gohr 66393a7873eSAndreas Gohr // we can change the password if SSL is set 66493a7873eSAndreas Gohr if($opts['use_ssl'] || $opts['use_tls']) { 66593a7873eSAndreas Gohr $this->cando['modPass'] = true; 66693a7873eSAndreas Gohr } else { 66793a7873eSAndreas Gohr $this->cando['modPass'] = false; 66893a7873eSAndreas Gohr } 66993a7873eSAndreas Gohr 67012d195abSAndreas Gohr // adLDAP expects empty user/pass as NULL, we're less strict FS#2781 67112d195abSAndreas Gohr if(empty($opts['admin_username'])) $opts['admin_username'] = null; 67212d195abSAndreas Gohr if(empty($opts['admin_password'])) $opts['admin_password'] = null; 67312d195abSAndreas Gohr 67412d195abSAndreas Gohr // user listing needs admin priviledges 6758257d713SAndreas Gohr if(!empty($opts['admin_username']) && !empty($opts['admin_password'])) { 67693a7873eSAndreas Gohr $this->cando['getUsers'] = true; 67793a7873eSAndreas Gohr } else { 6781b228d28SKlap-in $this->cando['getUsers'] = false; 67993a7873eSAndreas Gohr } 68093a7873eSAndreas Gohr 68193a7873eSAndreas Gohr return $opts; 68293a7873eSAndreas Gohr } 68393a7873eSAndreas Gohr 68493a7873eSAndreas Gohr /** 685741b8a48SAndreas Gohr * Returns a list of configured domains 686741b8a48SAndreas Gohr * 687741b8a48SAndreas Gohr * The default domain has an empty string as key 688741b8a48SAndreas Gohr * 689741b8a48SAndreas Gohr * @return array associative array(key => domain) 690741b8a48SAndreas Gohr */ 691741b8a48SAndreas Gohr public function _getConfiguredDomains() { 692741b8a48SAndreas Gohr $domains = array(); 693741b8a48SAndreas Gohr if(empty($this->conf['account_suffix'])) return $domains; // not configured yet 694741b8a48SAndreas Gohr 695741b8a48SAndreas Gohr // add default domain, using the name from account suffix 696741b8a48SAndreas Gohr $domains[''] = ltrim($this->conf['account_suffix'], '@'); 697741b8a48SAndreas Gohr 698741b8a48SAndreas Gohr // find additional domains 699741b8a48SAndreas Gohr foreach($this->conf as $key => $val) { 700741b8a48SAndreas Gohr if(is_array($val) && isset($val['account_suffix'])) { 701741b8a48SAndreas Gohr $domains[$key] = ltrim($val['account_suffix'], '@'); 702741b8a48SAndreas Gohr } 703741b8a48SAndreas Gohr } 704741b8a48SAndreas Gohr ksort($domains); 705741b8a48SAndreas Gohr 706741b8a48SAndreas Gohr return $domains; 707741b8a48SAndreas Gohr } 708741b8a48SAndreas Gohr 709741b8a48SAndreas Gohr /** 71093a7873eSAndreas Gohr * Check provided user and userinfo for matching patterns 71193a7873eSAndreas Gohr * 71293a7873eSAndreas Gohr * The patterns are set up with $this->_constructPattern() 71393a7873eSAndreas Gohr * 71493a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 715253d4b48SGerrit Uitslag * 71693a7873eSAndreas Gohr * @param string $user 71793a7873eSAndreas Gohr * @param array $info 71893a7873eSAndreas Gohr * @return bool 71993a7873eSAndreas Gohr */ 72093a7873eSAndreas Gohr protected function _filter($user, $info) { 72193a7873eSAndreas Gohr foreach($this->_pattern as $item => $pattern) { 72293a7873eSAndreas Gohr if($item == 'user') { 72393a7873eSAndreas Gohr if(!preg_match($pattern, $user)) return false; 72493a7873eSAndreas Gohr } else if($item == 'grps') { 72593a7873eSAndreas Gohr if(!count(preg_grep($pattern, $info['grps']))) return false; 72693a7873eSAndreas Gohr } else { 72793a7873eSAndreas Gohr if(!preg_match($pattern, $info[$item])) return false; 72893a7873eSAndreas Gohr } 72993a7873eSAndreas Gohr } 73093a7873eSAndreas Gohr return true; 73193a7873eSAndreas Gohr } 73293a7873eSAndreas Gohr 73393a7873eSAndreas Gohr /** 73493a7873eSAndreas Gohr * Create a pattern for $this->_filter() 73593a7873eSAndreas Gohr * 73693a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 737253d4b48SGerrit Uitslag * 73893a7873eSAndreas Gohr * @param array $filter 73993a7873eSAndreas Gohr */ 74093a7873eSAndreas Gohr protected function _constructPattern($filter) { 741f4476bd9SJan Schumann $this->_pattern = array(); 742f4476bd9SJan Schumann foreach($filter as $item => $pattern) { 743f4476bd9SJan Schumann $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters 744f4476bd9SJan Schumann } 745f4476bd9SJan Schumann } 746f4476bd9SJan Schumann} 747