LDAP
Setup LDAP Server and Client
Install ldapServer
- install basic packakage with setting admin password
and set rogerdeng.net to root dn
1 | sudo apt -y install slapd ldap-utils libnss-ldap libpam-ldapd gnutls-bin ssl-cert |
- check if ldap is working
1 | ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts |
1 | systemctl status slapd |
Creating a LDAP hierarchical organizational tree
Creating an LDAP hierarchical organizational tree
Create Group (ou)
1
2
3
4#group.ldif
dn: ou=Groups,dc=rogerdeng,dc=net
objectClass: organizationalUnit
ou: Groups1
ldapadd -x -D "cn=admin,dc=rogerdeng,dc=net" -W -f group.ldif
Create People (ou)
1
2
3
4#people.ldif
dn: ou=People,dc=rogerdeng,dc=net
objectClass: organizationalUnit
ou: People1
ldapadd -x -D "cn=admin,dc=rogerdeng,dc=net" -W -f people.ldif
Create RogerGroup under Groups
1
2
3
4
5#rogergroup.ldif
dn: cn=RogerGroup,ou=Groups,dc=rogerdeng,dc=net
objectClass: groupOfNames
cn: RogerGroup
member: cn=roger,ou=People,dc=rogerdeng,dc=net1
ldapadd -x -D "cn=admin,dc=rogerdeng,dc=net" -W -f rogergroup.ldif
Create Sudoer Group under Groups
1 | #sudoer.ldif |
1 | ldapadd -x -D "cn=admin,dc=rogerdeng,dc=net" -W -f sudoer.ldif |
- Create user roger under People
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16dn: cn=rogerdeng,ou=People,dc=rogerdeng,dc=net
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
objectClass: organizationalPerson
objectClass: top
objectClass: person
cn: rogerdeng
sn: Deng
homeDirectory: /mnt/home/rogerdeng
loginShell: /bin/bash
uid: rogerdeng
mail: rogerdeng92@gmail.com
gidNumber: 2001
uidNumber: 2001
userPassword: <yourPassword>
:::warning
The membersOf plugin is not loaded by default in LDAP !
as normally membersOf.la is at /etc/ldap/schema/ but it is not there in my system, and I found it at /usr/lib/ldap/schema/ so I import it.
- find slapd.conf
1
sudo find / -name slapd.conf
- edit slapd.conf
- add
moduleload /usr/lib/ldap/membersOf.la
in slapd.conf
- add
- restart slapd
1
sudo systemctl restart slapd
- check if membersOf loaded and it should show that :
1
ldapsearch -x -LLL -b cn=config -D cn=admin,cn=config -W olcModuleLoad=memberof
dn: cn=config olcModuleLoad: {0}memberof.la
:::
Integrating LDAP into the System
- Install nscd and nslcd
- Configure LDAP client authentication by editing /etc/nsswitch.conf file and adding the “ldap” option to the “passwd”, “group”, and “shadow” lines. This tells the system to use LDAP for user and group information:
1
2
3passwd: compat ldap
group: compat ldap
shadow: compat ldap - Configure LDAP client authentication by editing /etc/pam.d/common-auth and adding the following line at the top: This tells the PAM authentication module to use LDAP as a source of authentication information.
1
auth sufficient pam_ldap.so use_first_pass
- Configure LDAP client account management by editing /etc/pam.d/common-account and adding the following line at the top: This tells the PAM account management module to use LDAP as a source of account information.
1
account sufficient pam_ldap.so
- Configure LDAP client password management by editing /etc/pam.d/common-password and adding the following line at the top: This tells the PAM password management module to use LDAP to change passwords.
1
password sufficient pam_ldap.so use_authtok
- Configure LDAP client session management by editing /etc/pam.d/common-session and adding the following line at the top:
1
session required pam_ldap.so
- Configure the LDAP client by editing the /etc/ldap/ldap.conf file and adding the following lines: Replace “ldap.example.com” to “ldap://127.0.0.1”
1
2
3uri ldap://ldap.example.com
base dc=example,dc=com
ldap_version 3
Replace “dc=example,dc=com” to “dc=rogerdeng,dc=net” - Restart the necessary services: These commands restart the Name Service Cache Daemon (nscd) and the Name Service LDAP Client Daemon (nslcd), which are responsible for caching and querying LDAP information.
1
2sudo systemctl restart nscd
sudo systemctl restart nslcd
After completing these steps, you should be able to use LDAP for authentication and account information on your Linux system.
LDAP over TLS
- create CA private key
1
sudo certtool --generate-privkey --bits 4096 --outfile /etc/ssl/private/mycakey.pem
- create template of CA
vim /etc/ssl/ca.info
1
2
3
4cn = rogerdeng
ca
cert_signing_key
expiration_days = 3650- signed the CA
1
2
3
4sudo certtool --generate-self-signed \
--load-privkey /etc/ssl/private/mycakey.pem \
--template /etc/ssl/ca.info \
--outfile /usr/local/share/ca-certificates/mycacert.crt - let new CA be trusted
1
sudo update-ca-certificates
- point
1
2
3sudo certtool --generate-privkey \
--bits 2048 \
--outfile /etc/ldap/ldap01_slapd_key.pem - edit ldap01 info
vim /etc/ssl/ldap01.info
1
2
3
4
5
6organization = rogerdeng
cn = ldap.rogerdeng.net
tls_www_server
encryption_key
signing_key
expiration_days = 365 - let openldap read the CA
1
2sudo chgrp openldap /etc/ldap/ldap01_slapd_key.pem
sudo chmod 0640 /etc/ldap/ldap01_slapd_key.pem - tell slapd about our TLS work via the slapd-config
- ldapTls.ldif
1
2
3
4
5
6
7
8
9dn: cn=config
add: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/ssl/certs/mycacert.pem
-
add: olcTLSCertificateFile
olcTLSCertificateFile: /etc/ldap/ldap01_slapd_cert.pem
-
add: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/ldap/ldap01_slapd_key.pemsudo ldapmodify -Y EXTERNAL -H ldapi:/// -f ldapTls.ldif
- add
ldaps:///
into/etc/default/slapd
vim /etc/default/slapd
SLAPD_SERVICES="ldap:/// ldapi:/// ldaps:///"
- restart sladp
sudo systemctl restart slapd
- Test tls
ldapwhoami -x -ZZ -H ldap://ldap.rogerdeng.net
- Test ldaps
ldapwhoami -x -H ldaps://ldap.rogerdeng.net
Setup Client CA
- generate ca pair
1
2
3
4
5mkdir ldapconsumer-ssl
cd ldapconsumer-ssl
certtool --generate-privkey \
--bits 2048 \
--outfile ldapconsumer_slapd_key.pem - create ca info for ldapconsumer
1
2
3
4
5
6organization = rogerdeng
cn = ldapconsumer.rogerdeng.net
tls_www_server
encryption_key
signing_key
expiration_days = 365 - create consumer’s ca
1
2
3
4
5
6sudo certtool --generate-certificate \
--load-privkey ldapconsumer_slapd_key.pem \
--load-ca-certificate /etc/ssl/certs/mycacert.pem \
--load-ca-privkey /etc/ssl/private/mycakey.pem \
--template ldapconsumer.info \
--outfile ldapconsumer_slapd_cert.pem - send ldapconsumer-ssl folder to ldapconusmer and repeat LDAP over TLS
Forces TLS over LDAP
- both of consumer and provider have to setup
- make sure your database type :
sudo ldapsearch -H ldapi:// -Y EXTERNAL -b "cn=config" -LLL -Q "(olcSuffix=*)" dn olcSuffix
vim ~/forcetls.ldif
1
2
3
4dn: olcDatabase={1}<change to your database type>,cn=config
changetype: modify
add: olcSecurity
olcSecurity: tls=1sudo ldapmodify -H ldapi:// -Y EXTERNAL -f forcetls.ldif
sudo service slapd force-reload
sudo systemctl restat
ldapsearch -H ldap:// -x -b "dc=rogerdeng,dc=net" -LLL -Z dn
Setup Workstation(client)
sudo apt -y install ldap-utils libnss-ldap libpam-ldapd gnutls-bin ssl-cert
- set u’re base dn
- set server to provider or consumer or both
- set send u’re ca to workstation
- send provider:/usr/local/share/ca-certificates/mycacert.crt to workstation:/ /etc/ssl/certs/ldapCA.crt
- set
/etc/ldap/ldap.conf
1
2
3
4
5
6
7URI <ldapprovider domainname>
BINDDN cn=admin,dc=rogerdeng,dc=net
BINDPW <your password>
# and also set u're ca at here and start tls
TLS_CACERT /etc/ssl/certs/ldapCA.crt
ssl start_tls - repeat Integrating LDAP into the System and restart all of packages
HA with Mirror Mode
vim syncprov.ldif
import syncprov and setup syncprov- both of provider and consumer have to setup
1
2
3
4
5
6
7
8
9
10
11
12dn: cn=module,cn=config
objectClass: olcModuleList
cn: module
olcModulePath: /usr/lib/ldap
olcModuleLoad: syncprov.la
dn: olcOverlay=syncprov,olcDatabase={1}mdb,cn=config
objectClass: olcOverlayConfig
objectClass: olcSyncProvConfig
olcOverlay: syncprov
olcSpCheckpoint: 100 10
olcSpSessionLog: 100 vim HA.ldif
(for provider)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32dn: cn=config
changetype: modify
replace: olcServerID
olcServerID: 1
dn: olcDatabase={1}mdb,cn=config
changetype: modify
replace: olcSyncRepl
olcSyncRepl: rid=010
provider=ldap://ldapconsumer.rogerdeng.net:389
bindmethod=simple
binddn="cn=admin,dc=rogerdeng,dc=net"
credentials=<ldapconsumer admin pw>
searchbase="dc=rogerdeng,dc=net"
filter="(objectClass=*)"
scope=sub
schemachecking=on
attrs="*,+"
type=refreshAndPersist
retry="5 5 300 +"
interval=00:00:00:05
starttls=critical tls_reqcert=demand
-
add: olcMirrorMode
olcMirrorMode: TRUE
-
add: olcDbIndex
olcDbIndex: entryUUID eq
-
add: olcDbIndex
olcDbIndex: entvim HA.ldif
(for consumer)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32dn: cn=config
changetype: modify
replace: olcServerID
olcServerID: 2
dn: olcDatabase={1}mdb,cn=config
changetype: modify
replace: olcSyncRepl
olcSyncRepl: rid=010
provider=ldap://ldapprovider.rogerdeng.net:389
bindmethod=simple
binddn="cn=admin,dc=rogerdeng,dc=net"
credentials=<ldapprovider admin pw>
searchbase="dc=rogerdeng,dc=net"
filter="(objectClass=*)"
scope=sub
schemachecking=on
attrs="*,+"
type=refreshAndPersist
retry="5 5 300 +"
interval=00:00:00:05
starttls=critical tls_reqcert=demand
-
add: olcMirrorMode
olcMirrorMode: TRUE
-
add: olcDbIndex
olcDbIndex: entryUUID eq
-
add: olcDbIndex
olcDbIndex: entryCSN eq- modify ldapserver (both)
ldapmodify -Y EXTERNAL -H ldapi:/// -f HA.ldif -W
- check if hierarchical organizational tree are same
Sudo Setup
- add sudoers at root
vim sudoers.ldif
1
2
3
4dn: ou=Sudoers,dc=rogerdeng,dc=net
objectClass: organizationalUnit
objectClass: top
ou: Sudoersvim sudoPermissions.ldif
1
2
3
4
5
6
7dn: cn=%sudoer,ou=Sudoers,dc=rogerdeng,dc=net
cn: %sudoer
objectClass: sudoRole
objectClass: top
sudoCommand: ALL
sudoHost: ALL
sudoUser: %sudoer
ldapadd -x -D "cn=admin,dc=rogerdeng,dc=net" -W -f ?.ldif
- add sudo schema :
vim Sudoschema.ldif
1
2
3
4
5
6
7
8
9
10
11dn: cn=sudo,cn=schema,cn=config
objectClass: olcSchemaConfig
cn: sudo
olcAttributeTypes: ( 1.3.6.1.4.1.15953.9.1.1 NAME 'sudoUser' DESC 'User(s) who may run sudo' EQUALITY caseExactIA5Match SUBSTR caseExactIA5SubstringsMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
olcAttributeTypes: ( 1.3.6.1.4.1.15953.9.1.2 NAME 'sudoHost' DESC 'Host(s) who may run sudo' EQUALITY caseExactIA5Match SUBSTR caseExactIA5SubstringsMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
olcAttributeTypes: ( 1.3.6.1.4.1.15953.9.1.3 NAME 'sudoCommand' DESC 'Command(s) to be executed by sudo' EQUALITY caseExactIA5Match SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
olcAttributeTypes: ( 1.3.6.1.4.1.15953.9.1.4 NAME 'sudoRunAs' DESC 'User(s) impersonated by sudo (deprecated)' EQUALITY caseExactIA5Match SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
olcAttributeTypes: ( 1.3.6.1.4.1.15953.9.1.5 NAME 'sudoOption' DESC 'Options(s) followed by sudo' EQUALITY caseExactIA5Match SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
olcAttributeTypes: ( 1.3.6.1.4.1.15953.9.1.6 NAME 'sudoRunAsUser' DESC 'User(s) impersonated by sudo' EQUALITY caseExactIA5Match SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
olcAttributeTypes: ( 1.3.6.1.4.1.15953.9.1.7 NAME 'sudoRunAsGroup' DESC 'Group(s) impersonated by sudo' EQUALITY caseExactIA5Match SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
olcObjectClasses: ( 1.3.6.1.4.1.15953.9.2.1 NAME 'sudoRole' SUP top STRUCTURAL DESC 'Sudoer Entries' MUST ( cn ) MAY ( sudoUser $ sudoHost $ sudoCommand $ sudoRunAs $ sudoRunAsUser $ sudoRunAsGroup $ sudoOption $ description ) )ldapmodify -Y EXTERNAL -H ldapi:/// -f ?.ldif -W
Auto Create User Script
1 |
|
Create NFS server
- install package
sudo apt-get install nfs-kernel-server
- allow ID mapping
vim /etc/default/nfs-common
NEED_IDMAPD=yes
- NFS server setup
vim /etc/exports
1
/mnt/data 10.1.1.0/24(rw,nohide,insecure,sync,no_root_squash)
- no_root_squash explain (https://linux.vbird.org/linux_server/centos6/0330nfs.php)
Mount NFS Server by autofs
- install autofs
sudo apt-get install autofs
- automount
vim /etc/auto.master
/mnt /etc/auto.nfsdb --timeout=180
vim etc/auto.nfsdb
home -fstype=nfs,rw,soft,intr nfs.rogerdeng.net:/mnt/data/home
- restart and enable
sudo systemctl restart autofs
sudo systemctl enable autofs
References
- https://blueskyson.github.io/2021/06/15/LDAP-setting/
- https://linux.vbird.org/somepaper/20050817-ldap-3.pdf
- https://ubuntu.com/server/docs/service-ldap-with-tls
- https://www.openldap.org/doc/admin24/tls.html
- https://www.digitalocean.com/community/tutorials/how-to-encrypt-openldap-connections-using-starttls#setting-up-the-client-machines
- https://idmoim.blogspot.com/2014/05/ldapadd-insufficient-access-50-openldap.html