LDAP Authentication In Linux

In: Aashish| LDAP| Linux| centos| ubuntu

5 Mar 2010

Article by Aashish

This howto will show you howto store your users in LDAP and authenticate some of the services against it. I will not show howto install particular packages, as it is distribution/system dependant. I will focus on “pure” configuration of all componenets needed to have LDAP authentication/storage of users. The howto assumes somehow, that you are migrating from a regular passwd/shadow authentication, but it is also suitable for people who do it from scratch.

Requirements

OpenLDAP
pam_ldap
nss_ldap
PADL migrationtools

Introducion

The thing we want to achieve is to have our users stored in LDAP, authenticated against LDAP ( direct or pam ) and have some tool to manage this in a human understandable way.

This way we can use all software, which has ldap support or fallback to PAM ldap module, which will act as a PAM->LDAP gateway.

Configuring OpenLDAP

OpenLDAP consists of slapd and slurpd daemon. This howto covers one LDAP server without a replication, so we will focus only on slapd. I also assume you installed and initialized your openldap installation (depends on system/disribution). If so, let’s go to configuration part.

On my system (Gentoo), openldap’s configuration is stored in /etc/openldap, we are interested in/etc/openldap/slapd.conf file. But first we have to generate a password for LDAP administrator, to put it into the config file:

# slappasswd -h {md5}

The config looks like this:

# vim /etc/openldap/slapd.conf

include         /etc/openldap/schema/core.schema

include         /etc/openldap/schema/cosine.schema

include         /etc/openldap/schema/inetorgperson.schema

include         /etc/openldap/schema/nis.schema

allow bind_v2

pidfile         /var/run/openldap/slapd.pid

argsfile        /var/run/openldap/slapd.args

modulepath      /usr/lib/openldap/openldap

access to attrs=userPassword

        by dn="uid=root,ou=People,dc=hackadmin,dc=com" write

        by dn="cn=Manager,dc=hackadmin,dc=com" write

        by anonymous auth

        by self write

        by * none

access to dn.base="" by * read

access to *

         by dn="cn=Manager,dc=hackadmin,dc=com" write

         by * read

database        bdb

suffix          "dc=hackadmin,dc=com"

rootdn          "cn=Manager,dc=hackadmin,dc=com"
rootpw          {MD5}Tk1sMytv5ipjr+Vhcf03JQ==

directory       /var/lib/openldap-data

index   objectClass     eq

Remember to change suffix and paths to your needs.

These are basic options with some basic ACLs needed to change passwrods by user. If you want more functionality, please read the manual about openLDAP. Now when we have a proper config for slapd, we can start the daemon :

# /etc/init.d/ldap start

# chkconfig ldap on

Now we can test if openldap is running and working properly. We do not have any data yet in the directory, but we can try to bind as cn=Manager,dc=domain,dc=com. When you are asked for password, you should use the one you generated (of course the plain text version of it :) :

# ldapsearch -D “cn=Manager,dc=hackadmin,dc=com” -W

Migrate/Add data to the directory

Now when we have a running LDAP server, we have to fill it with data, either create or migrate entries. I will show you howto migrate existing entries from regular /etc/passwd, /etc/shadow , /etc/groups

The first step is to configure mogrationtools to your needs. The configuration file on gentoo is located in/usr/share/migrationtools/migrate_common.ph.

Generally you need to change only these:

$DEFAULT_BASE = "dc=hackadmin,dc=com";

$EXTENDED_SCHEMA = 1;

Now you are ready to migrate the data (actually it works even without the export command):

export ETC_SHADOW=/etc/shadow

# ./migrate_base.pl > /tmp/base.ldif
# ./migrate_group.pl /etc/group /tmp/group.ldif
# ./migrate_hosts.pl /etc/hosts /tmp/hosts.ldif
# ./migrate_passwd.pl /etc/passwd /tmp/passwd.ldif

Now we have the data in the format understood by LDAP server. Please open one the files with text editor to get used to the syntax. After that we can add the data from ldifs.

# ldapadd -D “cn=Manager,dc=domain,dc=com” -W -f /tmp/base.ldif

