This post details the installation of OpenLDAP to manage users and authentication.

First configuration

Ref: https://doc.ubuntu-fr.org/slapd (in French)

OpenLDAP is installed using the package slapd:

sudo apt install slapd
sudo dpkg-reconfigure slapd
where the answers have to be:
  • Omit OpenLDAP server configuration? No
  • DNS domain name: mydomain.org
  • Organization name: FunOrg
  • Create database? Yes
  • Administrator password: ***
  • Do you want the database to be removed when slapd is purged? Yes
  • Remove old database? Yes

In addition, the command slappasswd can be used to set an ecrypted administrator password. The file /etc/ldap/ldap.conf is then edited to contain the following files:

BASE	dc=mydomain,dc=org

URI	ldap://localhost:389

SIZELIMIT	0
TIMELIMIT	0
DEREF never

At this stage, OpenLDAP is operational and can be started with:

systemctl start slapd.service

Creating groups and users

Ref: https://guide.ubuntu-fr.org/server/openldap-server.html (in French)

First, ldap-utils is installed:

sudo apt install ldap-utils
and can be used to add a new ldiff file that contains the structure of users and groups (hereafter named init_ldap.ldiff:
dn: ou=Someone,dc=mydomain,dc=org
objectClass: organizationalUnit
ou: Someone

dn: ou=AGroup,dc=mydomain,dc=org
objectClass: organizationalUnit
ou: AGroup

dn: cn=tuxette,ou=AGroup,dc=mydomain,dc=org
objectClass: posixGroup
cn: tuxette
gidNumber: 10000

dn: uid=tuxette,ou=Someone,dc=mydomain,dc=org
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: tuxette
sn: Chix
givenName: Tuxette
cn: Tuxette Chix
displayName: Tuxette Chix
uidNumber: 1000
gidNumber: 10000
gecos: Tuxette Chix
loginShell: /bin/bash
homeDirectory: /home/tuxette
shadowExpire: -1
shadowFlag: 0
shadowWarning: 7
shadowMin: 8
shadowMax: 999999
shadowLastChange: 10877
mail: tuxette@mydomain.org
postalCode: 31000
l: Toulouse
mobile: +33 (0)X XX XX XX XX
homePhone: +33 (0)X XX XX XX XX
title: System Administrator
postalAddress: A street in Toulouse
initials: TC

This file indicates that groups are named AGroup, users are named Someone. Then, a first group tuxette and a first user tuxette are declared. The LDAP database is updated with

sudo ldapadd -x -D cn=admin,dc=nathalievialaneix,dc=eu -W -f init_ldap.ldiff

Authentication with LDAP

Ref: https://doc.ubuntu-fr.org/utilisateurs/fr.laugier/ldap_client (in French) and https://computingforgeeks.com/how-to-configure-ubuntu-18-04-ubuntu-16-04-lts-as-ldap-client/

The following packages are first installed:

sudo apt install libnss-ldap libpam-ldap
with the following answers:
  • LDAP URI: ldap://localhost:389
  • Distinguished name of the search base: dc=mydomain,dc=org
  • LDAP version to use: 3
  • Make local root Database admin: Yes
  • Does the LDAP database require login? No
  • LDAP account for root: cn=admin,cd=mydomain,cn=eu + password

The following step requires the edition of /etc/nsswitch.conf where ldap is added to passwd and group lines.

Then, PAM is configured with the edition of </code>/etc/pam.d/common-password</code> where use_authtok is removed from line 26:

password [success=1 user_unknown=ignore default=die] pam_ldap.so try_first_pass
and the edition of /etc/pam.d/common-session where the following line is added:
session optional pam_mkhomedir.so skel=/etc/skel umask=077
to allow the automatic creation of the home directory at first connexion.

The proper functionning can be tested with

sudo su tuxette
that should create the home directory /home/tuxette and change the current user to tuxette.

LDAP password management

Ref: https://guide.ubuntu-fr.org/server/openldap-server.html#openldap-auth-config

LDAP user passwords can be managed by installing ldapscripts (that is also convenient to add and remove users and more generally to manage the LDAP database):

sudo apt install ldapscripts

It is configured by editing the file /etc/ldapscripts/ldapscripts.conf so as to obtain:

SERVER=localhost
BINDDN='cn=admin,dc=mydomain,dc=org'
BINDPWDFILE="/etc/ldapscripts/ldapscripts.passwd"
SUFFIX='dc=mydomain,dc=org'
GSUFFIX='ou=AGroups'
USUFFIX='ou=Someone'
GIDSTART=10000
UIDSTART=1000

To allow root to automatically use its password, use:

sudo sh -c "echo -n '***' > /etc/ldapscripts/ldapscripts.passwd"
where *** is the admin root password. This file needs to be protected with:
sudo chmod 400 /etc/ldapscripts/ldapscripts.passwd

Then, the command

ldapsetpasswd tuxette
can be used to set tuxette's password. It can be tested by an external SSH connexion (if password connexion is allowed)

Manage LDAP sudoers

Ref: https://doc.ubuntu-fr.org/utilisateurs/fr.laugier/ldap_client#se_connecter_en_tant_qu_utilisateur_ldap

To add a user to the sudoers group, first create the corresponding group (named admin) in LDAP and add tuxette to the admin group:

ldapaddgroup admin
ldapaddusertogroup tuxette admin

This should give tuxette the right to use sudo.

Authentification par clé ssh -> copier la clé publique dans /home/nathalie/.ssh/authorized_keys (droits sur .ssh drwx—— ; droits sur le fichier rw-r–r–) -> interdire la connexion par mot de passe comme plus haut (ssh_config)