24 tháng 10, 2013

Cài đặt OpenLDAP trên CentOS 6

Bài viết này sẽ giúp chúng ta cài đặt OpenLDAP trên CentOS6
Tài liệu và giới thiệu về OpenLDAP các bạn có thể xem tại:

LDAP (Lightweight Directory Access Protocol) là giao thức dùng để xác thực người dùng. Giao thức LDAP hoạt động ở tầng Application trong mô hình mạng TCP/IP cho phép xác thực, truy xuất thông tin của người dùng như tên, địa chỉ, số điện thoại, email …

LDAP là giao thức được phát triển từ giao thức DAP với nhiều ưu điểm vượt trội như sử dụng ít bộ nhớ và hiệu suất xử lý của CPU.

OPENLDAP là phần mềm miễn phí, nguồn mở thực thi giao thức LDAP.

OPENLDAP được phát triển bởi dự án OPENLDAP, chạy trên nhiều hệ điều hành như BSD, AIX, Android, HP-UX, Mac OS X, Solaris và Windows.

Mô hình minh họa kiến trúc của giao thức LDAP:

 LDAP directory tree (traditional naming)

LDAP directory tree (Internet naming)
Bài viết bao gồm 6 bước:

  1. Cài đặt OpenLDAP
  2. Cấu hình OpenLDAP
  3. Tạo LDAP Database
  4. Tạo file Certficate
  5. Kiểm tra cấu hình
  6. Tạo Base Domain cho LDAP Server
1.  Cài đặt OpenLDAP

Kiểm tra phiên bản CentOS
[root@server Desktop]#cat /etc/redhat-release

Kiểm tra gói openldap đã được cài đặt chưa:
[root@server Desktop]# rpm -qa openldap*
openldap-2.4.23-32.el6_4.x86_64

Nếu chưa được cài đặt hoặc cài đặt còn thiều, ta phải cài đặt đầy đủ các gói openldap như sau:
[root@server Desktop]# yum install openldap-servers openldap openldap-clients openldap-devel

Kiểm tra lại các gói đã được cài
[root@server Desktop]# rpm -qa openldap*
openldap-2.4.23-32.el6_4.1.x86_64
openldap-servers-2.4.23-32.el6_4.1.x86_64
openldap-devel-2.4.23-32.el6_4.1.x86_64
openldap-clients-2.4.23-32.el6_4.1.x86_64
[root@server Desktop]#

Tạo mật khẩu được mã hóa sử dụng cho LDAP bằng lệnh
[root@server Desktop]# slappasswd
New password:
Re-enter new password:
{SSHA}oqzVhYjZb4IS00qilR80tAnOYsADKR1z
Ta sẽ có được pass đã được mã hóa, ở đây là {SSHA}oqzVhYjZb4IS00qilR80tAnOYsADKR1z

Copy lại pass đã mã hóa này

2.    Cấu hình OpenLDAP

Mở file olcDatabase={2}bdb.ldif
[root@server Desktop]# vi /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{2\}bdb.ldif

Ta sửa các giá trị cần thiết trong file này như tên domain (tên domain của mình là dongthuanphat.com)
olcSuffix: dc=dongthuanphat,dc=com
olcRootDN: cn=Manager,dc=dongthuanphat,dc=com

Thêm vào cuối 3 dòng sau, với olcRootPW ta paste mật khẩu mã hóa vừa được tạo ở bước trên vào.
olcRootPW: {SSHA}oqzVhYjZb4IS00qilR80tAnOYsADKR1z
olcTLSCertificateFile: /etc/pki/tls/certs/slapdcert.pem
olcTLSCertificateKeyFile: /etc/pki/tls/certs/slapdkey.pem

Để cho phép user đổi mật khẩu,… ta thêm một số cấu hình sau vào cuối file.
olcAccess: to attrs=userPassword
       by self write
       by anonymous auth
       by dn.base="cn=Manager,dc=dongthuanphat,dc=com" write
       by * none
olcAccess: to *
       by self write
       by dn.base="cn=Manager,dc=dongthuanphat,dc=com" write
       by * read

Mở file olcDatabase\=\{1\}monitor.ldif và sửa giá trị domain lại cho phù hợp
[root@server Desktop]#vi /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{1\}monitor.ldif
olcAccess: {0}to *  by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read  by dn.base="cn=manager,dc=dongthuanphat,dc=com" read  by * none

