OpenLDAP

From Wikislax

Revision as of 06:31, 8 July 2012 by WikiSysop (Talk | contribs)
Jump to: navigation, search

Contents

What is OpenLDAP ?

OpenLdap is an open source implementation of the Ligthweigth Directory Access Protocol, a lightweight alternative to the X500 Directory Address Protocol. In our installation, we use it as an address book and authentication server. To keep it safe, ldap access is restricted to localhost (slapd daemon listening only on localhost, ldap and ldaps ports closed at firewall level). The referecence document to read is the Open Ldap Admin Guide but it's lengthy. Wikipedia has a nice summary page that could help setting up the big view.

Installing OpenLDAP

There is a chicken-and-egg problem with OpenLdap and CyrusSasl as they reference each other. So first install OpenLdap without --enable-spasswd --with-cyrus-sasl, then after installing Cyrus-sasl (next chapter), reinstall OpenLdap with --enable-spasswd --with-cyrus-sasl (this will be reminded at next chapter). Download OpenLDAP, untar to /usr/local then compile and install as described below :

# tar -tvf openldap-x.y.z.tgz
# tar -C /usr/local -xvf openldap-x.y.z.tgz
# cd /usr/local
# chown -R root:root openldap-x.y.z
# cd openldap-x.y.z
# ./configure --help | less
# ./configure --libdir=/usr/local/lib64 --mandir=/usr/local/man --with-tls
# make depend
# make
# make test
# make install

To prepare running ldap as an unpriviledged user, execute the commands below. Note : it will also be a good idea to review the individual file permissions under the openldap directories and to restrict access to the ldap user :

# groupadd ldap
# mkdir /var/run/ldap
# useradd -s /bin/false -d /var/run/ldap -g ldap ldap
# chown -R ldap:ldap /etc/openldap /usr/local/etc/openldap /usr/local/var/openldap-data /var/run/ldap

Configuring OpenLDAP

OpenLdap is made of two daemons but we will use only one. slapd is the stand-alone LDAP daemon and slurpd is the stand-alone LDAP update replication daemon, that we will not use. Below is a working example of a slapd.conf configuration file that you can use, just replacing the domain and the rootpw value. The password-hash {CLEARTEXT} option is mandatory for SASL to use LDAP (the format is enforced when using the password). The authz-regexp maps an authentication request to a real entry in the directory. The reason why this mapping is useful is that it avoids knowing anything about the underlying directory structure to authenticate. See man slapd.conf for more configuration details.

#
# See slapd.conf(5) for details on configuration options.
# This file should NOT be world readable.
#
include         /usr/local/etc/openldap/schema/core.schema

# Do not enable referrals until AFTER you have a working directory
# service AND an understanding of referrals.
# referral       ldap://root.openldap.org

pidfile         /var/run/ldap/slapd.pid
argsfile        /var/run/ldap/slapd.args

# Load dynamic backend modules:
# modulepath    /usr/local/libexec/openldap
# moduleload    back_bdb.la
# moduleload    back_hdb.la
# moduleload    back_ldap.la

# Sample security restrictions
#       Require integrity protection (prevent hijacking)
#       Require 112-bit (3DES or better) encryption for updates
#       Require 63-bit encryption for simple bind
# security ssf=1 update_ssf=112 simple_bind=64

# Sample access control policy:
#       Root DSE: allow anyone to read it
#       Subschema (sub)entry DSE: allow anyone to read it
#       Other DSEs:
#               Allow self write access
#               Allow authenticated users read access
#               Allow anonymous users to authenticate

access to dn.base="" by * read
access to dn.base="cn=Subschema" by * read

access to *
        by self write
        by users read
        by anonymous auth

access to attrs=userPassword
        by self =dxw
        by users none
        by anonymous auth

# if no access controls are present, the default policy
# allows anyone and everyone to read anything but restricts
# updates to rootdn.  (e.g., "access to * by * read")
#
# rootdn can always read and write EVERYTHING!

#######################################################################
# proxy user definitions - SASL requires CLEARTEXT
#######################################################################

password-hash   {CLEARTEXT}

authz-regexp
                uid=([^,]*),cn=digest-md5,cn=auth
                cn=$1,dc=domain,dc=com

authz-policy    to

access to attrs=authzTo
        by self none
        by users none
        by anonymous none

#######################################################################
# Berkeley Data Base front end definitions
#######################################################################

database        bdb
suffix          "dc=domain,dc=com"
rootdn          "cn=Manager,dc=domain,dc=com"

