Tuesday, August 11, 2009

OpenLDAP Configuration in RHEL6.2



Another vital configuration which is most important aspect of my RHEL6 learning,I was new to setup LDAP with TLS configuration in Linux, this was another painful configuration which kept me alive late night for so many days and now it is open to all who are intrested to play with LDAP.

I'll cover LDAP configuration in Following parts:
1. Install and Configure LDAP Server
2. populate LDAP server with organizational Units,user and group using ldif file exported from other LDAP server
3. Test the LDAP Server Fuctionality with client browsers
4.create SSL certificates to configure LDAP to support TLS[transport level security]/SSL[secure Socket  Layer]
5. Configure LDAP server to provide support for TLS and SSL
6. Configure LDAP client to connect with TLS/SSL enabled LDAP Server

ok now moving ahead with configuration, I like to mentioned that I'm using OpenLDAP which is bundled along with RHEL6 we can also download the newest version from OPENLDAP website[http://www.openldap.org/].


1.INSTALL & CONFIGURE LDAP SERVER
I'm not going to cover yum configuration in this section assuming that it is already configured.

yum install openldap* openssl*


3.CREATE SSL CERTIFICATES TO CONFIGURE LDAP TO SUPPORT TLS & SSL
by running this command you can create your certificates:


cd /etc/openldap/cacerts
openssl req -x509 -nodes -newkey rsa:1024 -days 730 -out slapd.crt -keyout slapd.key


you'll find output something like this:
Generating a 1024 bit RSA private key
.................................................................++++++
.....++++++
writing new private key to 'slapd.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:

after providing all inputs slapd.crt & slapd.key get created in /cacerts folder.


4.CONFIGURING LDAP SERVER TO PROVIDE TLS & SSL SUPPORT:
I'm my ldap configuration I'm using slapd.conf for my configuration:
backup /etc/openldap/slapd.d  and remove it from /etc/openldap/ folder and copy slapd.conf from /usr/share/openldap-servers/slapd.obsolete and rename it to slapd.conf

My slapd.conf:
-------------------------------------------------------------------------------

#
# See slapd.conf(5) for details on configuration options.
# This file should NOT be world readable.
#

include /etc/openldap/schema/corba.schema
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/duaconf.schema
include /etc/openldap/schema/dyngroup.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/java.schema
include /etc/openldap/schema/misc.schema
include /etc/openldap/schema/nis.schema
include /etc/openldap/schema/openldap.schema
include /etc/openldap/schema/ppolicy.schema
include /etc/openldap/schema/collective.schema

# Allow LDAPv2 client connections.  This is NOT the default.
allow bind_v2

# 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/openldap/slapd.pid
argsfile /var/run/openldap/slapd.args

# Load dynamic backend modules
# - modulepath is architecture dependent value (32/64-bit system)
# - back_sql.la overlay requires openldap-server-sql package
# - dyngroup.la and dynlist.la cannot be used at the same time

# modulepath /usr/lib/openldap
# modulepath /usr/lib64/openldap

# moduleload accesslog.la
# moduleload auditlog.la
# moduleload back_sql.la
# moduleload chain.la
# moduleload collect.la
# moduleload constraint.la
# moduleload dds.la
# moduleload deref.la
# moduleload dyngroup.la
# moduleload dynlist.la
# moduleload memberof.la
# moduleload pbind.la
# moduleload pcache.la
# moduleload ppolicy.la
# moduleload refint.la
# moduleload retcode.la
# moduleload rwm.la
# moduleload seqmod.la
# moduleload smbk5pwd.la
# moduleload sssvlv.la
# moduleload syncprov.la
# moduleload translucent.la
# moduleload unique.la
# moduleload valsort.la