3.      Tạo LDAP Database

[root@server Desktop]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@server Desktop]# chown -Rf ldap:ldap /var/lib/ldap

Tạo các Certficates
Mở file ldap
[root@server Desktop]#  vi /etc/sysconfig/ldap
Bỏ comment và đặt giá trị SLAPD_LDAPS=yes
# Run slapd with -h "... ldaps:/// ..."
#   yes/no, default: no
SLAPD_LDAPS=yes
4.      Tạo file Certficate
Ta gõ lệnh sau để tạo ra file Certificate
[root@server Desktop]# openssl req -new -x509 -nodes -out /etc/pki/tls/certs/slapdcert.pem -keyout /etc/pki/tls/certs/slapdkey.pem -days 365
Generating a 2048 bit RSA private key
.....+++
.+++
writing new private key to '/etc/pki/tls/certs/slapdkey.pem'
-----
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]:VN
State or Province Name (full name) []:Da Nang
Locality Name (eg, city) [Default City]:Da Nang
Organization Name (eg, company) [Default Company Ltd]:Home
Organizational Unit Name (eg, section) []:Home
Common Name (eg, your name or your server's hostname) []:server
Email Address []:nvkhoiit@gmail.com      
[root@server Desktop]#
File Certificate này sẽ được lưu ở thư mục /etc/pki/tls/certs/. Ta cần phân quyền lại để dịch vụ ldap có thể đọc được nội dung file này.
[root@server Desktop]# chown -Rf root:ldap /etc/pki/tls/certs/$cert.pem
[root@server Desktop]# chmod -Rf 750 /etc/pki/tls/certs/$key.pem

Tiếp tục ta mở file ldap.conf
[root@server Desktop]# vi /etc/openldap/ldap.conf
 Đặt giá trị TLS_CACERT là
TLS_CACERT /etc/pki/tls/certs/slapdcert.pem

5.      Kiểm tra cấu hình

Bước cấu hình cơ bản xong, chúng ta check lại các file cấu hình bằng lệnh slaptest
[root@server Desktop]# slaptest -u
config file testing succeeded
[root@server Desktop]#
config file testing succeeded à Các thiết lập trên file cấu hình không lỗi.

Khởi động dịch vụ slapd
[root@server Desktop]# service slapd start
Starting slapd:                                            [  OK  ]

Cho ldap khởi động cùng mỗi khi máy tính khởi động
[root@server Desktop]# chkconfig slapd on

6.      Tạo Base Domain cho LDAP Server

Tạo file test.ldif với nội dung như sau
###EMPTY line (enter xuống hàng, trống 1 dòng)
dn: dc=dongthuanphat,dc=com
dc: dongthuanphat
objectClass: top
objectClass: domain
###EMPTY line (enter xuống hàng, trống 1 dòng)
dn: ou=Sale,dc=dongthuanphat,dc=com
ou: Sale
objectClass: top
objectClass: organizationalUnit
###EMPTY line (enter xuống hàng, trống 1 dòng)
dn: ou=KyThuat,dc=dongthuanphat,dc=com
ou: KyThuat
objectClass: top
objectClass: organizationalUnit
###EMPTY line (enter xuống hàng, trống 1 dòng)

Thực hiện import file test.ldif vào CSDL của LDAP
[root@server Desktop]# ldapadd -x -W -D "cn=Manager,dc=dongthuanphat,dc=com" -f test.ldif
Nhập password ldap

Nếu thành công sẽ có các thông báo trả về như sau:
adding new entry "dc=dongthuanphat,dc=com"
adding new entry “ou=Sale, dc=dongthuanphat,dc=com”
adding new entry “ou=KyThuat, dc=dongthuanphat,dc=com”

Lưu ý: trước mỗi dòng “dn:” ta phải để trống 1 dòng kể cả “dn:” đầu tiên, và trống dòng cuối cùng, không để những khoảng trắng không cần thiết nếu không khi ta import vào file sẽ báo lỗi không import được.

to be continued...

Cài đặt LAM và LDAP Admin xem bài:

2 nhận xét:

  1. Anh oi loi nay la bi gi vay anh
    5485c02e ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif"
    5485c02e ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={2}bdb.ldif"

    Trả lờiXóa
  2. anh cho em hỏi cấu hình cái này thì có cần phải ài DNS trước không ạ?

    Trả lờiXóa