PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/authad/auth.php

http://github.com/splitbrain/dokuwiki
PHP | 786 lines | 575 code | 45 blank | 166 comment | 43 complexity | 69c1fdecd2c098955f233064efc0a135 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Active Directory authentication backend for DokuWiki
  4. *
  5. * This makes authentication with a Active Directory server much easier
  6. * than when using the normal LDAP backend by utilizing the adLDAP library
  7. *
  8. * Usage:
  9. * Set DokuWiki's local.protected.php auth setting to read
  10. *
  11. * $conf['authtype'] = 'authad';
  12. *
  13. * $conf['plugin']['authad']['account_suffix'] = '@my.domain.org';
  14. * $conf['plugin']['authad']['base_dn'] = 'DC=my,DC=domain,DC=org';
  15. * $conf['plugin']['authad']['domain_controllers'] = 'srv1.domain.org,srv2.domain.org';
  16. *
  17. * //optional:
  18. * $conf['plugin']['authad']['sso'] = 1;
  19. * $conf['plugin']['authad']['admin_username'] = 'root';
  20. * $conf['plugin']['authad']['admin_password'] = 'pass';
  21. * $conf['plugin']['authad']['real_primarygroup'] = 1;
  22. * $conf['plugin']['authad']['use_ssl'] = 1;
  23. * $conf['plugin']['authad']['use_tls'] = 1;
  24. * $conf['plugin']['authad']['debug'] = 1;
  25. * // warn user about expiring password this many days in advance:
  26. * $conf['plugin']['authad']['expirywarn'] = 5;
  27. *
  28. * // get additional information to the userinfo array
  29. * // add a list of comma separated ldap contact fields.
  30. * $conf['plugin']['authad']['additional'] = 'field1,field2';
  31. *
  32. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  33. * @author James Van Lommel <jamesvl@gmail.com>
  34. * @link http://www.nosq.com/blog/2005/08/ldap-activedirectory-and-dokuwiki/
  35. * @author Andreas Gohr <andi@splitbrain.org>
  36. * @author Jan Schumann <js@schumann-it.com>
  37. */
  38. class auth_plugin_authad extends DokuWiki_Auth_Plugin
  39. {
  40. /**
  41. * @var array hold connection data for a specific AD domain
  42. */
  43. protected $opts = array();
  44. /**
  45. * @var array open connections for each AD domain, as adLDAP objects
  46. */
  47. protected $adldap = array();
  48. /**
  49. * @var bool message state
  50. */
  51. protected $msgshown = false;
  52. /**
  53. * @var array user listing cache
  54. */
  55. protected $users = array();
  56. /**
  57. * @var array filter patterns for listing users
  58. */
  59. protected $pattern = array();
  60. protected $grpsusers = array();
  61. /**
  62. * Constructor
  63. */
  64. public function __construct()
  65. {
  66. global $INPUT;
  67. parent::__construct();
  68. require_once(DOKU_PLUGIN.'authad/adLDAP/adLDAP.php');
  69. require_once(DOKU_PLUGIN.'authad/adLDAP/classes/adLDAPUtils.php');
  70. // we load the config early to modify it a bit here
  71. $this->loadConfig();
  72. // additional information fields
  73. if (isset($this->conf['additional'])) {
  74. $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']);
  75. $this->conf['additional'] = explode(',', $this->conf['additional']);
  76. } else $this->conf['additional'] = array();
  77. // ldap extension is needed
  78. if (!function_exists('ldap_connect')) {
  79. if ($this->conf['debug'])
  80. msg("AD Auth: PHP LDAP extension not found.", -1);
  81. $this->success = false;
  82. return;
  83. }
  84. // Prepare SSO
  85. if (!empty($_SERVER['REMOTE_USER'])) {
  86. // make sure the right encoding is used
  87. if ($this->getConf('sso_charset')) {
  88. $_SERVER['REMOTE_USER'] = iconv($this->getConf('sso_charset'), 'UTF-8', $_SERVER['REMOTE_USER']);
  89. } elseif (!\dokuwiki\Utf8\Clean::isUtf8($_SERVER['REMOTE_USER'])) {
  90. $_SERVER['REMOTE_USER'] = utf8_encode($_SERVER['REMOTE_USER']);
  91. }
  92. // trust the incoming user
  93. if ($this->conf['sso']) {
  94. $_SERVER['REMOTE_USER'] = $this->cleanUser($_SERVER['REMOTE_USER']);
  95. // we need to simulate a login
  96. if (empty($_COOKIE[DOKU_COOKIE])) {
  97. $INPUT->set('u', $_SERVER['REMOTE_USER']);
  98. $INPUT->set('p', 'sso_only');
  99. }
  100. }
  101. }
  102. // other can do's are changed in $this->_loadServerConfig() base on domain setup
  103. $this->cando['modName'] = (bool)$this->conf['update_name'];
  104. $this->cando['modMail'] = (bool)$this->conf['update_mail'];
  105. $this->cando['getUserCount'] = true;
  106. }
  107. /**
  108. * Load domain config on capability check
  109. *
  110. * @param string $cap
  111. * @return bool
  112. */
  113. public function canDo($cap)
  114. {
  115. //capabilities depend on config, which may change depending on domain
  116. $domain = $this->getUserDomain($_SERVER['REMOTE_USER']);
  117. $this->loadServerConfig($domain);
  118. return parent::canDo($cap);
  119. }
  120. /**
  121. * Check user+password [required auth function]
  122. *
  123. * Checks if the given user exists and the given
  124. * plaintext password is correct by trying to bind
  125. * to the LDAP server
  126. *
  127. * @author James Van Lommel <james@nosq.com>
  128. * @param string $user
  129. * @param string $pass
  130. * @return bool
  131. */
  132. public function checkPass($user, $pass)
  133. {
  134. if ($_SERVER['REMOTE_USER'] &&
  135. $_SERVER['REMOTE_USER'] == $user &&
  136. $this->conf['sso']
  137. ) return true;
  138. $adldap = $this->initAdLdap($this->getUserDomain($user));
  139. if (!$adldap) return false;
  140. try {
  141. return $adldap->authenticate($this->getUserName($user), $pass);
  142. } catch (adLDAPException $e) {
  143. // shouldn't really happen
  144. return false;
  145. }
  146. }
  147. /**
  148. * Return user info [required auth function]
  149. *
  150. * Returns info about the given user needs to contain
  151. * at least these fields:
  152. *
  153. * name string full name of the user
  154. * mail string email address of the user
  155. * grps array list of groups the user is in
  156. *
  157. * This AD specific function returns the following
  158. * addional fields:
  159. *
  160. * dn string distinguished name (DN)
  161. * uid string samaccountname
  162. * lastpwd int timestamp of the date when the password was set
  163. * expires true if the password expires
  164. * expiresin int seconds until the password expires
  165. * any fields specified in the 'additional' config option
  166. *
  167. * @author James Van Lommel <james@nosq.com>
  168. * @param string $user
  169. * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin
  170. * @return array
  171. */
  172. public function getUserData($user, $requireGroups = true)
  173. {
  174. global $conf;
  175. global $lang;
  176. global $ID;
  177. $adldap = $this->initAdLdap($this->getUserDomain($user));
  178. if (!$adldap) return array();
  179. if ($user == '') return array();
  180. $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol');
  181. // add additional fields to read
  182. $fields = array_merge($fields, $this->conf['additional']);
  183. $fields = array_unique($fields);
  184. $fields = array_filter($fields);
  185. //get info for given user
  186. $result = $adldap->user()->info($this->getUserName($user), $fields);
  187. if ($result == false) {
  188. return array();
  189. }
  190. //general user info
  191. $info = array();
  192. $info['name'] = $result[0]['displayname'][0];
  193. $info['mail'] = $result[0]['mail'][0];
  194. $info['uid'] = $result[0]['samaccountname'][0];
  195. $info['dn'] = $result[0]['dn'];
  196. //last password set (Windows counts from January 1st 1601)
  197. $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600;
  198. //will it expire?
  199. $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD
  200. // additional information
  201. foreach ($this->conf['additional'] as $field) {
  202. if (isset($result[0][strtolower($field)])) {
  203. $info[$field] = $result[0][strtolower($field)][0];
  204. }
  205. }
  206. // handle ActiveDirectory memberOf
  207. $info['grps'] = $adldap->user()->groups($this->getUserName($user), (bool) $this->opts['recursive_groups']);
  208. if (is_array($info['grps'])) {
  209. foreach ($info['grps'] as $ndx => $group) {
  210. $info['grps'][$ndx] = $this->cleanGroup($group);
  211. }
  212. }
  213. // always add the default group to the list of groups
  214. if (!is_array($info['grps']) || !in_array($conf['defaultgroup'], $info['grps'])) {
  215. $info['grps'][] = $conf['defaultgroup'];
  216. }
  217. // add the user's domain to the groups
  218. $domain = $this->getUserDomain($user);
  219. if ($domain && !in_array("domain-$domain", (array) $info['grps'])) {
  220. $info['grps'][] = $this->cleanGroup("domain-$domain");
  221. }
  222. // check expiry time
  223. if ($info['expires'] && $this->conf['expirywarn']) {
  224. try {
  225. $expiry = $adldap->user()->passwordExpiry($user);
  226. if (is_array($expiry)) {
  227. $info['expiresat'] = $expiry['expiryts'];
  228. $info['expiresin'] = round(($info['expiresat'] - time())/(24*60*60));
  229. // if this is the current user, warn him (once per request only)
  230. if (($_SERVER['REMOTE_USER'] == $user) &&
  231. ($info['expiresin'] <= $this->conf['expirywarn']) &&
  232. !$this->msgshown
  233. ) {
  234. $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']);
  235. if ($this->canDo('modPass')) {
  236. $url = wl($ID, array('do'=> 'profile'));
  237. $msg .= ' <a href="'.$url.'">'.$lang['btn_profile'].'</a>';
  238. }
  239. msg($msg);
  240. $this->msgshown = true;
  241. }
  242. }
  243. } catch (adLDAPException $e) {
  244. // ignore. should usually not happen
  245. }
  246. }
  247. return $info;
  248. }
  249. /**
  250. * Make AD group names usable by DokuWiki.
  251. *
  252. * Removes backslashes ('\'), pound signs ('#'), and converts spaces to underscores.
  253. *
  254. * @author James Van Lommel (jamesvl@gmail.com)
  255. * @param string $group
  256. * @return string
  257. */
  258. public function cleanGroup($group)
  259. {
  260. $group = str_replace('\\', '', $group);
  261. $group = str_replace('#', '', $group);
  262. $group = preg_replace('[\s]', '_', $group);
  263. $group = \dokuwiki\Utf8\PhpString::strtolower(trim($group));
  264. return $group;
  265. }
  266. /**
  267. * Sanitize user names
  268. *
  269. * Normalizes domain parts, does not modify the user name itself (unlike cleanGroup)
  270. *
  271. * @author Andreas Gohr <gohr@cosmocode.de>
  272. * @param string $user
  273. * @return string
  274. */
  275. public function cleanUser($user)
  276. {
  277. $domain = '';
  278. // get NTLM or Kerberos domain part
  279. list($dom, $user) = explode('\\', $user, 2);
  280. if (!$user) $user = $dom;
  281. if ($dom) $domain = $dom;
  282. list($user, $dom) = explode('@', $user, 2);
  283. if ($dom) $domain = $dom;
  284. // clean up both
  285. $domain = \dokuwiki\Utf8\PhpString::strtolower(trim($domain));
  286. $user = \dokuwiki\Utf8\PhpString::strtolower(trim($user));
  287. // is this a known, valid domain or do we work without account suffix? if not discard
  288. if (!is_array($this->conf[$domain]) && $this->conf['account_suffix'] !== '') {
  289. $domain = '';
  290. }
  291. // reattach domain
  292. if ($domain) $user = "$user@$domain";
  293. return $user;
  294. }
  295. /**
  296. * Most values in LDAP are case-insensitive
  297. *
  298. * @return bool
  299. */
  300. public function isCaseSensitive()
  301. {
  302. return false;
  303. }
  304. /**
  305. * Create a Search-String useable by adLDAPUsers::all($includeDescription = false, $search = "*", $sorted = true)
  306. *
  307. * @param array $filter
  308. * @return string
  309. */
  310. protected function constructSearchString($filter)
  311. {
  312. if (!$filter) {
  313. return '*';
  314. }
  315. $adldapUtils = new adLDAPUtils($this->initAdLdap(null));
  316. $result = '*';
  317. if (isset($filter['name'])) {
  318. $result .= ')(displayname=*' . $adldapUtils->ldapSlashes($filter['name']) . '*';
  319. unset($filter['name']);
  320. }
  321. if (isset($filter['user'])) {
  322. $result .= ')(samAccountName=*' . $adldapUtils->ldapSlashes($filter['user']) . '*';
  323. unset($filter['user']);
  324. }
  325. if (isset($filter['mail'])) {
  326. $result .= ')(mail=*' . $adldapUtils->ldapSlashes($filter['mail']) . '*';
  327. unset($filter['mail']);
  328. }
  329. return $result;
  330. }
  331. /**
  332. * Return a count of the number of user which meet $filter criteria
  333. *
  334. * @param array $filter $filter array of field/pattern pairs, empty array for no filter
  335. * @return int number of users
  336. */
  337. public function getUserCount($filter = array())
  338. {
  339. $adldap = $this->initAdLdap(null);
  340. if (!$adldap) {
  341. dbglog("authad/auth.php getUserCount(): _adldap not set.");
  342. return -1;
  343. }
  344. if ($filter == array()) {
  345. $result = $adldap->user()->all();
  346. } else {
  347. $searchString = $this->constructSearchString($filter);
  348. $result = $adldap->user()->all(false, $searchString);
  349. if (isset($filter['grps'])) {
  350. $this->users = array_fill_keys($result, false);
  351. /** @var admin_plugin_usermanager $usermanager */
  352. $usermanager = plugin_load("admin", "usermanager", false);
  353. $usermanager->setLastdisabled(true);
  354. if (!isset($this->grpsusers[$this->filterToString($filter)])) {
  355. $this->fillGroupUserArray($filter, $usermanager->getStart() + 3*$usermanager->getPagesize());
  356. } elseif (count($this->grpsusers[$this->filterToString($filter)]) <
  357. $usermanager->getStart() + 3*$usermanager->getPagesize()
  358. ) {
  359. $this->fillGroupUserArray(
  360. $filter,
  361. $usermanager->getStart() +
  362. 3*$usermanager->getPagesize() -
  363. count($this->grpsusers[$this->filterToString($filter)])
  364. );
  365. }
  366. $result = $this->grpsusers[$this->filterToString($filter)];
  367. } else {
  368. /** @var admin_plugin_usermanager $usermanager */
  369. $usermanager = plugin_load("admin", "usermanager", false);
  370. $usermanager->setLastdisabled(false);
  371. }
  372. }
  373. if (!$result) {
  374. return 0;
  375. }
  376. return count($result);
  377. }
  378. /**
  379. *
  380. * create a unique string for each filter used with a group
  381. *
  382. * @param array $filter
  383. * @return string
  384. */
  385. protected function filterToString($filter)
  386. {
  387. $result = '';
  388. if (isset($filter['user'])) {
  389. $result .= 'user-' . $filter['user'];
  390. }
  391. if (isset($filter['name'])) {
  392. $result .= 'name-' . $filter['name'];
  393. }
  394. if (isset($filter['mail'])) {
  395. $result .= 'mail-' . $filter['mail'];
  396. }
  397. if (isset($filter['grps'])) {
  398. $result .= 'grps-' . $filter['grps'];
  399. }
  400. return $result;
  401. }
  402. /**
  403. * Create an array of $numberOfAdds users passing a certain $filter, including belonging
  404. * to a certain group and save them to a object-wide array. If the array
  405. * already exists try to add $numberOfAdds further users to it.
  406. *
  407. * @param array $filter
  408. * @param int $numberOfAdds additional number of users requested
  409. * @return int number of Users actually add to Array
  410. */
  411. protected function fillGroupUserArray($filter, $numberOfAdds)
  412. {
  413. if (isset($this->grpsusers[$this->filterToString($filter)])) {
  414. $actualstart = count($this->grpsusers[$this->filterToString($filter)]);
  415. } else {
  416. $this->grpsusers[$this->filterToString($filter)] = [];
  417. $actualstart = 0;
  418. }
  419. $i=0;
  420. $count = 0;
  421. $this->constructPattern($filter);
  422. foreach ($this->users as $user => &$info) {
  423. if ($i++ < $actualstart) {
  424. continue;
  425. }
  426. if ($info === false) {
  427. $info = $this->getUserData($user);
  428. }
  429. if ($this->filter($user, $info)) {
  430. $this->grpsusers[$this->filterToString($filter)][$user] = $info;
  431. if (($numberOfAdds > 0) && (++$count >= $numberOfAdds)) break;
  432. }
  433. }
  434. return $count;
  435. }
  436. /**
  437. * Bulk retrieval of user data
  438. *
  439. * @author Dominik Eckelmann <dokuwiki@cosmocode.de>
  440. *
  441. * @param int $start index of first user to be returned
  442. * @param int $limit max number of users to be returned
  443. * @param array $filter array of field/pattern pairs, null for no filter
  444. * @return array userinfo (refer getUserData for internal userinfo details)
  445. */
  446. public function retrieveUsers($start = 0, $limit = 0, $filter = array())
  447. {
  448. $adldap = $this->initAdLdap(null);
  449. if (!$adldap) return array();
  450. //if (!$this->users) {
  451. //get info for given user
  452. $result = $adldap->user()->all(false, $this->constructSearchString($filter));
  453. if (!$result) return array();
  454. $this->users = array_fill_keys($result, false);
  455. //}
  456. $i = 0;
  457. $count = 0;
  458. $result = array();
  459. if (!isset($filter['grps'])) {
  460. /** @var admin_plugin_usermanager $usermanager */
  461. $usermanager = plugin_load("admin", "usermanager", false);
  462. $usermanager->setLastdisabled(false);
  463. $this->constructPattern($filter);
  464. foreach ($this->users as $user => &$info) {
  465. if ($i++ < $start) {
  466. continue;
  467. }
  468. if ($info === false) {
  469. $info = $this->getUserData($user);
  470. }
  471. $result[$user] = $info;
  472. if (($limit > 0) && (++$count >= $limit)) break;
  473. }
  474. } else {
  475. /** @var admin_plugin_usermanager $usermanager */
  476. $usermanager = plugin_load("admin", "usermanager", false);
  477. $usermanager->setLastdisabled(true);
  478. if (!isset($this->grpsusers[$this->filterToString($filter)]) ||
  479. count($this->grpsusers[$this->filterToString($filter)]) < ($start+$limit)
  480. ) {
  481. if(!isset($this->grpsusers[$this->filterToString($filter)])) {
  482. $this->grpsusers[$this->filterToString($filter)] = [];
  483. }
  484. $this->fillGroupUserArray(
  485. $filter,
  486. $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1
  487. );
  488. }
  489. if (!$this->grpsusers[$this->filterToString($filter)]) return array();
  490. foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) {
  491. if ($i++ < $start) {
  492. continue;
  493. }
  494. $result[$user] = $info;
  495. if (($limit > 0) && (++$count >= $limit)) break;
  496. }
  497. }
  498. return $result;
  499. }
  500. /**
  501. * Modify user data
  502. *
  503. * @param string $user nick of the user to be changed
  504. * @param array $changes array of field/value pairs to be changed
  505. * @return bool
  506. */
  507. public function modifyUser($user, $changes)
  508. {
  509. $return = true;
  510. $adldap = $this->initAdLdap($this->getUserDomain($user));
  511. if (!$adldap) {
  512. msg($this->getLang('connectfail'), -1);
  513. return false;
  514. }
  515. // password changing
  516. if (isset($changes['pass'])) {
  517. try {
  518. $return = $adldap->user()->password($this->getUserName($user), $changes['pass']);
  519. } catch (adLDAPException $e) {
  520. if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
  521. $return = false;
  522. }
  523. if (!$return) msg($this->getLang('passchangefail'), -1);
  524. }
  525. // changing user data
  526. $adchanges = array();
  527. if (isset($changes['name'])) {
  528. // get first and last name
  529. $parts = explode(' ', $changes['name']);
  530. $adchanges['surname'] = array_pop($parts);
  531. $adchanges['firstname'] = join(' ', $parts);
  532. $adchanges['display_name'] = $changes['name'];
  533. }
  534. if (isset($changes['mail'])) {
  535. $adchanges['email'] = $changes['mail'];
  536. }
  537. if (count($adchanges)) {
  538. try {
  539. $return = $return & $adldap->user()->modify($this->getUserName($user), $adchanges);
  540. } catch (adLDAPException $e) {
  541. if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1);
  542. $return = false;
  543. }
  544. if (!$return) msg($this->getLang('userchangefail'), -1);
  545. }
  546. return $return;
  547. }
  548. /**
  549. * Initialize the AdLDAP library and connect to the server
  550. *
  551. * When you pass null as domain, it will reuse any existing domain.
  552. * Eg. the one of the logged in user. It falls back to the default
  553. * domain if no current one is available.
  554. *
  555. * @param string|null $domain The AD domain to use
  556. * @return adLDAP|bool true if a connection was established
  557. */
  558. protected function initAdLdap($domain)
  559. {
  560. if (is_null($domain) && is_array($this->opts)) {
  561. $domain = $this->opts['domain'];
  562. }
  563. $this->opts = $this->loadServerConfig((string) $domain);
  564. if (isset($this->adldap[$domain])) return $this->adldap[$domain];
  565. // connect
  566. try {
  567. $this->adldap[$domain] = new adLDAP($this->opts);
  568. return $this->adldap[$domain];
  569. } catch (Exception $e) {
  570. if ($this->conf['debug']) {
  571. msg('AD Auth: '.$e->getMessage(), -1);
  572. }
  573. $this->success = false;
  574. $this->adldap[$domain] = null;
  575. }
  576. return false;
  577. }
  578. /**
  579. * Get the domain part from a user
  580. *
  581. * @param string $user
  582. * @return string
  583. */
  584. public function getUserDomain($user)
  585. {
  586. list(, $domain) = explode('@', $user, 2);
  587. return $domain;
  588. }
  589. /**
  590. * Get the user part from a user
  591. *
  592. * When an account suffix is set, we strip the domain part from the user
  593. *
  594. * @param string $user
  595. * @return string
  596. */
  597. public function getUserName($user)
  598. {
  599. if ($this->conf['account_suffix'] !== '') {
  600. list($user) = explode('@', $user, 2);
  601. }
  602. return $user;
  603. }
  604. /**
  605. * Fetch the configuration for the given AD domain
  606. *
  607. * @param string $domain current AD domain
  608. * @return array
  609. */
  610. protected function loadServerConfig($domain)
  611. {
  612. // prepare adLDAP standard configuration
  613. $opts = $this->conf;
  614. $opts['domain'] = $domain;
  615. // add possible domain specific configuration
  616. if ($domain && is_array($this->conf[$domain])) foreach ($this->conf[$domain] as $key => $val) {
  617. $opts[$key] = $val;
  618. }
  619. // handle multiple AD servers
  620. $opts['domain_controllers'] = explode(',', $opts['domain_controllers']);
  621. $opts['domain_controllers'] = array_map('trim', $opts['domain_controllers']);
  622. $opts['domain_controllers'] = array_filter($opts['domain_controllers']);
  623. // compatibility with old option name
  624. if (empty($opts['admin_username']) && !empty($opts['ad_username'])) {
  625. $opts['admin_username'] = $opts['ad_username'];
  626. }
  627. if (empty($opts['admin_password']) && !empty($opts['ad_password'])) {
  628. $opts['admin_password'] = $opts['ad_password'];
  629. }
  630. $opts['admin_password'] = conf_decodeString($opts['admin_password']); // deobfuscate
  631. // we can change the password if SSL is set
  632. if ($opts['use_ssl'] || $opts['use_tls']) {
  633. $this->cando['modPass'] = true;
  634. } else {
  635. $this->cando['modPass'] = false;
  636. }
  637. // adLDAP expects empty user/pass as NULL, we're less strict FS#2781
  638. if (empty($opts['admin_username'])) $opts['admin_username'] = null;
  639. if (empty($opts['admin_password'])) $opts['admin_password'] = null;
  640. // user listing needs admin priviledges
  641. if (!empty($opts['admin_username']) && !empty($opts['admin_password'])) {
  642. $this->cando['getUsers'] = true;
  643. } else {
  644. $this->cando['getUsers'] = false;
  645. }
  646. return $opts;
  647. }
  648. /**
  649. * Returns a list of configured domains
  650. *
  651. * The default domain has an empty string as key
  652. *
  653. * @return array associative array(key => domain)
  654. */
  655. public function getConfiguredDomains()
  656. {
  657. $domains = array();
  658. if (empty($this->conf['account_suffix'])) return $domains; // not configured yet
  659. // add default domain, using the name from account suffix
  660. $domains[''] = ltrim($this->conf['account_suffix'], '@');
  661. // find additional domains
  662. foreach ($this->conf as $key => $val) {
  663. if (is_array($val) && isset($val['account_suffix'])) {
  664. $domains[$key] = ltrim($val['account_suffix'], '@');
  665. }
  666. }
  667. ksort($domains);
  668. return $domains;
  669. }
  670. /**
  671. * Check provided user and userinfo for matching patterns
  672. *
  673. * The patterns are set up with $this->_constructPattern()
  674. *
  675. * @author Chris Smith <chris@jalakai.co.uk>
  676. *
  677. * @param string $user
  678. * @param array $info
  679. * @return bool
  680. */
  681. protected function filter($user, $info)
  682. {
  683. foreach ($this->pattern as $item => $pattern) {
  684. if ($item == 'user') {
  685. if (!preg_match($pattern, $user)) return false;
  686. } elseif ($item == 'grps') {
  687. if (!count(preg_grep($pattern, $info['grps']))) return false;
  688. } else {
  689. if (!preg_match($pattern, $info[$item])) return false;
  690. }
  691. }
  692. return true;
  693. }
  694. /**
  695. * Create a pattern for $this->_filter()
  696. *
  697. * @author Chris Smith <chris@jalakai.co.uk>
  698. *
  699. * @param array $filter
  700. */
  701. protected function constructPattern($filter)
  702. {
  703. $this->pattern = array();
  704. foreach ($filter as $item => $pattern) {
  705. $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters
  706. }
  707. }
  708. }