1f4476bd9SJan Schumann<?php 2f4476bd9SJan Schumann// must be run within Dokuwiki 3f4476bd9SJan Schumannif(!defined('DOKU_INC')) die(); 4f4476bd9SJan Schumann 532fd494aSAndreas Gohrrequire_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php'); 632fd494aSAndreas Gohr 7f4476bd9SJan Schumann/** 8f4476bd9SJan Schumann * Active Directory authentication backend for DokuWiki 9f4476bd9SJan Schumann * 10f4476bd9SJan Schumann * This makes authentication with a Active Directory server much easier 11f4476bd9SJan Schumann * than when using the normal LDAP backend by utilizing the adLDAP library 12f4476bd9SJan Schumann * 13f4476bd9SJan Schumann * Usage: 14f4476bd9SJan Schumann * Set DokuWiki's local.protected.php auth setting to read 15f4476bd9SJan Schumann * 16f4476bd9SJan Schumann * $conf['authtype'] = 'authad'; 17f4476bd9SJan Schumann * 1832fd494aSAndreas Gohr * $conf['plugin']['authad']['account_suffix'] = '@my.domain.org'; 1932fd494aSAndreas Gohr * $conf['plugin']['authad']['base_dn'] = 'DC=my,DC=domain,DC=org'; 2032fd494aSAndreas Gohr * $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org'; 21f4476bd9SJan Schumann * 22f4476bd9SJan Schumann * //optional: 2332fd494aSAndreas Gohr * $conf['plugin']['authad']['sso'] = 1; 243002d731SAndreas Gohr * $conf['plugin']['authad']['admin_username'] = 'root'; 253002d731SAndreas Gohr * $conf['plugin']['authad']['admin_password'] = 'pass'; 2632fd494aSAndreas Gohr * $conf['plugin']['authad']['real_primarygroup'] = 1; 2732fd494aSAndreas Gohr * $conf['plugin']['authad']['use_ssl'] = 1; 2832fd494aSAndreas Gohr * $conf['plugin']['authad']['use_tls'] = 1; 2932fd494aSAndreas Gohr * $conf['plugin']['authad']['debug'] = 1; 3093a7873eSAndreas Gohr * // warn user about expiring password this many days in advance: 3132fd494aSAndreas Gohr * $conf['plugin']['authad']['expirywarn'] = 5; 32f4476bd9SJan Schumann * 33f4476bd9SJan Schumann * // get additional information to the userinfo array 34f4476bd9SJan Schumann * // add a list of comma separated ldap contact fields. 35f4476bd9SJan Schumann * $conf['plugin']['authad']['additional'] = 'field1,field2'; 36f4476bd9SJan Schumann * 37f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 38f4476bd9SJan Schumann * @author James Van Lommel <jamesvl@gmail.com> 39f4476bd9SJan Schumann * @link http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/ 40f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 41f4476bd9SJan Schumann * @author Jan Schumann <js@schumann-it.com> 42f4476bd9SJan Schumann */ 4393a7873eSAndreas Gohrclass auth_plugin_authad extends DokuWiki_Auth_Plugin { 4432fd494aSAndreas Gohr 4593a7873eSAndreas Gohr /** 4693a7873eSAndreas Gohr * @var array hold connection data for a specific AD domain 4793a7873eSAndreas Gohr */ 4893a7873eSAndreas Gohr protected $opts = array(); 4932fd494aSAndreas Gohr 5093a7873eSAndreas Gohr /** 5193a7873eSAndreas Gohr * @var array open connections for each AD domain, as adLDAP objects 5293a7873eSAndreas Gohr */ 5393a7873eSAndreas Gohr protected $adldap = array(); 5493a7873eSAndreas Gohr 5593a7873eSAndreas Gohr /** 5693a7873eSAndreas Gohr * @var bool message state 5793a7873eSAndreas Gohr */ 5893a7873eSAndreas Gohr protected $msgshown = false; 5993a7873eSAndreas Gohr 6093a7873eSAndreas Gohr /** 6193a7873eSAndreas Gohr * @var array user listing cache 6293a7873eSAndreas Gohr */ 6393a7873eSAndreas Gohr protected $users = array(); 6493a7873eSAndreas Gohr 6593a7873eSAndreas Gohr /** 6693a7873eSAndreas Gohr * @var array filter patterns for listing users 6793a7873eSAndreas Gohr */ 6893a7873eSAndreas Gohr protected $_pattern = array(); 69f4476bd9SJan Schumann 70f4476bd9SJan Schumann /** 71f4476bd9SJan Schumann * Constructor 72f4476bd9SJan Schumann */ 7393a7873eSAndreas Gohr public function __construct() { 7400d58927SMichael Hamann global $INPUT; 75454d868bSAndreas Gohr parent::__construct(); 76454d868bSAndreas Gohr 7732fd494aSAndreas Gohr // we load the config early to modify it a bit here 7832fd494aSAndreas Gohr $this->loadConfig(); 79f4476bd9SJan Schumann 80f4476bd9SJan Schumann // additional information fields 8132fd494aSAndreas Gohr if(isset($this->conf['additional'])) { 8232fd494aSAndreas Gohr $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']); 8332fd494aSAndreas Gohr $this->conf['additional'] = explode(',', $this->conf['additional']); 8432fd494aSAndreas Gohr } else $this->conf['additional'] = array(); 85f4476bd9SJan Schumann 86f4476bd9SJan Schumann // ldap extension is needed 87f4476bd9SJan Schumann if(!function_exists('ldap_connect')) { 8832fd494aSAndreas Gohr if($this->conf['debug']) 89f4476bd9SJan Schumann msg("AD Auth: PHP LDAP extension not found.", -1); 90f4476bd9SJan Schumann $this->success = false; 91f4476bd9SJan Schumann return; 92f4476bd9SJan Schumann } 93f4476bd9SJan Schumann 94f4476bd9SJan Schumann // Prepare SSO 95d34a2a38SAndreas Gohr if(!empty($_SERVER['REMOTE_USER'])) { 96d34a2a38SAndreas Gohr 97d34a2a38SAndreas Gohr // make sure the right encoding is used 98d34a2a38SAndreas Gohr if($this->getConf('sso_charset')) { 99d34a2a38SAndreas Gohr $_SERVER['REMOTE_USER'] = iconv($this->getConf('sso_charset'), 'UTF-8', $_SERVER['REMOTE_USER']); 100d34a2a38SAndreas Gohr } elseif(!utf8_check($_SERVER['REMOTE_USER'])) { 10193a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']); 10293a7873eSAndreas Gohr } 103d34a2a38SAndreas Gohr 104d34a2a38SAndreas Gohr // trust the incoming user 105d34a2a38SAndreas Gohr if($this->conf['sso']) { 10693a7873eSAndreas Gohr $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']); 107f4476bd9SJan Schumann 108f4476bd9SJan Schumann // we need to simulate a login 109f4476bd9SJan Schumann if(empty($_COOKIE[DOKU_COOKIE])) { 11000d58927SMichael Hamann $INPUT->set('u', $_SERVER['REMOTE_USER']); 11100d58927SMichael Hamann $INPUT->set('p', 'sso_only'); 112f4476bd9SJan Schumann } 113f4476bd9SJan Schumann } 114d34a2a38SAndreas Gohr } 115f4476bd9SJan Schumann 11693a7873eSAndreas Gohr // other can do's are changed in $this->_loadServerConfig() base on domain setup 117f4476bd9SJan Schumann $this->cando['modName'] = true; 118f4476bd9SJan Schumann $this->cando['modMail'] = true; 11925f80763SMichael Große $this->cando['getUserCount'] = true; 120f4476bd9SJan Schumann } 121f4476bd9SJan Schumann 122f4476bd9SJan Schumann /** 123a154806fSAndreas Gohr * Load domain config on capability check 124a154806fSAndreas Gohr * 125a154806fSAndreas Gohr * @param string $cap 126a154806fSAndreas Gohr * @return bool 127a154806fSAndreas Gohr */ 128a154806fSAndreas Gohr public function canDo($cap) { 129a154806fSAndreas Gohr //capabilities depend on config, which may change depending on domain 130a154806fSAndreas Gohr $domain = $this->_userDomain($_SERVER['REMOTE_USER']); 131a154806fSAndreas Gohr $this->_loadServerConfig($domain); 132a154806fSAndreas Gohr return parent::canDo($cap); 133a154806fSAndreas Gohr } 134a154806fSAndreas Gohr 135a154806fSAndreas Gohr /** 136f4476bd9SJan Schumann * Check user+password [required auth function] 137f4476bd9SJan Schumann * 138f4476bd9SJan Schumann * Checks if the given user exists and the given 139f4476bd9SJan Schumann * plaintext password is correct by trying to bind 140f4476bd9SJan Schumann * to the LDAP server 141f4476bd9SJan Schumann * 142f4476bd9SJan Schumann * @author James Van Lommel <james@nosq.com> 14393a7873eSAndreas Gohr * @param string $user 14493a7873eSAndreas Gohr * @param string $pass 145f4476bd9SJan Schumann * @return bool 146f4476bd9SJan Schumann */ 14793a7873eSAndreas Gohr public function checkPass($user, $pass) { 148f4476bd9SJan Schumann if($_SERVER['REMOTE_USER'] && 149f4476bd9SJan Schumann $_SERVER['REMOTE_USER'] == $user && 15032fd494aSAndreas Gohr $this->conf['sso'] 15193a7873eSAndreas Gohr ) return true; 152f4476bd9SJan Schumann 15393a7873eSAndreas Gohr $adldap = $this->_adldap($this->_userDomain($user)); 15493a7873eSAndreas Gohr if(!$adldap) return false; 15593a7873eSAndreas Gohr 15693a7873eSAndreas Gohr return $adldap->authenticate($this->_userName($user), $pass); 157f4476bd9SJan Schumann } 158f4476bd9SJan Schumann 159f4476bd9SJan Schumann /** 160f4476bd9SJan Schumann * Return user info [required auth function] 161f4476bd9SJan Schumann * 162f4476bd9SJan Schumann * Returns info about the given user needs to contain 163f4476bd9SJan Schumann * at least these fields: 164f4476bd9SJan Schumann * 165f4476bd9SJan Schumann * name string full name of the user 166f4476bd9SJan Schumann * mail string email address of the user 167f4476bd9SJan Schumann * grps array list of groups the user is in 168f4476bd9SJan Schumann * 16993a7873eSAndreas Gohr * This AD specific function returns the following 170f4476bd9SJan Schumann * addional fields: 171f4476bd9SJan Schumann * 172f4476bd9SJan Schumann * dn string distinguished name (DN) 17393a7873eSAndreas Gohr * uid string samaccountname 17493a7873eSAndreas Gohr * lastpwd int timestamp of the date when the password was set 17593a7873eSAndreas Gohr * expires true if the password expires 17693a7873eSAndreas Gohr * expiresin int seconds until the password expires 17793a7873eSAndreas Gohr * any fields specified in the 'additional' config option 178f4476bd9SJan Schumann * 179f4476bd9SJan Schumann * @author James Van Lommel <james@nosq.com> 18093a7873eSAndreas Gohr * @param string $user 1812046a654SChristopher Smith * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin 18293a7873eSAndreas Gohr * @return array 183f4476bd9SJan Schumann */ 1842046a654SChristopher Smith public function getUserData($user, $requireGroups=true) { 185f4476bd9SJan Schumann global $conf; 18693a7873eSAndreas Gohr global $lang; 18793a7873eSAndreas Gohr global $ID; 18893a7873eSAndreas Gohr $adldap = $this->_adldap($this->_userDomain($user)); 18993a7873eSAndreas Gohr if(!$adldap) return false; 190f4476bd9SJan Schumann 19193a7873eSAndreas Gohr if($user == '') return array(); 19293a7873eSAndreas Gohr 19393a7873eSAndreas Gohr $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol'); 194f4476bd9SJan Schumann 195f4476bd9SJan Schumann // add additional fields to read 19632fd494aSAndreas Gohr $fields = array_merge($fields, $this->conf['additional']); 197f4476bd9SJan Schumann $fields = array_unique($fields); 19814642325SAndreas Gohr $fields = array_filter($fields); 199f4476bd9SJan Schumann 200f4476bd9SJan Schumann //get info for given user 20132fd494aSAndreas Gohr $result = $adldap->user()->info($this->_userName($user), $fields); 20293a7873eSAndreas Gohr if($result == false){ 20393a7873eSAndreas Gohr return array(); 20493a7873eSAndreas Gohr } 20593a7873eSAndreas Gohr 206f4476bd9SJan Schumann //general user info 20759bc3b48SGerrit Uitslag $info = array(); 208f4476bd9SJan Schumann $info['name'] = $result[0]['displayname'][0]; 209f4476bd9SJan Schumann $info['mail'] = $result[0]['mail'][0]; 210f4476bd9SJan Schumann $info['uid'] = $result[0]['samaccountname'][0]; 211f4476bd9SJan Schumann $info['dn'] = $result[0]['dn']; 21293a7873eSAndreas Gohr //last password set (Windows counts from January 1st 1601) 21393a7873eSAndreas Gohr $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600; 21493a7873eSAndreas Gohr //will it expire? 21593a7873eSAndreas Gohr $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD 216f4476bd9SJan Schumann 217f4476bd9SJan Schumann // additional information 21832fd494aSAndreas Gohr foreach($this->conf['additional'] as $field) { 219f4476bd9SJan Schumann if(isset($result[0][strtolower($field)])) { 220f4476bd9SJan Schumann $info[$field] = $result[0][strtolower($field)][0]; 221f4476bd9SJan Schumann } 222f4476bd9SJan Schumann } 223f4476bd9SJan Schumann 224f4476bd9SJan Schumann // handle ActiveDirectory memberOf 22532fd494aSAndreas Gohr $info['grps'] = $adldap->user()->groups($this->_userName($user),(bool) $this->opts['recursive_groups']); 226f4476bd9SJan Schumann 227f4476bd9SJan Schumann if(is_array($info['grps'])) { 228f4476bd9SJan Schumann foreach($info['grps'] as $ndx => $group) { 229f4476bd9SJan Schumann $info['grps'][$ndx] = $this->cleanGroup($group); 230f4476bd9SJan Schumann } 231f4476bd9SJan Schumann } 232f4476bd9SJan Schumann 233f4476bd9SJan Schumann // always add the default group to the list of groups 234f4476bd9SJan Schumann if(!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) { 235f4476bd9SJan Schumann $info['grps'][] = $conf['defaultgroup']; 236f4476bd9SJan Schumann } 237f4476bd9SJan Schumann 23893a7873eSAndreas Gohr // add the user's domain to the groups 23993a7873eSAndreas Gohr $domain = $this->_userDomain($user); 24093a7873eSAndreas Gohr if($domain && !in_array("domain-$domain", (array) $info['grps'])) { 24193a7873eSAndreas Gohr $info['grps'][] = $this->cleanGroup("domain-$domain"); 24293a7873eSAndreas Gohr } 24393a7873eSAndreas Gohr 24493a7873eSAndreas Gohr // check expiry time 24532fd494aSAndreas Gohr if($info['expires'] && $this->conf['expirywarn']){ 2461e52e72aSAndreas Gohr $expiry = $adldap->user()->passwordExpiry($user); 2471e52e72aSAndreas Gohr if(is_array($expiry)){ 2481e52e72aSAndreas Gohr $info['expiresat'] = $expiry['expiryts']; 2491e52e72aSAndreas Gohr $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60)); 25093a7873eSAndreas Gohr 25193a7873eSAndreas Gohr // if this is the current user, warn him (once per request only) 25293a7873eSAndreas Gohr if(($_SERVER['REMOTE_USER'] == $user) && 2531e52e72aSAndreas Gohr ($info['expiresin'] <= $this->conf['expirywarn']) && 25493a7873eSAndreas Gohr !$this->msgshown 25593a7873eSAndreas Gohr ) { 2561e52e72aSAndreas Gohr $msg = sprintf($lang['authpwdexpire'], $info['expiresin']); 25793a7873eSAndreas Gohr if($this->canDo('modPass')) { 25893a7873eSAndreas Gohr $url = wl($ID, array('do'=> 'profile')); 25993a7873eSAndreas Gohr $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>'; 26093a7873eSAndreas Gohr } 26193a7873eSAndreas Gohr msg($msg); 26293a7873eSAndreas Gohr $this->msgshown = true; 26393a7873eSAndreas Gohr } 26493a7873eSAndreas Gohr } 2651e52e72aSAndreas Gohr } 26693a7873eSAndreas Gohr 267f4476bd9SJan Schumann return $info; 268f4476bd9SJan Schumann } 269f4476bd9SJan Schumann 270f4476bd9SJan Schumann /** 271f4476bd9SJan Schumann * Make AD group names usable by DokuWiki. 272f4476bd9SJan Schumann * 273f4476bd9SJan Schumann * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores. 274f4476bd9SJan Schumann * 275f4476bd9SJan Schumann * @author James Van Lommel (jamesvl@gmail.com) 27693a7873eSAndreas Gohr * @param string $group 27793a7873eSAndreas Gohr * @return string 278f4476bd9SJan Schumann */ 27993a7873eSAndreas Gohr public function cleanGroup($group) { 28093a7873eSAndreas Gohr $group = str_replace('\\', '', $group); 28193a7873eSAndreas Gohr $group = str_replace('#', '', $group); 28293a7873eSAndreas Gohr $group = preg_replace('[\s]', '_', $group); 28393a7873eSAndreas Gohr $group = utf8_strtolower(trim($group)); 28493a7873eSAndreas Gohr return $group; 285f4476bd9SJan Schumann } 286f4476bd9SJan Schumann 287f4476bd9SJan Schumann /** 288f4476bd9SJan Schumann * Sanitize user names 28993a7873eSAndreas Gohr * 29093a7873eSAndreas Gohr * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup) 29193a7873eSAndreas Gohr * 29293a7873eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 29393a7873eSAndreas Gohr * @param string $user 29493a7873eSAndreas Gohr * @return string 295f4476bd9SJan Schumann */ 29693a7873eSAndreas Gohr public function cleanUser($user) { 29793a7873eSAndreas Gohr $domain = ''; 29893a7873eSAndreas Gohr 29993a7873eSAndreas Gohr // get NTLM or Kerberos domain part 30093a7873eSAndreas Gohr list($dom, $user) = explode('\\', $user, 2); 30193a7873eSAndreas Gohr if(!$user) $user = $dom; 30293a7873eSAndreas Gohr if($dom) $domain = $dom; 30393a7873eSAndreas Gohr list($user, $dom) = explode('@', $user, 2); 30493a7873eSAndreas Gohr if($dom) $domain = $dom; 30593a7873eSAndreas Gohr 30693a7873eSAndreas Gohr // clean up both 30793a7873eSAndreas Gohr $domain = utf8_strtolower(trim($domain)); 30893a7873eSAndreas Gohr $user = utf8_strtolower(trim($user)); 30993a7873eSAndreas Gohr 31093a7873eSAndreas Gohr // is this a known, valid domain? if not discard 31132fd494aSAndreas Gohr if(!is_array($this->conf[$domain])) { 31293a7873eSAndreas Gohr $domain = ''; 31393a7873eSAndreas Gohr } 31493a7873eSAndreas Gohr 31593a7873eSAndreas Gohr // reattach domain 31693a7873eSAndreas Gohr if($domain) $user = "$user@$domain"; 31793a7873eSAndreas Gohr return $user; 318f4476bd9SJan Schumann } 319f4476bd9SJan Schumann 320f4476bd9SJan Schumann /** 321f4476bd9SJan Schumann * Most values in LDAP are case-insensitive 32293a7873eSAndreas Gohr * 32393a7873eSAndreas Gohr * @return bool 324f4476bd9SJan Schumann */ 32593a7873eSAndreas Gohr public function isCaseSensitive() { 326f4476bd9SJan Schumann return false; 327f4476bd9SJan Schumann } 328f4476bd9SJan Schumann 329*67a31a83SMichael Große protected function _constructSearchString($filter){ 330*67a31a83SMichael Große if (!$filter){ 331*67a31a83SMichael Große return '*'; 332*67a31a83SMichael Große } 333*67a31a83SMichael Große $result = '*'; 334*67a31a83SMichael Große if (isset($filter['name'])) { 335*67a31a83SMichael Große $result .= ')(displayname=*' . $filter['name'] . '*'; 336*67a31a83SMichael Große unset($filter['name']); 337*67a31a83SMichael Große } 338*67a31a83SMichael Große if (isset($filter['user'])) { 339*67a31a83SMichael Große $result .= ')(samAccountName=*' . $filter['user'] . '*'; 340*67a31a83SMichael Große unset($filter['user']); 341*67a31a83SMichael Große } 342*67a31a83SMichael Große 343*67a31a83SMichael Große if (isset($filter['mail'])) { 344*67a31a83SMichael Große $result .= ')(mail=*' . $filter['mail'] . '*'; 345*67a31a83SMichael Große unset($filter['mail']); 346*67a31a83SMichael Große } 347*67a31a83SMichael Große dbglog($result); 348*67a31a83SMichael Große return $result; 349*67a31a83SMichael Große } 350*67a31a83SMichael Große 351f4476bd9SJan Schumann /** 35225f80763SMichael Große * @param array $filter 35325f80763SMichael Große * @return int 35425f80763SMichael Große */ 35525f80763SMichael Große public function getUserCount($filter = array()) { 356*67a31a83SMichael Große if ($filter == array()) { 35725f80763SMichael Große $adldap = $this->_adldap(null); 35825f80763SMichael Große if(!$adldap) { 359*67a31a83SMichael Große dbglog("authad/auth.php getUserCount(): _adldap not set."); 36025f80763SMichael Große return -1; 36125f80763SMichael Große } 36225f80763SMichael Große $result = $adldap->user()->all(); 363*67a31a83SMichael Große $start = 0; 364*67a31a83SMichael Große } else {/* 365*67a31a83SMichael Große dbglog('_startcache: ' . $this->_startcache); 366*67a31a83SMichael Große $usermanager = plugin_load("admin", "usermanager", false); 367*67a31a83SMichael Große if ($this->_startcache < $usermanager->getStart()) { 368*67a31a83SMichael Große $start = $usermanager->getStart(); 369*67a31a83SMichael Große $this->_startcache = $start; 370*67a31a83SMichael Große } else { 371*67a31a83SMichael Große $start = $this->_startcache; 372*67a31a83SMichael Große } 373*67a31a83SMichael Große $pagesize = $usermanager->getPagesize(); 374*67a31a83SMichael Große $result = $this->retrieveUsers($start, 3*$pagesize,$filter,false);*/ 375*67a31a83SMichael Große $adldap = $this->_adldap(null); 376*67a31a83SMichael Große if(!$adldap) { 377*67a31a83SMichael Große dbglog("authad/auth.php getUserCount(): _adldap not set."); 378*67a31a83SMichael Große return -1; 379*67a31a83SMichael Große } 380*67a31a83SMichael Große dbglog($filter); 381*67a31a83SMichael Große $searchString = $this->_constructSearchString($filter); 382*67a31a83SMichael Große $result = $adldap->user()->all(false, $searchString); 383*67a31a83SMichael Große 384*67a31a83SMichael Große } 385*67a31a83SMichael Große 38625f80763SMichael Große if (!$result) { 38725f80763SMichael Große dbglog("authad/auth.php: getting all users failed."); 38825f80763SMichael Große return -1; 38925f80763SMichael Große } 39025f80763SMichael Große return count($result); 39125f80763SMichael Große } 39225f80763SMichael Große 39325f80763SMichael Große /** 394f4476bd9SJan Schumann * Bulk retrieval of user data 395f4476bd9SJan Schumann * 396f4476bd9SJan Schumann * @author Dominik Eckelmann <dokuwiki@cosmocode.de> 397253d4b48SGerrit Uitslag * 39893a7873eSAndreas Gohr * @param int $start index of first user to be returned 39993a7873eSAndreas Gohr * @param int $limit max number of users to be returned 40093a7873eSAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 401*67a31a83SMichael Große * @param bool $setStart 40293a7873eSAndreas Gohr * @return array userinfo (refer getUserData for internal userinfo details) 403f4476bd9SJan Schumann */ 404*67a31a83SMichael Große public function retrieveUsers($start = 0, $limit = 0, $filter = array(), $setStart = true) { 405*67a31a83SMichael Große dbglog("start: " . $start . "; limit: " . $limit); 40693a7873eSAndreas Gohr $adldap = $this->_adldap(null); 40793a7873eSAndreas Gohr if(!$adldap) return false; 408f4476bd9SJan Schumann 4090ba750c0SAndreas Gohr if(!$this->users) { 410f4476bd9SJan Schumann //get info for given user 411*67a31a83SMichael Große $result = $adldap->user()->all(false, $this->_constructSearchString($filter)); 412f4476bd9SJan Schumann if (!$result) return array(); 413f4476bd9SJan Schumann $this->users = array_fill_keys($result, false); 414f4476bd9SJan Schumann } 415f4476bd9SJan Schumann 416f4476bd9SJan Schumann $i = 0; 417f4476bd9SJan Schumann $count = 0; 418f4476bd9SJan Schumann $this->_constructPattern($filter); 419f4476bd9SJan Schumann $result = array(); 420f4476bd9SJan Schumann 421f4476bd9SJan Schumann foreach($this->users as $user => &$info) { 422f4476bd9SJan Schumann if($i++ < $start) { 423f4476bd9SJan Schumann continue; 424f4476bd9SJan Schumann } 425f4476bd9SJan Schumann if($info === false) { 426f4476bd9SJan Schumann $info = $this->getUserData($user); 427f4476bd9SJan Schumann } 428f4476bd9SJan Schumann if($this->_filter($user, $info)) { 429f4476bd9SJan Schumann $result[$user] = $info; 4309a2c73e8SAndreas Gohr if(($limit > 0) && (++$count >= $limit)) break; 431f4476bd9SJan Schumann } 432f4476bd9SJan Schumann } 433f4476bd9SJan Schumann return $result; 434f4476bd9SJan Schumann } 435f4476bd9SJan Schumann 436f4476bd9SJan Schumann /** 437f4476bd9SJan Schumann * Modify user data 438f4476bd9SJan Schumann * 43993a7873eSAndreas Gohr * @param string $user nick of the user to be changed 44093a7873eSAndreas Gohr * @param array $changes array of field/value pairs to be changed 441f4476bd9SJan Schumann * @return bool 442f4476bd9SJan Schumann */ 44393a7873eSAndreas Gohr public function modifyUser($user, $changes) { 444f4476bd9SJan Schumann $return = true; 44593a7873eSAndreas Gohr $adldap = $this->_adldap($this->_userDomain($user)); 44693a7873eSAndreas Gohr if(!$adldap) return false; 447f4476bd9SJan Schumann 448f4476bd9SJan Schumann // password changing 449f4476bd9SJan Schumann if(isset($changes['pass'])) { 450f4476bd9SJan Schumann try { 45132fd494aSAndreas Gohr $return = $adldap->user()->password($this->_userName($user),$changes['pass']); 452f4476bd9SJan Schumann } catch (adLDAPException $e) { 45332fd494aSAndreas Gohr if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); 454f4476bd9SJan Schumann $return = false; 455f4476bd9SJan Schumann } 456f4476bd9SJan Schumann if(!$return) msg('AD Auth: failed to change the password. Maybe the password policy was not met?', -1); 457f4476bd9SJan Schumann } 458f4476bd9SJan Schumann 459f4476bd9SJan Schumann // changing user data 460f4476bd9SJan Schumann $adchanges = array(); 461f4476bd9SJan Schumann if(isset($changes['name'])) { 462f4476bd9SJan Schumann // get first and last name 463f4476bd9SJan Schumann $parts = explode(' ', $changes['name']); 464f4476bd9SJan Schumann $adchanges['surname'] = array_pop($parts); 465f4476bd9SJan Schumann $adchanges['firstname'] = join(' ', $parts); 466f4476bd9SJan Schumann $adchanges['display_name'] = $changes['name']; 467f4476bd9SJan Schumann } 468f4476bd9SJan Schumann if(isset($changes['mail'])) { 469f4476bd9SJan Schumann $adchanges['email'] = $changes['mail']; 470f4476bd9SJan Schumann } 471f4476bd9SJan Schumann if(count($adchanges)) { 472f4476bd9SJan Schumann try { 47332fd494aSAndreas Gohr $return = $return & $adldap->user()->modify($this->_userName($user),$adchanges); 474f4476bd9SJan Schumann } catch (adLDAPException $e) { 47532fd494aSAndreas Gohr if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); 476f4476bd9SJan Schumann $return = false; 477f4476bd9SJan Schumann } 478f4476bd9SJan Schumann } 479f4476bd9SJan Schumann 480f4476bd9SJan Schumann return $return; 481f4476bd9SJan Schumann } 482f4476bd9SJan Schumann 483f4476bd9SJan Schumann /** 484f4476bd9SJan Schumann * Initialize the AdLDAP library and connect to the server 48593a7873eSAndreas Gohr * 48693a7873eSAndreas Gohr * When you pass null as domain, it will reuse any existing domain. 48793a7873eSAndreas Gohr * Eg. the one of the logged in user. It falls back to the default 48893a7873eSAndreas Gohr * domain if no current one is available. 48993a7873eSAndreas Gohr * 49093a7873eSAndreas Gohr * @param string|null $domain The AD domain to use 49193a7873eSAndreas Gohr * @return adLDAP|bool true if a connection was established 492f4476bd9SJan Schumann */ 49393a7873eSAndreas Gohr protected function _adldap($domain) { 49493a7873eSAndreas Gohr if(is_null($domain) && is_array($this->opts)) { 49593a7873eSAndreas Gohr $domain = $this->opts['domain']; 49693a7873eSAndreas Gohr } 49793a7873eSAndreas Gohr 49893a7873eSAndreas Gohr $this->opts = $this->_loadServerConfig((string) $domain); 49993a7873eSAndreas Gohr if(isset($this->adldap[$domain])) return $this->adldap[$domain]; 500f4476bd9SJan Schumann 501f4476bd9SJan Schumann // connect 502f4476bd9SJan Schumann try { 50393a7873eSAndreas Gohr $this->adldap[$domain] = new adLDAP($this->opts); 50493a7873eSAndreas Gohr return $this->adldap[$domain]; 505f4476bd9SJan Schumann } catch(adLDAPException $e) { 50632fd494aSAndreas Gohr if($this->conf['debug']) { 507f4476bd9SJan Schumann msg('AD Auth: '.$e->getMessage(), -1); 508f4476bd9SJan Schumann } 509f4476bd9SJan Schumann $this->success = false; 51093a7873eSAndreas Gohr $this->adldap[$domain] = null; 511f4476bd9SJan Schumann } 512f4476bd9SJan Schumann return false; 513f4476bd9SJan Schumann } 514f4476bd9SJan Schumann 515f4476bd9SJan Schumann /** 51693a7873eSAndreas Gohr * Get the domain part from a user 517f4476bd9SJan Schumann * 518253d4b48SGerrit Uitslag * @param string $user 51993a7873eSAndreas Gohr * @return string 520f4476bd9SJan Schumann */ 52193a7873eSAndreas Gohr public function _userDomain($user) { 52293a7873eSAndreas Gohr list(, $domain) = explode('@', $user, 2); 52393a7873eSAndreas Gohr return $domain; 524f4476bd9SJan Schumann } 525f4476bd9SJan Schumann 52693a7873eSAndreas Gohr /** 52793a7873eSAndreas Gohr * Get the user part from a user 52893a7873eSAndreas Gohr * 529253d4b48SGerrit Uitslag * @param string $user 53093a7873eSAndreas Gohr * @return string 53193a7873eSAndreas Gohr */ 53293a7873eSAndreas Gohr public function _userName($user) { 53393a7873eSAndreas Gohr list($name) = explode('@', $user, 2); 53493a7873eSAndreas Gohr return $name; 53593a7873eSAndreas Gohr } 53693a7873eSAndreas Gohr 53793a7873eSAndreas Gohr /** 53893a7873eSAndreas Gohr * Fetch the configuration for the given AD domain 53993a7873eSAndreas Gohr * 54093a7873eSAndreas Gohr * @param string $domain current AD domain 54193a7873eSAndreas Gohr * @return array 54293a7873eSAndreas Gohr */ 54393a7873eSAndreas Gohr protected function _loadServerConfig($domain) { 54493a7873eSAndreas Gohr // prepare adLDAP standard configuration 54532fd494aSAndreas Gohr $opts = $this->conf; 54693a7873eSAndreas Gohr 54793a7873eSAndreas Gohr $opts['domain'] = $domain; 54893a7873eSAndreas Gohr 54993a7873eSAndreas Gohr // add possible domain specific configuration 55032fd494aSAndreas Gohr if($domain && is_array($this->conf[$domain])) foreach($this->conf[$domain] as $key => $val) { 55193a7873eSAndreas Gohr $opts[$key] = $val; 55293a7873eSAndreas Gohr } 55393a7873eSAndreas Gohr 55493a7873eSAndreas Gohr // handle multiple AD servers 55593a7873eSAndreas Gohr $opts['domain_controllers'] = explode(',', $opts['domain_controllers']); 55693a7873eSAndreas Gohr $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']); 55793a7873eSAndreas Gohr $opts['domain_controllers'] = array_filter($opts['domain_controllers']); 55893a7873eSAndreas Gohr 5598257d713SAndreas Gohr // compatibility with old option name 5608257d713SAndreas Gohr if(empty($opts['admin_username']) && !empty($opts['ad_username'])) $opts['admin_username'] = $opts['ad_username']; 5618257d713SAndreas Gohr if(empty($opts['admin_password']) && !empty($opts['ad_password'])) $opts['admin_password'] = $opts['ad_password']; 5628257d713SAndreas Gohr 56393a7873eSAndreas Gohr // we can change the password if SSL is set 56493a7873eSAndreas Gohr if($opts['use_ssl'] || $opts['use_tls']) { 56593a7873eSAndreas Gohr $this->cando['modPass'] = true; 56693a7873eSAndreas Gohr } else { 56793a7873eSAndreas Gohr $this->cando['modPass'] = false; 56893a7873eSAndreas Gohr } 56993a7873eSAndreas Gohr 57012d195abSAndreas Gohr // adLDAP expects empty user/pass as NULL, we're less strict FS#2781 57112d195abSAndreas Gohr if(empty($opts['admin_username'])) $opts['admin_username'] = null; 57212d195abSAndreas Gohr if(empty($opts['admin_password'])) $opts['admin_password'] = null; 57312d195abSAndreas Gohr 57412d195abSAndreas Gohr // user listing needs admin priviledges 5758257d713SAndreas Gohr if(!empty($opts['admin_username']) && !empty($opts['admin_password'])) { 57693a7873eSAndreas Gohr $this->cando['getUsers'] = true; 57793a7873eSAndreas Gohr } else { 5781b228d28SKlap-in $this->cando['getUsers'] = false; 57993a7873eSAndreas Gohr } 58093a7873eSAndreas Gohr 58193a7873eSAndreas Gohr return $opts; 58293a7873eSAndreas Gohr } 58393a7873eSAndreas Gohr 58493a7873eSAndreas Gohr /** 585741b8a48SAndreas Gohr * Returns a list of configured domains 586741b8a48SAndreas Gohr * 587741b8a48SAndreas Gohr * The default domain has an empty string as key 588741b8a48SAndreas Gohr * 589741b8a48SAndreas Gohr * @return array associative array(key => domain) 590741b8a48SAndreas Gohr */ 591741b8a48SAndreas Gohr public function _getConfiguredDomains() { 592741b8a48SAndreas Gohr $domains = array(); 593741b8a48SAndreas Gohr if(empty($this->conf['account_suffix'])) return $domains; // not configured yet 594741b8a48SAndreas Gohr 595741b8a48SAndreas Gohr // add default domain, using the name from account suffix 596741b8a48SAndreas Gohr $domains[''] = ltrim($this->conf['account_suffix'], '@'); 597741b8a48SAndreas Gohr 598741b8a48SAndreas Gohr // find additional domains 599741b8a48SAndreas Gohr foreach($this->conf as $key => $val) { 600741b8a48SAndreas Gohr if(is_array($val) && isset($val['account_suffix'])) { 601741b8a48SAndreas Gohr $domains[$key] = ltrim($val['account_suffix'], '@'); 602741b8a48SAndreas Gohr } 603741b8a48SAndreas Gohr } 604741b8a48SAndreas Gohr ksort($domains); 605741b8a48SAndreas Gohr 606741b8a48SAndreas Gohr return $domains; 607741b8a48SAndreas Gohr } 608741b8a48SAndreas Gohr 609741b8a48SAndreas Gohr /** 61093a7873eSAndreas Gohr * Check provided user and userinfo for matching patterns 61193a7873eSAndreas Gohr * 61293a7873eSAndreas Gohr * The patterns are set up with $this->_constructPattern() 61393a7873eSAndreas Gohr * 61493a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 615253d4b48SGerrit Uitslag * 61693a7873eSAndreas Gohr * @param string $user 61793a7873eSAndreas Gohr * @param array $info 61893a7873eSAndreas Gohr * @return bool 61993a7873eSAndreas Gohr */ 62093a7873eSAndreas Gohr protected function _filter($user, $info) { 62193a7873eSAndreas Gohr foreach($this->_pattern as $item => $pattern) { 62293a7873eSAndreas Gohr if($item == 'user') { 62393a7873eSAndreas Gohr if(!preg_match($pattern, $user)) return false; 62493a7873eSAndreas Gohr } else if($item == 'grps') { 62593a7873eSAndreas Gohr if(!count(preg_grep($pattern, $info['grps']))) return false; 62693a7873eSAndreas Gohr } else { 62793a7873eSAndreas Gohr if(!preg_match($pattern, $info[$item])) return false; 62893a7873eSAndreas Gohr } 62993a7873eSAndreas Gohr } 63093a7873eSAndreas Gohr return true; 63193a7873eSAndreas Gohr } 63293a7873eSAndreas Gohr 63393a7873eSAndreas Gohr /** 63493a7873eSAndreas Gohr * Create a pattern for $this->_filter() 63593a7873eSAndreas Gohr * 63693a7873eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 637253d4b48SGerrit Uitslag * 63893a7873eSAndreas Gohr * @param array $filter 63993a7873eSAndreas Gohr */ 64093a7873eSAndreas Gohr protected function _constructPattern($filter) { 641f4476bd9SJan Schumann $this->_pattern = array(); 642f4476bd9SJan Schumann foreach($filter as $item => $pattern) { 643f4476bd9SJan Schumann $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters 644f4476bd9SJan Schumann } 645f4476bd9SJan Schumann } 646f4476bd9SJan Schumann} 647