# ldapadd -D “cn=Manager,dc=domain,dc=com” -W -f /tmp/group.ldif

# ldapadd -D “cn=Manager,dc=domain,dc=com” -W -f /tmp/passwd.ldif

# ldapadd -D “cn=Manager,dc=domain,dc=com” -W -f /tmp/hosts.ldif

You can try searching for some data:

# ldapsearch uid=foouser

Client configuration

By client I mean the machine, which connects to LDAP server to get users and authorize. It can be also the machine, the ldap server runs on. In both cases we have to edit three files : /etc/ldap.conf, /etc/nsswitch.conf and /etc/pam.d/system-auth

Let’s start woth ldap.conf, the ldap’s client:

BASE    dc=hackadmin, dc=com

scope sub

suffix          "dc=hackadmin,dc=com"

## when you want to change user's password by root 

rootbinddn cn=Manager,dc=hackadmin,dc=com

## there are needed when your ldap dies

timelimit 5

bind_timelimit 5

uri ldap://ldap.hackadmin.com/

pam_password exop

ldap_version 3

pam_filter objectclass=posixAccount

pam_login_attribute uid

pam_member_attribute memberuid

nss_base_passwd ou=Computers,dc=cognifide,dc=pl

nss_base_passwd ou=People,dc=cognifide,dc=pl

nss_base_shadow ou=People,dc=cognifide,dc=pl

nss_base_group  ou=Group,dc=cognifide,dc=pl

nss_base_hosts  ou=Hosts,dc=cognifide,dc=pl

Now it is time for nsswitch.conf and pam

Add these to nsswitch.conf:

passwd: files ldap

shadow: files ldap

group:  files ldap

And change the system-auth (or hatever you have like login, sshd etc) to :

auth       required     pam_env.so

auth       sufficient   pam_unix.so likeauth nullok

auth       sufficient   pam_ldap.so use_first_pass

auth       required     pam_deny.so

account    sufficient   pam_unix.so

account    sufficient   pam_ldap.so

account    required     pam_ldap.so

password   required     pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3

password   sufficient   pam_unix.so nullok md5 shadow use_authtok

password   sufficient   pam_ldap.so use_first_pass

password   required     pam_deny.so

session    required     pam_limits.so

session    required     pam_unix.so

session    optional     pam_ldap.so

Time to test it. The best tool for it is a good old getent. Pick a user from your system and issue:

# getent passwd | grep foouser

You should get the result twice, if so the nss_ldap works fine. The pam part can be tested by deleting a user from the /etc/passwd and trying to log in through ssh.

Apache mod_auth_ldap

To have LDAP authorization in apache, you have to load mod_auth_ldap module

LoadModule mm_auth_ldap_module modules/mod_auth_ldap.so

Now it is enought to make .htaccess like that:

AuthName "Restricted"

AuthType Basic

AuthLDAPURL ldap://ldap.hackadmin.com:389/ou=People,dc=hackadmin,dc=com?uid

AuthLDAPBindDN "cn=Manager,dc=hackadmin,dc=com"

AuthLDAPBindPassword "your_secret_secret_password_to_ldap_admin"

require valid-user

Note that this method can be also used for webdav subversion authorization

Administration tools for ldap

There are few tool I recommend using to administrate OpenLDAP server

phpldapadmin - web based tool
ldapvi - vim browsing
PADL migrationtools - migrationtools
IDEALX sambaldap tools - samba ldap tools

Share and Enjoy:

  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Live
  • MySpace
  • Netvibes
  • Reddit
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

Related Posts:

  • No Related Posts

66 Responses to LDAP Authentication In Linux

Avatar

Matilde

June 13th, 2011 at 11:43 pm

hello…

really good article. Ready to hear more next week,my blog http://hiyatechpark.info/linkall/blogs/posts/burtsam Many Thanks….

Avatar

Sterker

June 16th, 2011 at 12:56 pm

really good article…

I have spent a bit of time going through your posts, more than I should have but I must say, its worth it! http://www.blog.lu//admin.php?op=Dashboard many Thanks….

Avatar

Erederic

June 20th, 2011 at 12:14 am

hello…

