CanonTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Ldap
  24. */
  25. require_once 'Zend/Ldap.php';
  26. /* Note: The ldap_connect function does not actually try to connect. This
  27. * is why many tests attempt to bind with invalid credentials. If the
  28. * bind returns 'Invalid credentials' we know the transport related work
  29. * was successful.
  30. */
  31. /**
  32. * @category Zend
  33. * @package Zend_Ldap
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Ldap
  38. */
  39. class Zend_Ldap_CanonTest extends PHPUnit_Framework_TestCase
  40. {
  41. protected $_options = null;
  42. public function setUp()
  43. {
  44. $this->_options = array(
  45. 'host' => TESTS_ZEND_LDAP_HOST,
  46. 'username' => TESTS_ZEND_LDAP_USERNAME,
  47. 'password' => TESTS_ZEND_LDAP_PASSWORD,
  48. 'baseDn' => TESTS_ZEND_LDAP_BASE_DN,
  49. );
  50. if (defined('TESTS_ZEND_LDAP_PORT'))
  51. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  52. if (defined('TESTS_ZEND_LDAP_USE_START_TLS'))
  53. $this->_options['useStartTls'] = TESTS_ZEND_LDAP_USE_START_TLS;
  54. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  55. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  56. if (defined('TESTS_ZEND_LDAP_BIND_REQUIRES_DN'))
  57. $this->_options['bindRequiresDn'] = TESTS_ZEND_LDAP_BIND_REQUIRES_DN;
  58. if (defined('TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT'))
  59. $this->_options['accountFilterFormat'] = TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT;
  60. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME'))
  61. $this->_options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  62. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT'))
  63. $this->_options['accountDomainNameShort'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT;
  64. }
  65. public function testPlainCanon()
  66. {
  67. $ldap = new Zend_Ldap($this->_options);
  68. /* This test tries to canonicalize each name (uname, uname@example.com,
  69. * EXAMPLE\uname) to each of the 3 forms (username, principal and backslash)
  70. * for a total of canonicalizations.
  71. */
  72. if (defined('TESTS_ZEND_LDAP_ALT_USERNAME')) {
  73. $names[Zend_Ldap::ACCTNAME_FORM_USERNAME] = TESTS_ZEND_LDAP_ALT_USERNAME;
  74. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME')) {
  75. $names[Zend_Ldap::ACCTNAME_FORM_PRINCIPAL] =
  76. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  77. }
  78. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT')) {
  79. $names[Zend_Ldap::ACCTNAME_FORM_BACKSLASH] =
  80. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME;
  81. }
  82. }
  83. foreach ($names as $_form => $name) {
  84. foreach ($names as $form => $_name) {
  85. $ret = $ldap->getCanonicalAccountName($name, $form);
  86. $this->assertEquals($names[$form], $ret);
  87. }
  88. }
  89. }
  90. public function testInvalidAccountCanon()
  91. {
  92. $ldap = new Zend_Ldap($this->_options);
  93. try {
  94. $ldap->bind('invalid', 'invalid');
  95. $this->fail('Expected exception not thrown');
  96. } catch (Zend_Ldap_Exception $zle) {
  97. $msg = $zle->getMessage();
  98. $this->assertTrue(strstr($msg, 'Invalid credentials') ||
  99. strstr($msg, 'No such object') ||
  100. strstr($msg, 'No object found'));
  101. }
  102. }
  103. public function testDnCanon()
  104. {
  105. $ldap = new Zend_Ldap($this->_options);
  106. $name = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME, Zend_Ldap::ACCTNAME_FORM_DN);
  107. $this->assertEquals(TESTS_ZEND_LDAP_ALT_DN, $name);
  108. }
  109. public function testMismatchDomainBind()
  110. {
  111. $ldap = new Zend_Ldap($this->_options);
  112. try {
  113. $ldap->bind('BOGUS\\doesntmatter', 'doesntmatter');
  114. $this->fail('Expected exception not thrown');
  115. } catch (Zend_Ldap_Exception $zle) {
  116. $this->assertTrue($zle->getCode() == Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH);
  117. }
  118. }
  119. public function testAccountCanonization()
  120. {
  121. $options = $this->_options;
  122. $ldap = new Zend_Ldap($options);
  123. $canonDn = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  124. Zend_Ldap::ACCTNAME_FORM_DN);
  125. $this->assertEquals(TESTS_ZEND_LDAP_ALT_DN, $canonDn);
  126. $canonUsername = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  127. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  128. $this->assertEquals(TESTS_ZEND_LDAP_ALT_USERNAME, $canonUsername);
  129. $canonBackslash = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  130. Zend_Ldap::ACCTNAME_FORM_BACKSLASH);
  131. $this->assertEquals(
  132. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME,
  133. $canonBackslash);
  134. $canonPrincipal = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  135. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL);
  136. $this->assertEquals(
  137. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  138. $canonPrincipal);
  139. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_USERNAME;
  140. $ldap->setOptions($options);
  141. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  142. $this->assertEquals(TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  143. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_BACKSLASH;
  144. $ldap->setOptions($options);
  145. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  146. $this->assertEquals(
  147. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  148. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_PRINCIPAL;
  149. $ldap->setOptions($options);
  150. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  151. $this->assertEquals(
  152. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME, $canon);
  153. unset($options['accountCanonicalForm']);
  154. unset($options['accountDomainName']);
  155. $ldap->setOptions($options);
  156. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  157. $this->assertEquals(
  158. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  159. unset($options['accountDomainNameShort']);
  160. $ldap->setOptions($options);
  161. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  162. $this->assertEquals(TESTS_ZEND_LDAP_ALT_USERNAME, $canon);
  163. $options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  164. $ldap->setOptions($options);
  165. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME);
  166. $this->assertEquals(
  167. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME, $canon);
  168. }
  169. public function testDefaultAccountFilterFormat()
  170. {
  171. $options = $this->_options;
  172. unset($options['accountFilterFormat']);
  173. $options['bindRequiresDn'] = true;
  174. $ldap = new Zend_Ldap($options);
  175. try {
  176. $canon = $ldap->getCanonicalAccountName('invalid', Zend_Ldap::ACCTNAME_FORM_DN);
  177. $this->fail('Expected exception not thrown');
  178. } catch (Zend_Ldap_Exception $zle) {
  179. $this->assertContains('(&(objectClass=posixAccount)(uid=invalid))', $zle->getMessage());
  180. }
  181. $options['bindRequiresDn'] = false;
  182. $ldap = new Zend_Ldap($options);
  183. try {
  184. $canon = $ldap->getCanonicalAccountName('invalid', Zend_Ldap::ACCTNAME_FORM_DN);
  185. $this->fail('Expected exception not thrown');
  186. } catch (Zend_Ldap_Exception $zle) {
  187. $this->assertContains('(&(objectClass=user)(sAMAccountName=invalid))', $zle->getMessage());
  188. }
  189. }
  190. public function testPossibleAuthority()
  191. {
  192. $options = $this->_options;
  193. $ldap = new Zend_Ldap($options);
  194. try {
  195. $canon = $ldap->getCanonicalAccountName('invalid\invalid',
  196. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  197. $this->fail('Expected exception not thrown');
  198. } catch (Zend_Ldap_Exception $zle) {
  199. $this->assertContains('Binding domain is not an authority for user: invalid\invalid',
  200. $zle->getMessage());
  201. }
  202. try {
  203. $canon = $ldap->getCanonicalAccountName('invalid@invalid.tld',
  204. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  205. $this->fail('Expected exception not thrown');
  206. } catch (Zend_Ldap_Exception $zle) {
  207. $this->assertContains('Binding domain is not an authority for user: invalid@invalid.tld',
  208. $zle->getMessage());
  209. }
  210. unset($options['accountDomainName']);
  211. $ldap = new Zend_Ldap($options);
  212. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  213. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  214. $this->assertEquals('invalid', $canon);
  215. try {
  216. $canon = $ldap->getCanonicalAccountName('invalid@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  217. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  218. $this->fail('Expected exception not thrown');
  219. } catch (Zend_Ldap_Exception $zle) {
  220. $this->assertContains('Binding domain is not an authority for user: invalid@' .
  221. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  222. $zle->getMessage());
  223. }
  224. unset($options['accountDomainNameShort']);
  225. $options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  226. $ldap = new Zend_Ldap($options);
  227. try {
  228. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  229. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  230. $this->fail('Expected exception not thrown');
  231. } catch (Zend_Ldap_Exception $zle) {
  232. $this->assertContains('Binding domain is not an authority for user: ' .
  233. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  234. $zle->getMessage());
  235. }
  236. $canon = $ldap->getCanonicalAccountName('invalid@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  237. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  238. $this->assertEquals('invalid', $canon);
  239. unset($options['accountDomainName']);
  240. $ldap = new Zend_Ldap($options);
  241. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\invalid',
  242. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  243. $this->assertEquals('invalid', $canon);
  244. $canon = $ldap->getCanonicalAccountName('invalid@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  245. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  246. $this->assertEquals('invalid', $canon);
  247. }
  248. public function testInvalidAccountName()
  249. {
  250. $options = $this->_options;
  251. $ldap = new Zend_Ldap($options);
  252. try {
  253. $canon = $ldap->getCanonicalAccountName('0@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  254. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  255. $this->fail('Expected exception not thrown');
  256. } catch (Zend_Ldap_Exception $zle) {
  257. $this->assertContains('Invalid account name syntax: 0@' .
  258. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME,
  259. $zle->getMessage());
  260. }
  261. try {
  262. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\0',
  263. Zend_Ldap::ACCTNAME_FORM_USERNAME);
  264. $this->fail('Expected exception not thrown');
  265. } catch (Zend_Ldap_Exception $zle) {
  266. $this->assertContains('Invalid account name syntax: ' .
  267. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\0',
  268. $zle->getMessage());
  269. }
  270. }
  271. public function testGetUnknownCanonicalForm()
  272. {
  273. $options = $this->_options;
  274. $ldap = new Zend_Ldap($options);
  275. try {
  276. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME, 99);
  277. $this->fail('Expected exception not thrown');
  278. } catch (Zend_Ldap_Exception $zle) {
  279. $this->assertContains('Unknown canonical name form: 99',
  280. $zle->getMessage());
  281. }
  282. }
  283. public function testGetUnavailableCanoncialForm()
  284. {
  285. $options = $this->_options;
  286. unset($options['accountDomainName']);
  287. $ldap = new Zend_Ldap($options);
  288. try {
  289. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  290. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL);
  291. $this->fail('Expected exception not thrown');
  292. } catch (Zend_Ldap_Exception $zle) {
  293. $this->assertContains('Option required: accountDomainName',
  294. $zle->getMessage());
  295. }
  296. unset($options['accountDomainNameShort']);
  297. $ldap = new Zend_Ldap($options);
  298. try {
  299. $canon = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_USERNAME,
  300. Zend_Ldap::ACCTNAME_FORM_BACKSLASH);
  301. $this->fail('Expected exception not thrown');
  302. } catch (Zend_Ldap_Exception $zle) {
  303. $this->assertContains('Option required: accountDomainNameShort',
  304. $zle->getMessage());
  305. }
  306. }
  307. public function testSplittingOption()
  308. {
  309. $options = $this->_options;
  310. unset($options['accountDomainName']);
  311. unset($options['accountDomainNameShort']);
  312. $options['tryUsernameSplit'] = true;
  313. $ldap = new Zend_Ldap($options);
  314. $this->assertEquals('username', $ldap->getCanonicalAccountName('username@example.com',
  315. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  316. $this->assertEquals('username', $ldap->getCanonicalAccountName('EXAMPLE\username',
  317. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  318. $this->assertEquals('username', $ldap->getCanonicalAccountName('username',
  319. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  320. $options['tryUsernameSplit'] = false;
  321. $ldap = new Zend_Ldap($options);
  322. $this->assertEquals('username@example.com',
  323. $ldap->getCanonicalAccountName('username@example.com', Zend_Ldap::ACCTNAME_FORM_USERNAME));
  324. $this->assertEquals('example\username', $ldap->getCanonicalAccountName('EXAMPLE\username',
  325. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  326. $this->assertEquals('username', $ldap->getCanonicalAccountName('username',
  327. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  328. }
  329. /**
  330. * ZF-4495
  331. */
  332. public function testSpecialCharacterInUsername()
  333. {
  334. $options = $this->_options;
  335. $options['accountDomainName'] = 'example.com';
  336. $options['accountDomainNameShort'] = 'EXAMPLE';
  337. $ldap = new Zend_Ldap($options);
  338. $this->assertEquals('schäfer', $ldap->getCanonicalAccountName('SCHÄFER@example.com',
  339. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  340. $this->assertEquals('schäfer', $ldap->getCanonicalAccountName('EXAMPLE\SCHÄFER',
  341. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  342. $this->assertEquals('schäfer', $ldap->getCanonicalAccountName('SCHÄFER',
  343. Zend_Ldap::ACCTNAME_FORM_USERNAME));
  344. $this->assertEquals('schäfer@example.com', $ldap->getCanonicalAccountName('SCHÄFER@example.com',
  345. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL));
  346. $this->assertEquals('schäfer@example.com', $ldap->getCanonicalAccountName('EXAMPLE\SCHÄFER',
  347. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL));
  348. $this->assertEquals('schäfer@example.com', $ldap->getCanonicalAccountName('SCHÄFER',
  349. Zend_Ldap::ACCTNAME_FORM_PRINCIPAL));
  350. $this->assertEquals('EXAMPLE\schäfer', $ldap->getCanonicalAccountName('SCHÄFER@example.com',
  351. Zend_Ldap::ACCTNAME_FORM_BACKSLASH));
  352. $this->assertEquals('EXAMPLE\schäfer', $ldap->getCanonicalAccountName('EXAMPLE\SCHÄFER',
  353. Zend_Ldap::ACCTNAME_FORM_BACKSLASH));
  354. $this->assertEquals('EXAMPLE\schäfer', $ldap->getCanonicalAccountName('SCHÄFER',
  355. Zend_Ldap::ACCTNAME_FORM_BACKSLASH));
  356. }
  357. }