# The next three lines allow use of TLS for encrypting connections using a
# dummy test certificate which you can generate by changing to
# /etc/pki/tls/certs, running "make slapd.pem", and fixing permissions on
# slapd.pem so that the ldap user or group can read it.  Your client software
# may balk at self-signed certificates, however.
TLSCACertificateFile /etc/openldap/cacerts/slapd.crt
TLSCertificateFile /etc/openldap/cacerts/slapd.crt
TLSCertificateKeyFile /etc/openldap/cacerts/slapd.key

# 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
# Directives needed to implement policy:
# 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
#
# 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!

# enable on-the-fly configuration (cn=config)
database config
access to *
by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage
by * none

# enable server status monitoring (cn=monitor)
database monitor
access to *
by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read
        by dn.exact="cn=Manager,dc=example,dc=com" read
        by * none

#######################################################################
# database definitions
#######################################################################

database bdb
suffix dc=example,dc=com                           ##specify appropriate suffix
checkpoint 1024 15
rootdn cn=Manager,dc=example,dc=com    ## specify appropriate rootDN

# The database directory MUST exist prior to running slapd AND
# should only be accessible by the slapd and slap tools.
# Mode 700 recommended.
directory /var/lib/ldap

# Indices to maintain for this database
index objectClass                       eq,pres
index ou,cn,mail,surname,givenname      eq,pres,sub
index uidNumber,gidNumber,loginShell    eq,pres
index uid,memberUid                     eq,pres,sub
index nisMapName,nisMapEntry            eq,pres,sub

# Replicas of this database
#replogfile /var/lib/ldap/openldap-master-replog
#replica host=127.0.0.1:389 starttls=critical
#     bindmethod=sasl saslmech=GSSAPI
#     authcId=host/ldap-master.example.com@EXAMPLE.COM
rootpw {crypt}41YetSOwKRgm2    ## created using slapdpasswd


Now our LDAP Server is almost ready:

2.POPULATE LDAP SERVER WITH ORGANIZATIONAL UNITS,USERS AND GROUPS USING LDIF FILE EXPORTED FROM OTHER LDAP SERVER:

 My sample ldif file[domain.ldif]:
--------------------------------------------------------------------------------------------------


dn: dc=example,dc=com
objectclass: top
objectclass: domain
dc: example
description: Fictional example domain

dn: ou=People,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: People
description: Fictional example organizational unit
telephoneNumber: 555-5559

dn: cn=June Rossi,ou=People,dc=example,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: June Rossi
sn: Rossi
givenName: June
mail: rossi@example.com
userPassword: {sha}KDIE3AL9DK
ou: Accounting
ou: people
telephoneNumber: 2616
roomNumber: 220

dn: cn=Marc Chambers,ou=People,dc=example,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: Marc Chambers
sn: Chambers
givenName: Marc
mail: chambers@example.com
userPassword: {sha}jdl2alem87dlacz1
telephoneNumber: 2652
ou: Manufacturing
ou: People
roomNumber: 167

dn: cn=Robert Wong,ou=People,example.com Corp,dc=example,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: Robert Wong
cn: Bob Wong
sn: Wong
givenName: Robert
givenName: Bob
mail: bwong@example.com
userPassword: {sha}nn2msx761
telephoneNumber: 2881
roomNumber: 211
ou: Manufacturing
ou: people

dn: ou=Groups,dc=example,dc=com
objectclass: top
objectclass: organizationalUnit
ou: groups
description: Fictional example organizational unit


--------------------------------------------------------------------------------------------------

Run following command to add OU ,users and groups:
[root@ldap_server ~]# ldapadd -x -D "cn=Manager,dc=example,dc=com" -W -f domain.ldif

now LDAP is polulated you can check with following commands:
[root@ldap_server ~]# ldapsearch -x
# extended LDIF
#
# LDAPv3
# base (default) with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#

# example.com
dn: dc=example,dc=com
objectClass: top
objectClass: domain
dc: example
description: Fictional example domain

# People, example.com
dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit
ou: people