Hi there just quality post! http://ben11.sier.no/ ,i’d a great read.thank you for your article,My problem has been resolved….

Avatar

sander

June 21st, 2011 at 10:37 am

Great…

You did a great job! http://demetrius.blogfree.net/?t=3502105…

Avatar

Benzing

June 25th, 2011 at 11:48 pm

quality post…

I have spent a bit of time going through your posts! http://denice.hostablog.net/2011/06/19/the-as-well-as-sized-designs-of-vogue-italia/ ,i had a good read….

Avatar

Khantelle

July 18th, 2011 at 11:02 am

Great One…

I must say, its worth it! My link!http://duffy071.i.ph/blogs/duffy071/ ,thanks haha…

Avatar

PERRY

July 29th, 2011 at 9:53 am

Purchase@Discount.Abana” rel=”nofollow”>…

Buywithout prescription…

Avatar

LEWIS

July 29th, 2011 at 10:19 am

Cheap@Abana.Online” rel=”nofollow”>..

Buynow it…

Avatar

ROY

July 29th, 2011 at 12:58 pm

Buy@Discount.Abilify” rel=”nofollow”>…

Buywithout prescription…

Avatar

EDUARDO

July 29th, 2011 at 1:24 pm

Order@Abilify.Online” rel=”nofollow”>.

Buynow it…

Avatar

TERRENCE

July 29th, 2011 at 3:25 pm

Purchase@Discount.Abilify” rel=”nofollow”>..

Buyit now…

Avatar

GUY

July 30th, 2011 at 1:49 am

Buy@Acai.Online” rel=”nofollow”>.

Buywithout prescription…

Avatar

TRACY

July 30th, 2011 at 11:00 am

Order@Discount.Acai” rel=”nofollow”>…

Buywithout prescription…

Avatar

WALLACE

July 31st, 2011 at 4:44 am

Purchase@Generic.Acai” rel=”nofollow”>…

Buygeneric drugs…

Avatar

SALVADOR

July 31st, 2011 at 9:53 am

Cheap@Acai.Without.Prescription” rel=”nofollow”>..

Buygeneric meds…

Avatar

DUSTIN

July 31st, 2011 at 10:16 am

Cheap@Acai.500mg” rel=”nofollow”>..…

Buygeneric drugs…

Avatar

EDUARDO

July 31st, 2011 at 11:04 am

acai@berry.detox.fort.worth.where.to.buy” rel=”nofollow”>..…

Buygeneric pills…

Avatar

BRANDON

July 31st, 2011 at 11:29 am

Purchase@Generic.Acai.500mg” rel=”nofollow”>..…

Buyno prescription…

Avatar

TYLER

October 17th, 2011 at 3:57 pm

stage 3 ovarian cancer

Buy_drugs without prescription…

Avatar

JOHNNY

October 18th, 2011 at 1:13 pm

antidepressant drugs for cats

Buy_generic drugs…

Avatar

FREDDIE

October 18th, 2011 at 7:53 pm

the process of methanogens metabolism

Buy_drugs without prescription…

Avatar

MILTON

October 19th, 2011 at 7:14 pm

drug detection testing

Buy_generic meds…

Avatar

LESLIE

October 21st, 2011 at 11:25 am

best life diet

Buy_no prescription…

Avatar

PEDRO

October 22nd, 2011 at 2:25 am

ept pregnancy test

Buy_generic meds…

Avatar

DERRICK

October 23rd, 2011 at 9:01 am

market drugs edmonton

Buy_now it…

Avatar

LESLIE

October 24th, 2011 at 7:26 pm

adrenal cancer symptoms

Buy_generic drugs…

Avatar

ALFRED

October 24th, 2011 at 10:46 pm

green nerf ds lite armour

Buy_generic pills…

Avatar

GORDON

October 25th, 2011 at 11:46 pm

indigestion and chest pain

Buy_now it…

Avatar

PERRY

October 26th, 2011 at 6:26 am

bmi chart for kids

Buy_generic drugs…

Avatar

ISAAC

October 26th, 2011 at 9:46 am

ultimate diet pills

Buy_generic pills…

Avatar