# Cleartext passwords, especially for the rootdn, should
# be avoid.  See slappasswd(8) and slapd.conf(5) for details.
# To generate {SSHA} secret : slappasswd -s secret
rootpw          {SSHA}16U2kC8+yDaWDtaKWsyvKSRHMWCUmcKg

# The database directory MUST exist prior to running slapd AND
# should only be accessible by the slapd and slap tools.
# Mode 700 recommended.
directory       /usr/local/var/openldap-data

# Indexing options for database #1
index           objectClass     eq
index           cn,sn           eq

#######################################################################
# TLS Certificates
#######################################################################

TLSCACertificateFile    /etc/ssl/certs/cacert.pem
TLSCACertificatePath    /etc/ssl/certs
TLSCertificateFile      /etc/ssl/certs/mtacert.pem
TLSCertificateKeyFile   /etc/ssl/private/ldap.mtakey.pem.unsecure

When using LDAP clients on the same host, the ldap.conf file must include a TLS_CACERT directive specifiying the same certificate as in TLSCACertificateFile above. Here is a working example of such a file :

#
# LDAP Defaults
#

# See ldap.conf(5) for details
# This file should be world readable but not world writable.

#BASE   dc=example,dc=com
#URI    ldap://ldap.example.com ldap://ldap-master.example.com:666

#SIZELIMIT      12
#TIMELIMIT      15
#DEREF          never

TLS_CACERT      /etc/ssl/certs/cacert.pem
TLS_CACERTDIR   /etc/ssl/certs

After that it is possible to listen on port ldaps and to use TLS to encapsulate the communications.

Running OpenLDAP

Start the stand-alone LDAP server slapd, then check to see if the server is running with ps -ef | grep slapd and if not use switch -d296 to troubleshoot from slapd output (see man slapd for details). When OK try a ldapsearch.

# /usr/local/libexec/slapd -u ldap -g ldap -H ldaps://localhost/
# ps -ef | grep slapd
# /usr/local/bin/ldapsearch -x -H ldaps://localhost/ -b "" -s base "(objectclass=*)" namingContexts

Slapd runs as a daemon so must be launched at startup and stopped at shutdown. Update /etc/rc.d/rc.local and /etc/rc.d/rc.local_shutdown accordingly :

# vi /etc/rc.d/rc.local
. . .
# start slapd
if [ -x /usr/local/sbin/slapd ]; then
        echo "Starting slapd daemon: /usr/local/sbin/slapd -u ldap -g ldap -h ldaps://localhost/"
        /usr/local/sbin/slapd -u ldap -g ldap -h ldaps://localhost/
fi
 <esc>
:x
# vi /etc/rc.d/rc.local_shutdown
. . .
# stop slapd
if [ -r /var/run/ldap/slapd.pid ]; then
        echo "Stopping slapd: kill -INT `cat /var/run/ldap/slapd.pid`"
        kill -INT `cat /var/run/ldap/slapd.pid`
fi
<esc>
:x

Creating Directory Entries

To add entries to the directory, create an ldif file. Run ldapadd to add the entries, then run ldapsearch to make sure it worked.

# cd /usr/local/etc/openldap
# vi db.ldif
i
dn: dc=domain,dc=com
objectClass: dcObject
objectClass: organization
o: organization
dc: domain

dn: cn=Manager,dc=domain,dc=com
objectclass: organizationalRole
cn: Manager

dn: cn=proxyUser,dc=domain,dc=com
objectclass: person
cn: proxyUser
sn: proxyUser
userPassword: proxyPassword
authzTo: ldap:///dc=domain,dc=COM??sub?(objectclass=person)

dn: cn=postmaster,dc=domain,dc=COM
objectclass: person
cn: postmaster
sn: postmaster
userPassword: postmasterPassword

dn: cn=myUser,dc=domain,dc=COM
objectclass: person
cn: myUser
sn: myUser
userPassword: myPassword
<esc>

# ldapadd -x -H ldaps://localhost/ -D "cn=Manager,dc=domain,dc=com" -W -f db.ldif
# ldapsearch -x -H ldaps://localhost/ -b "dc=domain,dc=com" "(objectclass=*)"
# ldapdelete -x -H ldaps://localhost/ -D "cn=Manager,dc=domain,dc=com" -W "cn=myOtherUser,dc=domain,dc=com"

the authzTo: item above defines an OpenLdap proxy user. Once authenticated, an OpenLdap proxy user can impersonate other OpenLdap users. This affords e.g. checking passwords (needed by SASL) or updating data for other users.


OpenSSL Main Page Cyrus-SASL
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox