Zend_Ldap-Introduction.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.ldap.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_Ldap</classname> is a class for performing <acronym>LDAP</acronym>
  7. operations including but not limited to binding, searching and modifying entries
  8. in an <acronym>LDAP</acronym> directory.
  9. </para>
  10. <sect2 id="zend.ldap.introduction.theory-of-operations">
  11. <title>Theory of operation</title>
  12. <para>
  13. This component currently consists of the main <classname>Zend_Ldap</classname> class,
  14. that conceptually represents a binding to a single <acronym>LDAP</acronym> server
  15. and allows for executing operations against a <acronym>LDAP</acronym> server such
  16. as OpenLDAP or ActiveDirectory (AD) servers. The parameters for binding may be
  17. provided explicitly or in the form of an options array.
  18. <classname>Zend_Ldap_Node</classname> provides an object-oriented interface
  19. for single <acronym>LDAP</acronym> nodes and can be used to form a basis for an
  20. active-record-like interface for a <acronym>LDAP</acronym>-based domain model.
  21. </para>
  22. <para>
  23. The component provides several helper classes to perform operations on
  24. <acronym>LDAP</acronym> entries (<classname>Zend_Ldap_Attribute</classname>) such as
  25. setting and retrieving attributes (date values, passwords, boolean values, ...), to
  26. create and modify <acronym>LDAP</acronym> filter strings
  27. (<classname>Zend_Ldap_Filter</classname>) and to manipulate <acronym>LDAP</acronym>
  28. distinguished names (DN) (<classname>Zend_Ldap_Dn</classname>).
  29. </para>
  30. <para>
  31. Additionally the component abstracts <acronym>LDAP</acronym> schema browsing
  32. for OpenLDAP and ActiveDirectoy servers <classname>Zend_Ldap_Node_Schema</classname>
  33. and server information retrieval for OpenLDAP-, ActiveDirectory- and Novell
  34. eDirectory servers (<classname>Zend_Ldap_Node_RootDse</classname>).
  35. </para>
  36. <para>
  37. Using the <classname>Zend_Ldap</classname> class depends on the type of
  38. <acronym>LDAP</acronym> server and is best summarized with some simple examples.
  39. </para>
  40. <para>
  41. If you are using OpenLDAP, a simple example looks like the following
  42. (note that the <emphasis>bindRequiresDn</emphasis> option is important if you are
  43. <emphasis>not</emphasis> using AD):
  44. </para>
  45. <programlisting language="php"><![CDATA[
  46. $options = array(
  47. 'host' => 's0.foo.net',
  48. 'username' => 'CN=user1,DC=foo,DC=net',
  49. 'password' => 'pass1',
  50. 'bindRequiresDn' => true,
  51. 'accountDomainName' => 'foo.net',
  52. 'baseDn' => 'OU=Sales,DC=foo,DC=net',
  53. );
  54. $ldap = new Zend_Ldap($options);
  55. $acctname = $ldap->getCanonicalAccountName('abaker',
  56. Zend_Ldap::ACCTNAME_FORM_DN);
  57. echo "$acctname\n";
  58. ]]></programlisting>
  59. <para>
  60. If you are using Microsoft AD a simple example is:
  61. </para>
  62. <programlisting language="php"><![CDATA[
  63. $options = array(
  64. 'host' => 'dc1.w.net',
  65. 'useStartTls' => true,
  66. 'username' => 'user1@w.net',
  67. 'password' => 'pass1',
  68. 'accountDomainName' => 'w.net',
  69. 'accountDomainNameShort' => 'W',
  70. 'baseDn' => 'CN=Users,DC=w,DC=net',
  71. );
  72. $ldap = new Zend_Ldap($options);
  73. $acctname = $ldap->getCanonicalAccountName('bcarter',
  74. Zend_Ldap::ACCTNAME_FORM_DN);
  75. echo "$acctname\n";
  76. ]]></programlisting>
  77. <para>
  78. Note that we use the <methodname>getCanonicalAccountName()</methodname> method
  79. to retrieve the account DN here only because that is what exercises the
  80. most of what little code is currently present in this class.
  81. </para>
  82. <sect3 id="zend.ldap.introduction.theory-of-operations.automatic-username-canonicalization">
  83. <title>Automatic Username Canonicalization When Binding</title>
  84. <para>
  85. If <methodname>bind()</methodname> is called with a non-DN username but
  86. <emphasis>bindRequiresDN</emphasis> is <constant>TRUE</constant> and no username in
  87. DN form was supplied as an option, the bind will fail. However, if a username in DN
  88. form is supplied in the options array, <classname>Zend_Ldap</classname> will
  89. first bind with that username, retrieve the account DN for the username
  90. supplied to <methodname>bind()</methodname> and then re-bind with that DN.
  91. </para>
  92. <para>
  93. This behavior is critical to <link
  94. linkend="zend.auth.adapter.ldap"><classname>Zend_Auth_Adapter_Ldap</classname></link>,
  95. which passes the username supplied by the user directly to
  96. <methodname>bind()</methodname>.
  97. </para>
  98. <para>
  99. The following example illustrates how the non-DN username
  100. '<emphasis>abaker</emphasis>' can be used with <methodname>bind()</methodname>:
  101. </para>
  102. <programlisting language="php"><![CDATA[
  103. $options = array(
  104. 'host' => 's0.foo.net',
  105. 'username' => 'CN=user1,DC=foo,DC=net',
  106. 'password' => 'pass1',
  107. 'bindRequiresDn' => true,
  108. 'accountDomainName' => 'foo.net',
  109. 'baseDn' => 'OU=Sales,DC=foo,DC=net',
  110. );
  111. $ldap = new Zend_Ldap($options);
  112. $ldap->bind('abaker', 'moonbike55');
  113. $acctname = $ldap->getCanonicalAccountName('abaker',
  114. Zend_Ldap::ACCTNAME_FORM_DN);
  115. echo "$acctname\n";
  116. ]]></programlisting>
  117. <para>
  118. The <methodname>bind()</methodname> call in this example sees that
  119. the username '<emphasis>abaker</emphasis>' is not in DN form, finds
  120. <emphasis>bindRequiresDn</emphasis> is <constant>TRUE</constant>, uses
  121. '<command>CN=user1,DC=foo,DC=net</command>' and '<emphasis>pass1</emphasis>' to
  122. bind, retrieves the DN for '<emphasis>abaker</emphasis>', unbinds and then rebinds
  123. with the newly discovered
  124. '<command>CN=Alice Baker,OU=Sales,DC=foo,DC=net</command>'.
  125. </para>
  126. </sect3>
  127. <sect3 id="zend.ldap.introduction.theory-of-operations.account-name-canonicalization">
  128. <title>Account Name Canonicalization</title>
  129. <para>
  130. The <emphasis>accountDomainName</emphasis> and
  131. <emphasis>accountDomainNameShort</emphasis>
  132. options are used for two purposes: (1) they facilitate multi-domain
  133. authentication and failover capability, and (2) they are also used to
  134. canonicalize usernames. Specifically, names are canonicalized to the
  135. form specified by the <emphasis>accountCanonicalForm</emphasis> option.
  136. This option may one of the following values:
  137. </para>
  138. <table id="zend.ldap.using.theory-of-operation.account-name-canonicalization.table">
  139. <title>Options for accountCanonicalForm</title>
  140. <tgroup cols="3">
  141. <thead>
  142. <row>
  143. <entry>Name</entry>
  144. <entry>Value</entry>
  145. <entry>Example</entry>
  146. </row>
  147. </thead>
  148. <tbody>
  149. <row>
  150. <entry><constant>ACCTNAME_FORM_DN</constant></entry>
  151. <entry>1</entry>
  152. <entry>CN=Alice Baker,CN=Users,DC=example,DC=com</entry>
  153. </row>
  154. <row>
  155. <entry><constant>ACCTNAME_FORM_USERNAME</constant></entry>
  156. <entry>2</entry>
  157. <entry>abaker</entry>
  158. </row>
  159. <row>
  160. <entry><constant>ACCTNAME_FORM_BACKSLASH</constant></entry>
  161. <entry>3</entry>
  162. <entry>EXAMPLE\abaker</entry>
  163. </row>
  164. <row>
  165. <entry><constant>ACCTNAME_FORM_PRINCIPAL</constant></entry>
  166. <entry>4</entry>
  167. <entry><filename>abaker@example.com</filename></entry>
  168. </row>
  169. </tbody>
  170. </tgroup>
  171. </table>
  172. <para>
  173. The default canonicalization depends on what account domain name options
  174. were supplied. If <emphasis>accountDomainNameShort</emphasis> was supplied, the
  175. default <emphasis>accountCanonicalForm</emphasis> value is
  176. <constant>ACCTNAME_FORM_BACKSLASH</constant>. Otherwise, if
  177. <emphasis>accountDomainName</emphasis> was supplied, the
  178. default is <constant>ACCTNAME_FORM_PRINCIPAL</constant>.
  179. </para>
  180. <para>
  181. Account name canonicalization ensures that the string used to identify
  182. an account is consistent regardless of what was supplied to
  183. <methodname>bind()</methodname>. For example, if the user supplies an account
  184. name of <filename>abaker@example.com</filename> or just
  185. <emphasis>abaker</emphasis> and the <emphasis>accountCanonicalForm</emphasis>
  186. is set to 3, the resulting canonicalized name would be
  187. <emphasis>EXAMPLE\abaker</emphasis>.
  188. </para>
  189. </sect3>
  190. <sect3 id="zend.ldap.introduction.theory-of-operations.multi-domain-failover">
  191. <title>Multi-domain Authentication and Failover</title>
  192. <para>
  193. The <classname>Zend_Ldap</classname> component by itself makes no attempt
  194. to authenticate with multiple servers. However, <classname>Zend_Ldap</classname>
  195. is specifically designed to handle this scenario gracefully. The
  196. required technique is to simply iterate over an array of arrays of serve
  197. options and attempt to bind with each server. As described above
  198. <methodname>bind()</methodname> will automatically canonicalize each name, so
  199. it does not matter if the user passes <filename>abaker@foo.net</filename> or
  200. <emphasis>W\bcarter</emphasis> or <emphasis>cdavis</emphasis> - the
  201. <methodname>bind()</methodname> method will only succeed if the credentials were
  202. successfully used in the bind.
  203. </para>
  204. <para>
  205. Consider the following example that illustrates the technique required to
  206. implement multi-domain authentication and failover:
  207. </para>
  208. <programlisting language="php"><![CDATA[
  209. $acctname = 'W\\user2';
  210. $password = 'pass2';
  211. $multiOptions = array(
  212. 'server1' => array(
  213. 'host' => 's0.foo.net',
  214. 'username' => 'CN=user1,DC=foo,DC=net',
  215. 'password' => 'pass1',
  216. 'bindRequiresDn' => true,
  217. 'accountDomainName' => 'foo.net',
  218. 'accountDomainNameShort' => 'FOO',
  219. 'accountCanonicalForm' => 4, // ACCT_FORM_PRINCIPAL
  220. 'baseDn' => 'OU=Sales,DC=foo,DC=net',
  221. ),
  222. 'server2' => array(
  223. 'host' => 'dc1.w.net',
  224. 'useSsl' => true,
  225. 'username' => 'user1@w.net',
  226. 'password' => 'pass1',
  227. 'accountDomainName' => 'w.net',
  228. 'accountDomainNameShort' => 'W',
  229. 'accountCanonicalForm' => 4, // ACCT_FORM_PRINCIPAL
  230. 'baseDn' => 'CN=Users,DC=w,DC=net',
  231. ),
  232. );
  233. $ldap = new Zend_Ldap();
  234. foreach ($multiOptions as $name => $options) {
  235. echo "Trying to bind using server options for '$name'\n";
  236. $ldap->setOptions($options);
  237. try {
  238. $ldap->bind($acctname, $password);
  239. $acctname = $ldap->getCanonicalAccountName($acctname);
  240. echo "SUCCESS: authenticated $acctname\n";
  241. return;
  242. } catch (Zend_Ldap_Exception $zle) {
  243. echo ' ' . $zle->getMessage() . "\n";
  244. if ($zle->getCode() === Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH) {
  245. continue;
  246. }
  247. }
  248. }
  249. ]]></programlisting>
  250. <para>
  251. If the bind fails for any reason, the next set of server options is tried.
  252. </para>
  253. <para>
  254. The <methodname>getCanonicalAccountName()</methodname> call gets the canonical
  255. account name that the application would presumably use to associate data with such
  256. as preferences. The <emphasis>accountCanonicalForm = 4</emphasis> in all server
  257. options ensures that the canonical form is consistent regardless of which
  258. server was ultimately used.
  259. </para>
  260. <para>
  261. The special <constant>LDAP_X_DOMAIN_MISMATCH</constant> exception occurs when an
  262. account name with a domain component was supplied (e.g.,
  263. <filename>abaker@foo.net</filename> or <emphasis>FOO\abaker</emphasis> and not just
  264. <emphasis>abaker</emphasis>) but the domain component did not match either domain
  265. in the currently selected server options. This exception indicates
  266. that the server is not an authority for the account. In this
  267. case, the bind will not be performed, thereby eliminating unnecessary
  268. communication with the server. Note that the <emphasis>continue</emphasis>
  269. instruction has no effect in this example, but in practice for error handling and
  270. debugging purposes, you will probably want to check for
  271. <constant>LDAP_X_DOMAIN_MISMATCH</constant> as well as
  272. <constant>LDAP_NO_SUCH_OBJECT</constant> and
  273. <constant>LDAP_INVALID_CREDENTIALS</constant>.
  274. </para>
  275. <para>
  276. The above code is very similar to code used within <link
  277. linkend="zend.auth.adapter.ldap"><classname>Zend_Auth_Adapter_Ldap</classname></link>.
  278. In fact, we recommend that you simply use that authentication adapter for
  279. multi-domain + failover <acronym>LDAP</acronym> based authentication
  280. (or copy the code).
  281. </para>
  282. </sect3>
  283. </sect2>
  284. </sect1>