<commit_before><sect1 id="zend.ldap">

    <title>Zend_Ldap</title>

</sect1>
<!--
vim:se ts=4 sw=4 et:
--><commit_msg>Zend_Ldap * initial commit of Zend_Ldap documentation<commit_after><sect1 id="zend.ldap">

    <title>Zend_Ldap</title>

    <note>
        <title>Minimal Functionality</title>
        <para>
            Currently this class is designed only to satisfy the limited functionality necessary for the
            <link linkend="zend.auth.adapter.ldap"><code>Zend_Auth_Adapter_Ldap</code></link> authentication adapter.
            Operations such as searching, creating, modifying or renaming entries in the directory are currently not
            supported and will be defined at a later time.
        </para>
    </note>

    <para>
        Zend_Ldap is a class for performing LDAP operations including but not limited to binding, searching and
        modifying entries in an LDAP directory.
    </para>

    <sect2 id="zend.ldap.theory-of-operation">

        <title>Theory of Operation</title>

        <para>
            This component currently consists of two classes, <code>Zend_Ldap</code> and
            <code>Zend_Ldap_Exception</code>. The <code>Zend_Ldap</code> class conceptually represents a binding to a
            single LDAP server. The parameters for binding may be provided explicitly or in the form of an options
            array.
        </para>

        <para>
            Using the <code>Zend_Ldap</code> class depends on the type of LDAP server and is best summarized with some
            simple examples.
        </para>

        <para>
            If you are using OpenLDAP, a simple example looks like the following (note that the
            <code>bindRequiresDn</code> option is important if you are <emphasis>not</emphasis> using AD):

            <programlisting role="php"><![CDATA[<?php
require_once 'Zend/Ldap.php';

$options = array(
    'host' => 's0.foo.net',
    'username' => 'CN=user1,DC=foo,DC=net',
    'password' => '<PASSWORD>',
    'bindRequiresDn' => true,
    'accountDomainName' => 'foo.net',
    'baseDn' => 'OU=Sales,DC=foo,DC=net',
);
$ldap = new Zend_Ldap($options);
$acctname = $ldap->getCanonicalAccountName('abaker', Zend_Ldap::ACCTNAME_FORM_DN);
echo "$acctname\n";]]></programlisting>

        </para>

        <para>
            If you are using Microsoft AD a simple example is:

            <programlisting role="php"><![CDATA[<?php
require_once 'Zend/Ldap.php';

$options = array(
    'host' => 'dc1.w.net',
    'useSsl' => true,
    'username' => '<EMAIL>',
    'password' => '<PASSWORD>',
    'accountDomainName' => 'w.net',
    'accountDomainNameShort' => 'W',
    'baseDn' => 'CN=Users,DC=w,DC=net',
);
$ldap = new Zend_Ldap($options);
$acctname = $ldap->getCanonicalAccountName('bcarter', Zend_Ldap::ACCTNAME_FORM_DN);
echo "$acctname\n";]]></programlisting>

            Note that we use the <code>getCanonicalAccountName()</code> method to retrieve the account DN here only
            because that is what exercises the most of what little code is currently present in this class.
        </para>

        <sect3 id="zend.ldap.theory-of-operation.username-canonicalization-automatic">

            <title>Automatic Username Canonicalization When Binding</title>

            <para>
                If <code>bind()</code> is called with a non-DN username but <code>bindRequiresDN</code> is
                <code>true</code> and no username in DN form was supplied as an option, the bind will fail. However, if
                a username in DN form is supplied in the options array, <code>Zend_Ldap</code> will first bind with
                that username, retrieve the account DN for the username supplied to <code>bind()</code> and then re-
                bind with that DN.
            </para>

            <para>
                This behavior is critical to <code>Zend_Auth_Adapter_Ldap</code>, which passes the username supplied by
                the user directly to <code>bind()</code>.
            </para>

            <para>
                The following example illustrates how the non-DN username '<code>abaker</code>' can be used with
                <code>bind()</code>:

                <programlisting role="php"><![CDATA[<?php
require_once 'Zend/Ldap.php';

$options = array(
        'host' => 's0.foo.net',
        'username' => 'CN=user1,DC=foo,DC=net',
        'password' => '<PASSWORD>',
        'bindRequiresDn' => true,
        'accountDomainName' => 'foo.net',
        'baseDn' => 'OU=Sales,DC=foo,DC=net',
);
$ldap = new Zend_Ldap($options);
$ldap->bind('abaker', 'moonbike55');
$acctname = $ldap->getCanonicalAccountName('abaker', Zend_Ldap::ACCTNAME_FORM_DN);
echo "$acctname\n";]]></programlisting>

                The <code>bind()</code> call in this example sees that the username '<code>abaker</code>' is not in DN
                form, finds <code>bindRequiresDn</code> is <code>true</code>, uses
                '<code>CN=user1,DC=foo,DC=net</code>' and '<code>pass1</code>' to bind, retrieves the DN for
                '<code>abaker</code>', unbinds and then rebinds with the newly discovered
                '<code>CN=<NAME>,OU=Sales,DC=foo,DC=net</code>'.
            </para>

        </sect3>

        <sect3 id="zend.ldap.theory-of-operation.options">

            <title>Zend_Ldap Options</title>

            <para>
                The <code>Zend_Ldap</code> component accepts an array of options either supplied to the constructor or
                through the <code>setOptions()</code> method. The permitted options are as follows:

                <table id="zend.ldap.theory-of-operation.options.table">
                  <title>Zend_Ldap Options</title>
                  <tgroup cols="2">
                    <thead>
                      <row>
                        <entry>Name</entry>
                        <entry>Description</entry>
                      </row>
                    </thead>
                    <tbody>
                      <row>
                        <entry>host</entry>
                        <entry>
                            The default hostname of LDAP server if not supplied to <code>connect()</code> (also may be
                            used when trying to canonicalize usernames in <code>bind()</code>).
                        </entry>
                      </row>
                      <row>
                        <entry>port</entry>
                        <entry>
                            Default port of LDAP server if not supplied to <code>connect()</code>.
                        </entry>
                      </row>
                      <row>
                        <entry>useSsl</entry>
                        <entry>
                            Whether or not the LDAP client should use SSL / TLS encrypted transport. A value of
                            <code>true</code> is strongly favored in production environments to prevent passwords from
                            be transmitted in clear text. The default value is <code>false</code>, as servers
                            frequently require that a certificate be installed separately after installation.
                        </entry>
                      </row>
                      <row>
                        <entry>username</entry>
                        <entry>
                            The default credentials username. Some servers require that this be in DN form.
                        </entry>
                      </row>
                      <row>
                        <entry>password</entry>
                        <entry>
                            The default credentials password (used only with username above).
                        </entry>
                      </row>
                      <row>
                        <entry>bindRequiresDn</entry>
                        <entry>
                            If <code>true</code>, this instructs <code>Zend_Ldap</code> to retrieve the DN for the
                            account used to bind if the username is not already in DN form. The default value is
                            <code>false</code>.
                        </entry>
                      </row>
                      <row>
                        <entry>baseDn</entry>
                        <entry>
                            The default base DN used for searching (e.g., for accounts). This option is required for
                            most account related operations and should indicate the DN under which accounts are
                            located.
                        </entry>
                      </row>
                      <row>
                        <entry>accountCanonicalForm</entry>
                        <entry>
                            A small integer indicating the form to which account names should be canonicalized. See the
                            <emphasis>Account Name Canonicalization</emphasis> section below.
                        </entry>
                      </row>
                      <row>
                        <entry>accountDomainName</entry>
                        <entry>
                            The FQDN domain for which the target LDAP server is an authority (e.g., example.com).
                        </entry>
                      </row>
                      <row>
                        <entry>accountDomainNameShort</entry>
                        <entry>
                            The 'short' domain for which the target LDAP server is an authority. This is usually used
                            to specify the NetBIOS domain name for Windows networks but may also be used by non-AD
                            servers.
                        </entry>
                      </row>
                      <row>
                        <entry>accountFilterFormat</entry>
                        <entry>
                            The LDAP search filter used to search for accounts. This string is a
                            <ulink url="http://php.net/printf"><code>printf()</code></ulink> style expression that must
                            contain one '<code>%s</code>' to accomodate the username. The default value is
                            '<code>(&amp;(objectClass=user)(sAMAccountName=%s))</code>' unless
                            <code>bindRequiresDn</code> is set to <code>true</code>, in which case the default is
                            '<code>(&amp;(objectClass=posixAccount)(uid=%s))</code>'. Users of custom schemas may need
                            to change this option.
                        </entry>
                      </row>
                    </tbody>
                  </tgroup>
                </table>

            </para>

        </sect3>

        <sect3 id="zend.ldap.theory-of-operation.account-name-canonicalization">

            <title>Account Name Canonicalization</title>

            <para>
                The <code>accountDomainName</code> and <code>accountDomainNameShort</code> options are used for two
                purposes: (1) they facilitate multi-domain authentication and failover capability, and (2) they are
                also used to canonicalize usernames. Specifically, names are canonicalized to the form specified by the
                <code>accountCanonicalForm</code> option. This option may one of the following values:

                <table id="zend.ldap.theory-of-operation.account-name-canonicalization.table">
                  <title><code>accountCanonicalForm</code></title>
                  <tgroup cols="3">
                    <thead>
                      <row>
                        <entry>Name</entry>
                        <entry>Value</entry>
                        <entry>Description</entry>
                      </row>
                    </thead>
                    <tbody>
                      <row>
                        <entry><code>ACCTNAME_FORM_DN</code></entry>
                        <entry>1</entry>
                        <entry>CN=<NAME>,CN=Users,DC=example,DC=com</entry>
                      </row>
                      <row>
                        <entry><code>ACCTNAME_FORM_USERNAME</code></entry>
                        <entry>2</entry>
                        <entry>abaker</entry>
                      </row>
                      <row>
                        <entry><code>ACCTNAME_FORM_BACKSLASH</code></entry>
                        <entry>3</entry>
                        <entry>EXAMPLE\abaker</entry>
                      </row>
                      <row>
                        <entry><code>ACCTNAME_FORM_PRINCIPAL</code></entry>
                        <entry>4</entry>
                        <entry><EMAIL></entry>
                      </row>
                    </tbody>
                  </tgroup>
                </table>

            </para>

            <para>
                The default canonicalization depends on what account domain name options were supplied. If
                <code>accountDomainNameShort</code> was supplied, the default <code>accountCanonicalForm</code> value
                is <code>ACCTNAME_FORM_BACKSLASH</code>. Otherwise, if <code>accountDomainName</code> was supplied, the
                default is <code>ACCTNAME_FORM_PRINCIPAL</code>.
            </para>

            <para>
                Account name canonicalization ensures that the string used to identify an account is consistent
                regardless of what was supplied to <code>bind()</code>. For example, if the user supplies an account
                name of <emphasis><EMAIL></emphasis> or just <emphasis>abaker</emphasis> and the
                <code>accountCanonicalForm</code> is set to 3, the resulting canonicalized name would be
                <emphasis>EXAMPLE\abaker</emphasis>.
            </para>

        </sect3>

        <sect3 id="zend.ldap.theory-of-operation.multi-domain-failover">

            <title>Multi-domain Authentication and Failover</title>

            <para>
                The <code>Zend_Ldap</code> component by itself makes no attempt to authenticate with multiple servers.
                However, <code>Zend_Ldap</code> is specifically designed to handle this scenario gracefully. The
                required technique is to simply iterate over an array of arrays of server options and attempt to bind
                with each server. As described above <code>bind()</code> will automatically canonicalize each name, so
                it does not matter if the user passes <code><EMAIL></code> or <code>W\bcarter</code> or
                <code>cdavis</code> - the <code>bind()</code> method will only succeed if the credentials were
                successfully used in the bind.
            </para>

            <para>
                Consider the following example that illustrates the technique required to implement multi-domain
                authentication and failover:

                <programlisting role="php"><![CDATA[<?php
$acctname = 'W\\user2';
$password = '<PASSWORD>';

$multiOptions = array(
    'server1' => array(
        'host' => 's0.foo.net',
        'username' => 'CN=user1,DC=foo,DC=net',
        'password' => '<PASSWORD>',
        'bindRequiresDn' => true,
        'accountDomainName' => 'foo.net',
        'accountDomainNameShort' => 'FOO',
        'accountCanonicalForm' => 4, // ACCT_FORM_PRINCIPAL
        'baseDn' => 'OU=Sales,DC=foo,DC=net',
    ),
    'server2' => array(
        'host' => 'dc1.w.net',
        'useSsl' => true,
        'username' => '<EMAIL>',
        'password' => '<PASSWORD>',
        'accountDomainName' => 'w.net',
        'accountDomainNameShort' => 'W',
        'accountCanonicalForm' => 4, // ACCT_FORM_PRINCIPAL
        'baseDn' => 'CN=Users,DC=w,DC=net',
    ),
);

$ldap = new Zend_Ldap();

foreach ($multiOptions as $name => $options) {

    echo "Trying to bind using server options for '$name'\n";

    $ldap->setOptions($options);
    try {
        $ldap->bind($acctname, $password);
        $acctname = $ldap->getCanonicalAccountName($acctname);
        echo "SUCCESS: authenticated $acctname\n";
        return;
    } catch (Zend_Ldap_Exception $zle) {
        echo '  ' . $zle->getMessage() . "\n";
        if ($zle->getCode() === Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH) {
            continue;
        }
    }
}]]></programlisting>

                If the bind fails for any reason, the next set of server options is tried.
            </para>

            <para>
                The <code>getCanonicalAccountName</code> call gets the canonical account name that the application
                would presumably use to associate data with such as preferences. The
                <code>accountCanonicalForm = 4</code> in all server options ensures that the canonical form is
                consistent regardless of which server was ultimately used.
            </para>

            <para>
                The special <code>LDAP_X_DOMAIN_MISMATCH</code> exception occurs when an account name with a domain
                component was supplied (e.g., <code><EMAIL></code> or <code>FOO\abaker</code> and not just
                <code>abaker</code>) but the domain component did not match either domain in the currently selected
                server options. This exception indicates that the server is not an authority for the account. In this
                case, the bind will not be performed, thereby eliminating unnecessary communication with the server.
                Note that the <code>continue</code> instruction has no effect in this example, but in practice for
                error handling and debugging purposes, you will probably want to check for
                <code>LDAP_X_DOMAIN_MISMATCH</code> as well as <code>LDAP_NO_SUCH_OBJECT</code> and
                <code>LDAP_INVALID_CREDENTIALS</code>.
            </para>

            <para>
                The above code is very similar to code using
                <link linkend="zend.auth.adapter.ldap"><code>Zend_Auth_Adapter_Ldap</code></link>. In fact, we
                recommend that you simply use that authentication adapter for multi-domain + failover LDAP based
                authentication (or copy the code).
            </para>

        </sect3>

    </sect2>

</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
{'fraction_non_alphanumeric': 0.1021151650263716, 'fraction_numerical': 0.0024468490022293513, 'mean_word_length': 1.6323171604408186, 'pattern_counts': {'":': 0, '<': 397, '<?xml version=': 0, '>': 440, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5010282', 'n_tokens_mistral': 4872, 'n_tokens_neox': 4606, 'n_words': 1409}