# jsmith, people, example.com
dn: uid=jsmith,ou=people,dc=example,dc=com
cn: John Smith
givenName: John
sn: Smith
uid: jsmith
uidNumber: 10000
gidNumber: 10000
homeDirectory: /home/jsmith
mail: jsmith@dev.local
objectClass: top
objectClass: posixAccount
objectClass: shadowAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
userPassword:: e0NSWVBUfSo=

# groups, example.com
dn: ou=groups,dc=example,dc=com
objectClass: organizationalUnit
ou: groups

# jsmith, groups, example.com
dn: cn=jsmith,ou=groups,dc=example,dc=com
cn: jsmith
objectClass: top
objectClass: posixGroup
gidNumber: 10000

# aroy, people, example.com
dn: uid=aroy,ou=people,dc=example,dc=com
cn: amit roy
givenName: amit
sn: roy
uid: aroy
uidNumber: 11000
gidNumber: 11000
homeDirectory: /home/aroy
mail: aroy@dev.local
objectClass: top
objectClass: posixAccount
objectClass: shadowAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
userPassword:: e1NTSEF9YktTWE1JYjN0bUtHTngxT3pHUi9tR2RyKzVWWU4wUEk=

# search result
search: 2
result: 0 Success

# numResponses: 7
# numEntries: 6

Now LDAP is ready to connect with client and view different objects[OU,USERS,GROUPS]

3.TEST LDAP SERVER FUNCTIONALITY WITH CLIENT:
I tested it with following commands:
a)getent

[root@ldap_server ~]# getent passwd jsmith
jsmith:x:10000:10000:John Smith:/home/jsmith:/bin/bash


b)id

[root@ldap_server ~]# id jsmith
uid=10000(jsmith) gid=10000(jsmith) groups=10000(jsmith)


these two command display the user from Ldap.

5.CONFIGURE LDAP SERVER TO PROVIDE SUPPORT FOR  TLS & SSL
we already configure LDAP and created certificates to support TLS and SSL now configure LDAP to provide support for TLS and SSL:

open slapd.conf
cd /etc/openldap/slapd.conf
add following entry:


TLSCACertificateFile /etc/openldap/cacerts/slapd.crt
TLSCertificateFile /etc/openldap/cacerts/slapd.crt
TLSCertificateKeyFile /etc/openldap/cacerts/slapd.key

save it

open ldap file

cd /etc/sysconfig/ldap

SLAPD_LDAPS=yes    ###[default value is no make it yes]
SLAPD_LDAP=yes
SLAPD_LDAPI=yes


save it
and 

restart with slapd service
service slapd restart

now check the LDAP is enabled with TLS and SSL on 636 port
run this command:
openssl s_client -connect 192.168.0.112:636 -showcerts


CONNECTED(00000003)
depth=0 C = IN, ST = maha, L = pune, O = example, OU = example, CN = 192.168.0.112, emailAddress = root@localhost
verify error:num=18:self signed certificate
verify return:1
depth=0 C = IN, ST = maha, L = pune, O = example, OU = example, CN = 192.168.0.112, emailAddress = root@localhost
verify return:1
---
Certificate chain
 0 s:/C=IN/ST=maha/L=pune/O=example/OU=example/CN=192.168.0.112/emailAddress=root@localhost
   i:/C=IN/ST=maha/L=pune/O=example/OU=example/CN=192.168.0.112/emailAddress=root@localhost
