CanonTest.php 17 KB

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