Zend_Ldap-Introduction.xml 15 KB

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