NICHOLAS

October 26th, 2011 at 11:26 am

buy cheap clomid

Buy_it now…

Avatar

GEORGE

October 28th, 2011 at 3:05 pm

what is valtrex used for

Buy_generic drugs…

Avatar

KYLE

October 29th, 2011 at 8:19 pm

Avatar

FERNANDO

October 30th, 2011 at 5:24 am

Avatar

JORDAN

October 31st, 2011 at 1:23 am

canine kidney failure symptoms

Buy_generic pills…

Avatar

MILTON

October 31st, 2011 at 3:03 am

deadliest type of cancer

Buy_generic pills…

Avatar

STEPHEN

October 31st, 2011 at 4:23 pm

Avatar

EDWIN

November 1st, 2011 at 12:43 am

zyrtec and drug tests

Buy_generic meds…

Avatar

BRADLEY

November 1st, 2011 at 2:03 pm

insulin receptor inhibition

Buy_without prescription…

Avatar

WALLACE

November 2nd, 2011 at 8:23 am

persistent nausea and stomach pain

Buy_generic meds…

Avatar

ENRIQUE

November 2nd, 2011 at 8:03 pm

cordarone intravenous

Buy_generic meds…

Avatar

NELSON

November 2nd, 2011 at 9:43 pm

Avatar

JUSTIN

November 2nd, 2011 at 11:23 pm

Avatar

RICARDO

November 4th, 2011 at 2:50 am

sample tlc diets

Buy_generic drugs…

Avatar

JORGE

November 5th, 2011 at 9:47 am

Avatar

THEODORE

November 6th, 2011 at 7:27 am

rebound phenomenon during gradual clonidine withdrawal

Buy_drugs without prescription…

Avatar

CHARLIE

November 6th, 2011 at 12:27 pm

what causes constant itching

Buy_generic pills…

Avatar

BRADLEY

November 6th, 2011 at 2:07 pm

kids weight gain

Buy_generic drugs…

Avatar

JEFFREY

November 6th, 2011 at 8:47 pm

washington university marfan losartan study

Buy_drugs without prescription…

Avatar

NORMAN

November 7th, 2011 at 12:07 am

clinical trial ediary data

Buy_now it…

Avatar

MICHAEL

November 7th, 2011 at 9:47 pm

100 mile diet store

Buy_generic meds…

Avatar

JERRY

November 7th, 2011 at 11:27 pm

juliet’s on the spot acne treatment

Buy_drugs without prescription…

Avatar

JIMMY

November 8th, 2011 at 1:41 pm

pain relief until root canal

Buy_generic meds…

Avatar

ZACHARY

November 8th, 2011 at 8:21 pm

glucose to insulin ratio metabolic syndrome

Buy_drugs without prescription…

Avatar

ANDRE

November 10th, 2011 at 12:42 am

neurontin and mood lability

Buy_generic meds…

Avatar

LUKE

November 10th, 2011 at 5:21 pm

Avatar

STEVE

November 12th, 2011 at 1:01 am

dog retching vomiting weight loss

Buy_generic drugs…

Avatar

DEREK

November 12th, 2011 at 6:01 am

does hiv always lead to aids

Buy_generic meds…

Avatar

ARTURO

November 13th, 2011 at 3:41 am

space herpes

Buy_now…

Avatar

LAWRENCE

November 13th, 2011 at 6:41 pm

can iodine help thyroid function

Buy_generic drugs…

Avatar

JOSHUA

November 14th, 2011 at 1:21 am

breven medication for add

Buy_generic drugs…

Avatar

NELSON

November 16th, 2011 at 12:19 am

Avatar

DOUG

November 16th, 2011 at 11:59 am

remeron dreams

Buy_it now…

Avatar

JIM

November 17th, 2011 at 1:19 am

fruity taste and diabetes

Buy_it now…

Avatar

LANCE

November 17th, 2011 at 2:59 am

dogs to detect cancer

Buy_generic drugs…

Avatar

HUBERT

December 10th, 2011 at 4:16 am

what@is.prograf” rel=”nofollow”>…

Buygeneric pills…

Comment Form

Recent Posts