-----BEGIN CERTIFICATE-----
MIIC3DCCAkWgAwIBAgIJAKWoGlraCD0gMA0GCSqGSIb3DQEBBQUAMIGGMQswCQYD
VQQGEwJJTjENMAsGA1UECAwEbWFoYTENMAsGA1UEBwwEcHVuZTEQMA4GA1UECgwH
ZXhhbXBsZTEQMA4GA1UECwwHZXhhbXBsZTEWMBQGA1UEAwwNMTkyLjE2OC4wLjEx
MjEdMBsGCSqGSIb3DQEJARYOcm9vdEBsb2NhbGhvc3QwHhcNMTIwNjE0MjAzNDI5
WhcNMTQwNjE0MjAzNDI5WjCBhjELMAkGA1UEBhMCSU4xDTALBgNVBAgMBG1haGEx
DTALBgNVBAcMBHB1bmUxEDAOBgNVBAoMB2V4YW1wbGUxEDAOBgNVBAsMB2V4YW1w
bGUxFjAUBgNVBAMMDTE5Mi4xNjguMC4xMTIxHTAbBgkqhkiG9w0BCQEWDnJvb3RA
bG9jYWxob3N0MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuwSb+OdeCACIz
4AWDakdPe4DejReq6pIsvcpbJ4E+wU30et7cJnbcsPKVgWqySIFXlORvOb1A6K77
OEKflC4DxxvNMd3O57O9j1G8GQ5huciXO3KgkooQG9a8MRWECR2oZ93GD4kId+MB
l5GpYwu3tpT55di3RGKrP610rt8HDQIDAQABo1AwTjAdBgNVHQ4EFgQUkwhSBuvP
hAzgOevAUX78wcAezvQwHwYDVR0jBBgwFoAUkwhSBuvPhAzgOevAUX78wcAezvQw
DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQAWXsq4GRwGyvp+pp2H/Sfe
5CoZlM+JHUl7jxkvPKSAGL4yW5JZUwoBFFAHzMBG/82RJPaOYBlbKYIqUMhBIrp/
1HiakVgVvTeHzxdI429rYZb20Idug2AuL2qTNsBs9g6Flg8YybvGlFBZoiUucb8W
ZwxcCEPW4GbEvaBp8+odvA==
-----END CERTIFICATE-----
---
Server certificate
subject=/C=IN/ST=maha/L=pune/O=example/OU=example/CN=192.168.0.112/emailAddress=root@localhost
issuer=/C=IN/ST=maha/L=pune/O=example/OU=example/CN=192.168.0.112/emailAddress=root@localhost
---
No client certificate CA names sent
---
SSL handshake has read 895 bytes and written 311 bytes
---
New, TLSv1/SSLv3, Cipher is AES256-SHA
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1
    Cipher    : AES256-SHA
    Session-ID: 0742E5A8AEFF2A6F2B6FB8812260A7C7449BAEB36C4CB4AE3E48790044F4EBFD
    Session-ID-ctx:
    Master-Key: 2596FB9A333FE02936D0474DE0C5320229E4ED836DC296BD287794230EDDE9AACF430D20B15FE3369AECCB9164FB2A80
    Key-Arg   : None
    Krb5 Principal: None
    PSK identity: None
    PSK identity hint: None
    Start Time: 1340110089
    Timeout   : 300 (sec)
    Verify return code: 18 (self signed certificate)
---
if you get output something like this that means your server is properly configured to support TLS/SSL

Note:
some useful commands like
slapcat
slaptest
netstat -ntlp

help you to verify LDAP configuration

6. CONFIGURE LDAP CLIENT TO CONNECT WITH TLS/SSL ENABLED LDAP SERVER:


open ldap.conf

cd /etc/openldap/ldap.conf
add following entry:


TLS_CACERTDIR /etc/openldap/cacerts
TLS_CACERTFILE /etc/openldap/cacerts/slapd.crt
host ldap_server
URI ldap://192.168.0.112/
save it



Restart slapd service
service slapd restart


I tested it with following commands:
a)getent

[root@ldap_server ~]# getent passwd jsmith
jsmith:x:10000:10000:John Smith:/home/jsmith:/bin/bash


b)id

[root@ldap_server ~]# id jsmith
uid=10000(jsmith) gid=10000(jsmith) groups=10000(jsmith)


these two command display the user from Ldap








hope this help to those who really wanna try to configure LDAP.










No comments: