打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
OpenLDAP学习笔记
(1) 何时使用LDAP
----------------------------------------------------------------------------------
原则上,LDAP 在目的上类似于 NIS。两者都将一些关于网络 配置的 结构化信息从客户机发布
到 服务器;但是在层次地构造哪些信息发布到哪些客户机、将请求重定向到其他必需的 LDAP
服务器以及以安全机制进行构建等方面,LDAP走得更远一些。此外,LDAP为客户机提供机制和
工具来更新存储在 LDAP 服务器中的信息,该服务器然后又将信息发布到请求该信息的客户机
(当然,需要具有权限)。

NIS于LDAP的一個區別是: LDAP可以做遞增的即時更新,而NIS的更新總是整個數據庫傳送的.


(2) 安裝OpenLDAP之前需先配置的支持庫
----------------------------------------------------------------------------------
在运行 OpenLDAP(通常用于 Linux 上的免费 软件实现,尽管存在一些商业实现)之前,需要
安装或者验证已经存在几个必要的库:

[1] OpenSSL Transport Layer Security (TLS) 服务可从OpenSSL Project(或通过您的Linux
发行版的安装机制)而获得:

whereis openssl;                #檢查系統是否已經安裝OpenSSL;
apt-get install openssl;        #在Debian系統中安裝OpenSSL;


[2] Kerberos Authentication Services 是可选支持的,但是最好安装它。MIT Kerberos或者
Heimdal Kerberos 将会工作。

whereis krb;                        #檢查系統是否已經安裝Kerberos;
yum list | grep krb;                #在RedHat系統中查找并安裝Kerberos;
apt-get install krb5-user;        #在Debian系統中安裝Kerberos;


[3] Simple Authentication and Security Layer 可以作为基本发行版的一部分安装,但是也
可以作为 Cyrus SASL 获得。

whereis sasl;                        #檢查系統是否已經安裝;
apt-get install libsasl2 libsasl2-modules libsasl2-modules- sql;


[4] Sleepycat Software Berkeley DB 是推荐的,尽管其他DBM实现可能是兼容的。

參考網站: http://wiki.huihoo.com/index.php?title=Berkeley_DB

whereis libdb;                        #檢查系統是否已經安裝;

在Debian系統使用apt-get工具安裝Berkeley v4.3數據庫:
apt-get install libdb4.3;        #Berkeley v4.3 Database Libraries [runtime];
apt-get install libdb4.3-dev;        #Berkeley v4.3 Database Libraries [development];
apt-get install libdb4.3++;        #Berkeley v4.3 Database Libraries for C++[runtime];
apt-get install libdb4.3++-dev;        #Database Libraries for C++ [development];

在Debian系統中使用apt-get工具安裝Berkeley v4.4數據庫:
apt-get install libdb4.4 libdb4.4-dev;

在RedHat系統中使用yum工具安裝:
yum install libdbi.i386;
yum install libdbi-devel.i386;

使用Tarball資源安裝BerkeleyDB:
cd build_unix
../dist/configure
$ make
# make install
install to /usr/local/BerkeleyDB.4.4


[5] Posix线程和TCP包装器(TCP Wrappers)如果不是严格必需的,也最好安装它。

參考網址:
http://www-128.ibm.com/developer ... _thread1/index.html
----------------------------------------------------------------------------------


(3) 安裝和配置OpenLDAP
----------------------------------------------------------------------------------
在RedHat系統中使用yum工具安裝OpenLDAP:
yum install openldap.i386;
yum install openldap-devel.i386;
yum install openldap-servers.i386;
yum install openldap-clients.i386;        #包括常用的客戶端工具(如ldapsearch等);

在Debian系統中使用apt-get工具安裝OpenLDAP:
apt-get install slapd;                #這是OpenLDAP的服務器套件;
apt-get install ldap-utils;        #這是OpenLDAP的實用工具(包括客戶端指令);
請注意: 使用apt-get工具安裝slapd時,會提問管理者密碼, 并會自動從分析和提取主機名稱
當作域組件的根(root)的設置參數, 并將之寫入slapd.conf配置文件中.

下載OpenLDAP的Tarball資源,并执行普通的安裝命令:
wget ftp://ftp.openldap.org/pub/OpenL ... /openldap-2.4.6.tgz;
tar zxvf openldap-2.4.6.tgz;
cd openldap-2.4.6/;
./configure;
make depend;
make;
make test;
make install;

在完成基本的安装之后,还需要配置slapd配置,它通常位于/etc/openldap/slapd.conf或者
/usr/local/etc/openldap/slapd.conf路徑中(Debian系統預設在/etc/ldap/slapd.conf路徑),
在此配置文件中,应该包含您的域组件,例如:

vim /etc/ldap/slapd.conf;        #如下Debian系統(用apt-get命令)安裝的默認配置;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
include         /etc/ldap/schema/core.schema
include         /etc/ldap/schema/cosine.schema
include         /etc/ldap/schema/nis.schema
include         /etc/ldap/schema/inetorgperson.schema
pidfile         /var/run/slapd/slapd.pid
argsfile        /var/run/slapd/slapd.args
loglevel        0
modulepath      /usr/lib/ldap
moduleload      back_bdb
sizelimit 500
tool-threads 1
backend         bdb
checkpoint 512 30
database        bdb
suffix          "dc=hung,dc=mooo,dc=com"

rootdn          "cn=admin,dc=hung,dc=mooo,dc=com"
rootpw          {SSHA}lu5jWK06Yvs2Qjye8oTa/YAvaz/6GBmV

directory       "/var/lib/ldap"
dbconfig set_cachesize 0 2097152 0
dbconfig set_lk_max_objects 1500
dbconfig set_lk_max_locks 1500
dbconfig set_lk_max_lockers 1500
index           objectClass eq
lastmod         on
access to attrs=userPassword,shadowLastChange
        by dn="cn=admin,dc=hung,dc=mooo,dc=com" write
        by anonymous auth
        by self write
        by * none
access to dn.base="" by * read
access to *
        by dn="cn=admin,dc=hung,dc=mooo,dc=com" write
        by * read
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 在默認的配置中原本是注釋了rootdn和缺少rootpw兩個參數的,這里必須啟用或添加;
這個rootdn對于LDAP而言,就像是Linux中的root身份一樣,可以對整個LDAP系統的數據庫資料
做新增,刪除和修改等操作. 例如上述所指定的rootdn: "cn=admin,dc=hung,dc=mooo,dc=com"
就是本LDAP服務器的管理員.

rootpw就是管理員(rootdn)的密碼,可以使用明文密碼,但這不是好習慣,最好還是使用加密過
的字串. 可以使用slappasswd這個專用命令, 來創建后臺數據庫管理員密碼的密文字串,并且
可以指定字串的加密方式, 例如:

slappasswd -h {SSHA};        #可用模式有: CRYPT,MD5,SMD5,SSHA和SHA(SSHA是預設且最強的);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
New password:XXXXXX                                #在這里輸入您想設定的密碼;
Re-enter new password:                                #再重復輸入一次密碼;
{SSHA}lu5jWK06Yvs2Qjye8oTa/YAvaz/6GBmV
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 上述最后一行就是密文,須將它粘帖到slqpd.conf配置文件中,當作rootpw的設置值;

slaptest;                #檢驗配置是否正確(如下返回成功信息);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
config file testing succeeded
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

相關配置參數的說明:
include                指定slapd要讀入的附加的配置文件;
pidfile                指定slapd的PID文件;
argsfile        指定slapd的進程執行參數;
database        指定儲存目錄數據的后臺數據庫格式;
suffix                指定查詢的DN后綴,如果匹配此后綴就將被傳遞給后臺數據庫;
rootdn                指定后臺數據庫的管理員用戶帳號;
rootpw                指定后臺數據庫的管理員用戶密碼;
directory        指定后臺數據庫的儲存目錄;
index                指定索引,用來維護給定的屬性;
access to        設定目錄的ACL訪問控制;

關于后臺數據庫的類型:
bdb                Berkeley DB transactional backend;
dnssrv                DNS SRV backend;
ldap                LDAP Proxy backend;
ldbm                Lightweitht DBM backend;
meta                Meta directory backend;
monitor                Monitor backend;
passwd                Provides read-only access to passwd;
perl                Perl programmable backend;
shell                Shell(external program) backend;
sql                SQL programmable backend;

關于ACL訪問控制列表的允許動作:
none                不允許訪問;
auth                僅供驗證用途;
compare                僅容許比較;
search                可供搜索過濾訪問;
read                數據可被查看;
write                數據可被查看,也可被修改;

ls -l /var/lib/ldap/;        #看看數據庫文件;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-rw-r--r-- 1 openldap openldap    4096 2007-11-13 02:37 alock
-rw------- 1 openldap openldap    8192 2007-11-13 02:17 __db.001
-rw------- 1 openldap openldap 2629632 2007-11-13 02:17 __db.002
-rw------- 1 openldap openldap   98304 2007-11-13 02:17 __db.003
-rw------- 1 openldap openldap  565248 2007-11-13 02:17 __db.004
-rw------- 1 openldap openldap   24576 2007-11-13 02:17 __db.005
-rw-r--r-- 1 openldap openldap      96 2007-11-13 02:17 DB_CONFIG
-rw------- 1 openldap openldap    8192 2007-11-13 02:17 dn2id.bdb
-rw------- 1 openldap openldap   32768 2007-11-13 02:17 id2entry.bdb
-rw------- 1 openldap openldap   53979 2007-11-13 02:37 log.0000000001
-rw------- 1 openldap openldap    8192 2007-11-13 02:17 objectClass.bdb
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 這個目錄下的數據庫非常重要,必須經常備份;

/usr/sbin/slapd -4 -d10 -f /etc/ldap/slapd.conf;        #以測試模式啟動slapd進程;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@(#) $OpenLDAP: slapd 2.3.30 (Mar  9 2007 05:43:02) $
        root@windlord:/tmp/buildd/openldap2.3-2.3.30/debian/build/servers/slapd
slapd starting
daemon: added 4r listener=(nil)
daemon: added 6r listener=0x8129180
daemon: select: listen=6 active_threads=0 tvp=NULL

daemon: shutdown requested and initiated.
daemon: closing 6
slapd shutdown: waiting for 0 threads to terminate
slapd stopped.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/etc/init.d/slapd start;        #啟動ldap服務,然后用如下命令測試其反應;
/usr/bin/ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# extended LDIF
#
# LDAPv3
# base <> with scope baseObject
# filter: (objectclass=*)
# requesting: namingContexts
#

#
dn:
namingContexts: dc=hung,dc=mooo,dc=com

# search result
search: 2
result: 0 Success

# numResponses: 2

# numEntries: 1

(4) 利用LDIF文件添加数据
----------------------------------------------------------------------------------
LDAP中使用的数据格式是二进制格式,但是一个名为 LDAP Data Interchange Format(LDIF)
的ASCII序列化格式的文件, 可用于向LDAP数据库导出和导入数据. LDAP的LDIF文件必須使用
UTF-8編碼格式, 它的二进制数据被表示为base64编码.

OpenLDAP中包含系列工具:
ldapsearch        向LDAP服務器查詢的命令(用-L參數可将数据导出為LDIF文件);
ldapadd                将数据从LDIF导入到LDAP服务器;
ldapmodify        将LDIF中描述的一组更改应用到LDAP服务器(同ldapadd類似).
ldapdelete       

此外,LDIF是用于为 Mozilla Application Suite 和其他用户应用程序级别的工具导入和
导出地址簿数据的格式之一, 甚至Microsoft Windows 2000 Server和Windows Server 2003
也包含一个LDIF工具(LDIFDE),用于向 Active Directory 导入和导出数据。


【ldapadd】添加節點:

-x                表示密碼驗證;
-D <DN>                指定管理員的DN;
-w <PASSWORD>        指定管理員的密碼;
-W                提示輸入管理員的密碼;
-f <FILE>        指定LDIF文件路徑;
-H <URL>        指定LDAP服務器的URL(例如本機為 ldap://localhost/);

要向LDAP服务器手动添加信息,首先要创建一个LDIF文件. 必須注意LDAP是樹狀結構的數據庫,
每棵目錄樹都必須由一個根(root)項目開始,其后所添加的分支節點的dn就必須對應此根項目
的dn內容來擴展. 如果您所添加的節點還沒有根節點, 或者沒有上層的父節點,那么就不可能
添加成功的. 如下是一個LDIF范例文件,它定義了一個根節點:

vim /tmp/test.ldif;                #這是一個簡單的根定義;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dn:                dc=hung,dc=mooo,dc=com
objectClass:        top
objectClass:        dcObject
objectClass:        organization
o:                Testing Domain
description:        This is my testing Openldap
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 數據格式須正確才能加入(dn必須唯一). 可用如下命令將该文件導入到LDAP數據庫:

ldapadd -x -D "cn=admin,dc=hung,dc=mooo,dc=com" -W -f /tmp/test.ldif;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter LDAP Password:
adding new entry "dc=hung,dc=mooo,dc=com"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

vim /tmp/example.ldif;                #這是一個測試文件(在當前根下添加兩個子節點);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dn: dc=example,dc=hung,dc=mooo,dc=com
objectClass: dcObject
objectClass: organization
dc: example
o: Example Company
description: Corporation

dn: cn=Manager,dc=hung,dc=mooo,dc=com
objectClass: organizationalRole
cn: Manager
description: Directory Manager
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 上述范例原文是example.com,在這里用一个适合本站的域(example.hung.mooo.com)
來取代它。通常LDAP域层次结构和名称与熟悉的DNS名称中使用的层次结构和名称匹配.

ldapadd -x -D "cn=admin,dc=hung,dc=mooo,dc=com" -W -f /tmp/example.ldif;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter LDAP Password:
adding new entry "dc=example,dc=hung,dc=mooo,dc=com"
adding new entry "cn=Manager,dc=hung,dc=mooo,dc=com"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 上述導入過程需要寫入數據庫, 所以會提示您輸入管理員的密碼(用-W參數提示密碼),
這個密碼就是前述的slapd.conf中指定的rootpw值(Debian中好像不設置此值也一樣提示密碼).
另外,也可以用 -w password 連同密碼一起輸入命令中.


【ldapsearch】 測試查詢上述三個記錄:

ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' -s base '(objectclass=*)'
ldapsearch -x -b 'dc=example,dc=hung,dc=mooo,dc=com' -s base '(objectclass=*)'
ldapsearch -x -b 'cn=Manager,dc=hung,dc=mooo,dc=com' -s base '(objectclass=*)'

ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' '(objectclass=*)';            #顯示整棵樹;
ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' -s sub '(objectclass=*)'; #(同上);

ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' '(objectclass=*)' -L;        #按LDIF格式顯示;
ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' '(objectclass=*)' -LL;
ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' '(objectclass=*)' -LLL;


【ldapdelete】

ldapdelete "cn=Manager,dc=hung,dc=mooo,dc=com" -x -W \
-D "cn=admin,dc=hung,dc=mooo,dc=com";        #刪除節點;

ldapdelete -n "dc=example,dc=hung,dc=mooo,dc=com" -x -W \
-D "cn=admin,dc=hung,dc=mooo,dc=com";        #嘗試刪除節點但不是真正刪除;


【常見問題】

如果是在Windows上編好LDIF檔之後再傳到主機上的話可能會遇到每行后面多了^M字元的問題,
必須將這些字元清除,Unix/Linux系統下有個格式轉換工具dos2unix,可以達到這個目的,例如:

apt-get install tofrodos;        #需先安裝此套件;
dos2unix -b my.dos.ldif;        #參數-b指定備份原文件(所備份的文件為 原文件名.bak);

因為LDAP就規定了LDIF文件必須要使用UTF-8格式,所以如果您的LDIF是其他格式的文件,那就
必須先轉換成UTF-8格式,可以用iconv命令來做這件事,例如:

iconv -f big5 -t UTF-8 -o my.ldif.utf8 my.big5.ldif;
iconv -f gb2312 -t UTF-8 -o my.ldif.utf8 my.gb2312.ldif;

(5) OpenLDAP的應用(導入現有的帳號資料)
----------------------------------------------------------------------------------
對于網絡管理員來說,LDAP最經常的用途是作為多種服務器共同的帳號數據庫. OpenLDAP非常
適合于用來統籌管理帳號信息, 讓管理員只做一次增加,刪除和修改等動作, 就可應用于整個
組織.


【引入所需模式】

要讓LDAP目錄能儲存帳號信息,還必須引入(include)具有合適對象類別的模式(schema),例如
要想使用原本儲存于/etc/passwd和/etc/shadow文件中的帳號,就必須先引入nis.schema模式,
因為該模式提供了posixAccount和shadowAccount兩個對象類(objectClass),可用以滿足上述
要求. 此外, 如果還想要儲存全公司通用的用戶帳號, 你還需要cosine.schema模式所定義的
account對象類. 檢查并修正前述的/etc/ladp/slapd.conf配置文件,確認包含如下選項:

cat /etc/ldap/slapd.conf;        #帳號管理所需的模式等配置;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
include         /etc/ldap/schema/cosine.schema
include         /etc/ldap/schema/nis.schema
index           cn,uid eq
index           uidNumber eq
index           gidNumber eq
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(注: 可用egrep過濾查詢命令: cat /etc/ldap/slapd.conf | egrep '^index|^include';)
請注意: 上述三個索引(index)的作用是提高搜索速度.


【轉換現有的帳號數據】

PADL軟件公司提供了一組轉換工具,可從現有的/etc/passwd和/etc/shadow文件中取得數據并
將之轉換成LDIF格式的文件. 這個工具叫MigrationTools(一組Perl腳本),可從如下地址下載:
http://www.padl.com/OSS/MigrationTools.html

wget http://www.padl.com/download/MigrationTools.tgz;
tar zxvf MigrationTools.tgz;
cd MigrationTools-47/;

執行任何轉換任務之前,必須先編輯migrate_common.ph這個配置文件,以便將幾個重要的變數
設定成符合您的系統的實際情況. 需要修改的參數大概是如下幾個:

$DEFAULT_MAIL_DOMAIN        缺省的DNS域名;
$DEFAULT_BASE                缺省的基本域名(即根的域名);
$DEFAULT_MAIL_HOST        缺省的

vim ./migrate_common.ph;        #如下是適用于我的系統的設置;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$DEFAULT_MAIL_DOMAIN = "hung.mooo.com";
$DEFAULT_BASE = "dc=hung,dc=mooo,dc=com";
$DEFAULT_MAIL_HOST = "mail.hung.mooo.com";
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

./migrate_passwd.pl /etc/passwd /tmp/passwd.ldif;        #轉換passwd文件;
cat /tmp/passwd.ldif;                                        #
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dn: uid=root,ou=People,dc=hung,dc=mooo,dc=com
uid: root
cn: root
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}$1$pXy0ET57$QdbsSV0FGxcff80OjbZBQ/
shadowLastChange: 13814
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 0
gidNumber: 0
homeDirectory: /root
gecos: root

...(省略)...

dn: uid=testing,ou=People,dc=hung,dc=mooo,dc=com
uid: testing
cn: testing
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}cCiyk5hxetmRY
shadowLastChange: 13829
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 1002
gidNumber: 1002
homeDirectory: /home/testing
gecos: testing

...(省略)...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

從上述LDIF數據可看出,因為這些節點都是定義在ou=People這個節點下面的:

cat /tmp/passwd.ldif | grep 'dn:';        #看看所有需添加的節點的dn內容;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dn: uid=root,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=daemon,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=bin,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=sys,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=sync,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=games,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=man,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=lp,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=mail,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=news,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=uucp,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=proxy,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=www-data,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=backup,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=list,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=irc,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=gnats,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=nobody,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=Debian-exim,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=statd,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=identd,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=bind,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=sshd,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=named,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=apache,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=testing,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=openldap,ou=People,dc=hung,dc=mooo,dc=com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

如果在之前的操作中并未添加過這個節點的話, 那么就必須先產生這個節點. 當然您可以自己
手工來添加這個節點, 但是有一個更方便的做法, 就是利用MigrationTools這組工具中附帶的
一個Perl腳本來自動生成. 這個腳本及其使用方法請參考如下:

./migrate_base.pl > /tmp/people.ldif;
cat /tmp/people.ldif;                        #看看
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dn: dc=mooo,dc=com
dc: mooo
objectClass: top
objectClass: domain

dn: dc=hung,dc=mooo,dc=com
dc: hung
objectClass: top
objectClass: domain

...(省略)...

dn: ou=People,dc=hung,dc=mooo,dc=com
ou: People
objectClass: top
objectClass: organization**it

dn: ou=Group,dc=hung,dc=mooo,dc=com
ou: Group
objectClass: top
objectClass: organization**it

...(省略)...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

請注意: 在上述命令所創建的LDIF內容中,除了第一個和第二個節點通常可能因為系統中已經
存在或者格式不合, 因此不能導入LDAP數據庫之外,其他節點應該都是具有全新dn內容的節點.
如果是為了學習目的,建議您將上述LDIF文件的整個文件的內容都導入到LDAP數據庫中, 當然
如果您不愿意添加太多節點到您的LDAP數據庫中的話,也可以編輯上述文件的內容, 但至少要
包含其中的 ou=People,dc=hung,dc=mooo,dc=com 和 ou=Group,dc=hung,dc=mooo,dc=com 這
兩個節點,  因為這兩個節點就是下一個導入步驟(后面還需要轉換/etc/group文件)中將需要
的父節點. 導入命令參考如下:

ldapadd -x -D "cn=admin,dc=hung,dc=mooo,dc=com" -W -f /tmp/people.ldif;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter LDAP Password:
adding new entry "ou=People,dc=hung,dc=mooo,dc=com"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' '(objectclass=*)' -LL; #看看所有樹節點;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dn: dc=hung,dc=mooo,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: hung.mooo.com
dc: hung

dn: cn=admin,dc=hung,dc=mooo,dc=com
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator

dn: ou=People,dc=hung,dc=mooo,dc=com
ou: People
objectClass: top
objectClass: organization**it

dn: ou=Group,dc=hung,dc=mooo,dc=com
ou: Group
objectClass: top
objectClass: organization**it
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 上述資料顯示現在ou=People和ou=Group這兩個節點已經添加進入LDAP數據庫中了;

ldapadd -x -D "cn=admin,dc=hung,dc=mooo,dc=com" -W -f /tmp/passwd.ldif;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enter LDAP Password:
adding new entry "uid=root,ou=People,dc=hung,dc=mooo,dc=com"
adding new entry "uid=daemon,ou=People,dc=hung,dc=mooo,dc=com"
adding new entry "uid=bin,ou=People,dc=hung,dc=mooo,dc=com"
adding new entry "uid=sys,ou=People,dc=hung,dc=mooo,dc=com"
adding new entry "uid=sync,ou=People,dc=hung,dc=mooo,dc=com"
...(省略)...
adding new entry "uid=testing4,ou=People,dc=hung,dc=mooo,dc=com"
adding new entry "uid=openldap,ou=People,dc=hung,dc=mooo,dc=com"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

上述命令執行后的信息顯示,所有節點都添加成功了! 現在在看看LDAP數據庫的目錄樹:

ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' '(objectclass=*)' -LL | grep 'dn:';
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dn: dc=hung,dc=mooo,dc=com
dn: cn=admin,dc=hung,dc=mooo,dc=com
dn: ou=People,dc=hung,dc=mooo,dc=com
dn: uid=root,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=daemon,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=bin,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=sys,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=sync,ou=People,dc=hung,dc=mooo,dc=com
...(省略)...
dn: uid=testing4,ou=People,dc=hung,dc=mooo,dc=com
dn: uid=openldap,ou=People,dc=hung,dc=mooo,dc=com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

參考前述的轉換和導入步驟,將/etc/group文件也轉換并導入LDAP數據庫:
./migrate_group.pl /etc/group /tmp/group.ldif;
ldapadd -x -D "cn=admin,dc=hung,dc=mooo,dc=com" -W -f /tmp/group.ldif;

(6) 配置LDAP客戶端
----------------------------------------------------------------------------------
[1] 根據實際情況,您的LDAP客戶端可能需要安裝如下套件:

apt-get install ldap-utils;
apt-get install libldap2;
apt-get install libldap2-dev;
apt-get install libldap-2.3-0;


[2] 設置LDAP客戶端配置文件:

vim /etc/ldap/ldap.conf;                #調整(或添加)如下最后三行;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
HOST   10.10.0.1                        #這是新添加的行(其實這行可忽略);
BASE   dc=hung, dc=mooo, dc=com                #注意這里要修改(即指定根節點的dn);
URI    ldap://10.10.0.1/                #這里可設置多臺LDAP服務器,用空格隔開即可;

#添加如下三行,后面配置nss_ldap來使用LDAP服務時將要調用(若不用nss_ldap則可忽略);
nss_base_passwd ou=People,dc=hung,dc=mooo,dc=com
nss_base_shadow ou=People,dc=hung,dc=mooo,dc=com
nss_base_group  ou=Group,dc=hung,dc=mooo,dc=com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 通常預設的ldap.conf文件都將BASE和URI注釋掉, 但如要啟動LDAP客戶端,則一定要
設置BASE和URI參數. 尤其是URI參數更為重要, 如果您有多臺LDAP服務器(如Master和Slave),
那么也可以在URI中設置多個ldap參數,以空格分隔即可,例如:

URI    ldap://10.10.0.1/ ldap://10.10.1.1/        #這里設置了兩臺LDAP服務器;


[3] 現在可在客戶端測試LDAP服務器端的反應,例如在客戶端查詢LDAP服務器:

ldapsearch -x -b 'dc=hung,dc=mooo,dc=com' -h 10.10.0.1 '(objectclass=*)' -LL;
ldapsearch -x -b 'uid=testing4,ou=People,dc=hung,dc=mooo,dc=com' -LL;

如果執行上述命令之后,能夠返回LDAP服務器中的節點資料,說明LDAP客戶端已經成功連線并
能夠獲進行資料查詢了.
----------------------------------------------------------------------------------


----------------------------------------------------------------------------------
(7) 在LDAP客戶端配置nss_ldap來使用LDAP服務器提供認證服務
----------------------------------------------------------------------------------
LDAP的客戶端應用非常廣泛, 而且不同的應用須對應不同的目錄樹, 最后還必須依照客戶端
軟件的具體特點, 來將其數據源改為LDAP服務器的數據庫. 下面介紹一個例子, 演示了如何
在客戶端配置Linux系統的NSS設置, 即通過nss_ldap來使用LDAP服務器, 以提供用戶登錄等
驗證服務.

[1] 安裝nss_ldap套件

在配置NSS啟用LDAP服務之前,首先要安裝libnss-ldap套件. 可以參考如下幾種方法來安裝和
配置libnss-ldap套件(分別適用于Debian和RedHat,以及Tarbll資源).


【在Debian系統中使用套件管理工具來安裝libnss-ldap套件】

apt-get install libnss-ldap;        #此套件安裝過程會自動提問進行相關設置;

ls -l /etc/*nss*;        #看看同nss_ldap相關的配置文件(適用于Debian系統);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-rw-r--r-- 1 root root  /etc/libnss-ldap.conf        #這就是nss_ldap的配置文件了;
-rw------- 1 root root  /etc/libnss-ldap.secret        #這是登錄LDAP服務器的明文密碼;
-rw-r--r-- 1 root root  /etc/nsswitch.conf        #此文件需修改(參考后面步驟);
-rw-r--r-- 1 root root  /etc/nsswitch.ldap        #一個nsswitch.conf文件的樣本;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

vim /etc/libnss-ldap.conf;        #如下安裝過程自動配置的項目(適用于Debian系統);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
base dc=hung,dc=mooo,dc=com
uri ldap://10.10.0.1/
ldap_version 3
rootbinddn cn=admin,dc=hung,dc=mooo,dc=com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


【在RedHat系統中使用套件管理工具來安裝nss_ldap套件】

在RedHat系統中,通常默認應該已經安裝好了nss_ldap套件, 如果您的系統還未安裝,也可以
使用 yum 或相應的 rpm 命令來安裝此套件: yum install nss_ldap;                 

同Debian系統不同的是, 在RedHat系統中所有同認證相關的配置,都可以通過一個管理工具來
集中處理, 這個管理工具就是 authconfig(或authconfig-tui), 它是一個可以使用文本菜單
來進行各種配置操作的工具程式. 當然,為了學習目的, 這里還是介紹一下手工修改所需調整
的幾個配置文件(也可參考類似的NIS設置相關部分). 例如:

vim /etc/sysconfig/authconfig;        #(此文件只適用于RedHat系統);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
USELDAP=yes
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

cat /etc/ldap.conf | grep -v ^# | grep .; #RedHat的nss_ldap調用的是此文檔;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
base dc=hung,dc=mooo,dc=com
timelimit 120
bind_timelimit 120
idle_timelimit 3600
nss_initgroups_ignoreusers root,ldap,named,avahi,haldaemon
uri ldap://10.10.0.1/
ssl no
tls_cacertdir /etc/openldap/cacerts
pam_password md5
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: nss_ldap所使用的配置文件的路徑是/etc/ldap.conf, 這同LDAP客戶端的配置文件
/etc/openldap/ldap.conf路徑是不相同的(未知兩者是否可共用).

authconfig --test | grep nss_ldap -A 3;        #看看系統的授權情況;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
nss_ldap is enabled
LDAP+TLS is disabled
LDAP server = "ldap://10.10.0.1/"
LDAP base DN = "dc=hung,dc=mooo,dc=com"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


【使用Tarball資源來安裝nss_ldap套件】

也可以手工安裝nss_ldap套件(但必須自行設置相關配置文件及其參數):
wget http://www.padl.com/download/nss_ldap.tgz;
tar zxvf nss_ldap.tgz;
cd nss_ldap-259/;
./configure;
make;
make check;
make install;
(?采用Tarball資源安裝不適用于Debian系統?沒有相關配置文件?還是有bug?);


[2] 調整nsswitch.conf配置文件

在Linux系統中,可以通過NSS(/etc/nsswitch.conf)來設置各種應用的多種查詢來源, 因此也
可以通過NSS來設置您的系統查詢LDAP服務器的用戶資料,只需要在nsswitch.conf配置文件中
修改(或添加)查詢順序, 指定查詢時使用LDAP數據庫即可.

vim /etc/nsswitch.conf;                #調整passw,shadow和dgroup的查詢順序;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
passwd:         files ldap        #在后面加上ldap表示在第二順序啟用LDAP來驗證;
group:          files ldap        #在后面加上ldap表示在第二順序啟用LDAP來驗證;
shadow:         files ldap        #在后面加上ldap表示在第二順序啟用LDAP來驗證;

hosts:          files dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
請注意: 上述這個例子是針對我的Debian系統的, 在您的系統中可能內容不盡相同, 但基本上
應該是類似的,都是調整上述三個參數即可(例如原本的搜尋順序可能不止files一個).


[3] 現在可以測試看看能否從LDAP服務器中查詢用戶帳號:

getent passwd;        #正常情況下此命令應返回本機和LDAP服務器的大量帳號資料;
id testing4;        #顯示只在LDAP服務器中才具有的帳號,可確認LDAP服務器能否提供用戶資料;
----------------------------------------------------------------------------------


----------------------------------------------------------------------------------
(8) 在LDAP客戶端配置PAM來使用LDAP服務器提供認證服務
----------------------------------------------------------------------------------


【在Debian系統安裝libpam-ldap】

在Debian系統中使用套件管理工具來安裝libpam-ldap套件:
apt-get install libpam-ldap;        #此套件安裝過程會自動提問進行相關設置;

ll /lib/security/pam_ldap.so;        #安裝完成之后,就會添加這個庫模塊;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-rw-r--r-- 1 root root 37840 Mar 10  2007 /lib/security/pam_ldap.so
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

cat /etc/pam_ldap.conf;                #如下是按照時自動設置的參數;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
base dc=hung,dc=mooo,dc=com
uri ldap://10.10.0.1/
ldap_version 3
binddn cn=admin,dc=hung,dc=mooo,dc=com
bindpw 721115
pam_password crypt
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ll /etc/pam.d;                        #這是所有啟用了PAM認證的程式的相關設定;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-rw-r--r-- 1 root root  182 Jan  3  2006 atd
-rw-r--r-- 1 root root  384 Feb 27  2007 chfn
-rw-r--r-- 1 root root  581 Feb 27  2007 chsh
-rw-r--r-- 1 root root  392 Oct  4 06:23 common-account
-rw-r--r-- 1 root root  436 Oct  4 06:23 common-auth
-rw-r--r-- 1 root root 1097 Oct  4 06:23 common-password
-rw-r--r-- 1 root root  372 Oct  4 06:23 common-session
-rw-r--r-- 1 root root  289 Oct 14  2005 cron
-rw-r--r-- 1 root root 2843 Feb 27  2007 login
-rw-r--r-- 1 root root   38 Mar  8  2007 newrole
-rw-r--r-- 1 root root  520 Sep  1  2003 other
-rw-r--r-- 1 root root   92 Feb 27  2007 passwd
-rw-r--r-- 1 root root   38 Mar  8  2007 run_init
-rw-r--r-- 1 root root   69 May 30 17:53 samba
-rw-r--r-- 1 root root 1272 Mar  6  2007 ssh
-rw-r--r-- 1 root root 2305 Feb 27  2007 su

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
LDAP服务配置
Openldap学习笔记[转]-openldap介绍
Installing & Configuring OpenLDAP Server On CentOS 6.4 | RedHat / CentOS Linux Tutorials And How
LDAP基础讲座
OpenLDAP 最详细的概念教程
openldap
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服