粘一個早年學習ldap學習筆記,

yj11發表於2008-07-08

粘一個早年學習ldap學習筆記,

[@more@]粘一個早年學習ldap的自己整理的一些東西.

1)這時候啟動libexec/slapd程式,的相關日誌輸出:

2)向ldap中新增相關資料,檔案格式、命令和相應的日誌輸出:

3)再殺掉slapd程式時候的日誌輸出:

4)啟動slapd的時候使用-h指定繫結引數

5)ldapsearch的查詢過程和及相關的日誌輸出

6)透過/home/openldap/var/slapd.args檔案內容得知啟動slapd程式時所帶的引數。

7)使用sbin/slapdpasswd生成SSHA密碼。

8)把LDBM資料庫轉換成LDIF格式

9)使用ldapmodify對資料加進行修改

10client限制從ldap server查詢的entry數目

11)ldapsearch查詢匹配uid值及相應日誌

12)透過ldif檔案,刪除,新增,修改,替換相應屬性值

13)透過slapadd進行大量entry的新增

下面做的試驗是基於as2.1openldap-2.0.10版本來做的,安裝

./configure --prefix=/home/openldap --with-ldbm-api=gdbm

./make

./make intall

我測試機的sldap.conf需要新增如下:

include /usr/local/openldap/etc/openldap/schema/core.schema

include /usr/local/openldap/etc/openldap/schema/cosine.schema

include /usr/local/openldap/etc/openldap/schema/inetorgperson.schema

loglevel 256

database

ldbm

suffix "dc=myhome,dc=com"

rootdn "cn=root,dc=myhome,dc=com"

rootpw secret

directory /home/openlda-2.0.10/var/myhome

lastmod on

上為slapd.conf檔案的配置

還需要為openldap建立myhome目錄,以便可以保證相關的檔案。並且設定這個目錄的屬性為700

#mkdir /home/openld-2.0.10/var/myhome

#chmod 700 /home/openld-2.0.10/var/myhome

還需要為openldap設定日誌級別,這個需要設定系統的/etc/syslog.conf檔案,新增內容如下:

local4.* /var/log/ldap.log

儲存退出。

#touch /var/log/ldap.log

#/etc/init.d/syslog restart 重啟syslog服務

1)這時候啟動libexec/slapd程式,的相關日誌輸出:

#libexec/slapd

# ps -ef | grep slapd

root 20907 1 0 10:05 ? 00:00:00 libexec/slapd

root 20908 20907 0 10:05 ? 00:00:00 libexec/slapd

root 20909 20908 0 10:05 ? 00:00:00 libexec/slapd

root 20911 20908 0 10:05 ? 00:00:00 libexec/slapd

root 20912 20908 0 10:05 ? 00:00:00 libexec/slapd

root 20924 20749 0 10:14 pts/1 00:00:00 grep slapd

確認程式已經啟來,下面為slapd程式再啟動的時候輸出到ldap.log中的相應日誌資訊:

Feb 18 10:05:05 oradb slapd[20905]: daemon: socket() failed errno=97 (Address family not supported by protocol)

Feb 18 10:05:05 oradb slapd[20907]: slapd starting

2)ldap中新增相關資料,檔案格式、命令和相應的日誌輸出:

#more ff.ldif

dn: dc=myhome,dc=com

objectClass: dcObject

objectClass: organization

o: myhome

dc: myhome

description: this is myhome domain

dn: uid=bbb,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: bbb

sn: bbb

cn: bbb

telephoneNumber: 111-111-111

mail: bbb@myhome.com

dn: uid=ccc,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ccc

sn: ccc

cn: ccc

telephoneNumber: 222-111-111

mail: ccc@myhome.com

下面為向ldap資料庫中新增相關內容:

#bin/ldapadd -f ff.ldif -W -x -D 'cn=root,dc=myhome,dc=com'

Enter LDAP Password:

adding new entry "dc=myhome,dc=com"

adding new entry "uid=bbb,dc=myhome,dc=com"

adding new entry "uid=ccc,dc=myhome,dc=com"

下面為在新增相關資料的ldap.log的輸出內容:

Feb 18 10:05:58 oradb slapd[20909]: daemon: conn=0 fd=9 connection from IP=127.0.0.1:32770 (IP=0.0.0.0:34049) accepted.

Feb 18 10:05:58 oradb slapd[20911]: conn=0 op=0 BIND dn="CN=ROOT,DC=MYHOME,DC=COM" method=128

Feb 18 10:05:58 oradb slapd[20911]: conn=0 op=0 RESULT tag=97 err=0 text=

Feb 18 10:05:58 oradb slapd[20912]: conn=0 op=1 ADD dn="DC=MYHOME,DC=COM"

Feb 18 10:05:58 oradb slapd[20911]: conn=0 op=2 ADD dn="UID=BBB,DC=MYHOME,DC=COM"

Feb 18 10:05:58 oradb slapd[20912]: conn=0 op=1 RESULT tag=105 err=0 text=

Feb 18 10:05:58 oradb slapd[20912]: conn=0 op=3 ADD dn="UID=CCC,DC=MYHOME,DC=COM"

Feb 18 10:05:58 oradb slapd[20911]: conn=0 op=2 RESULT tag=105 err=0 text=

Feb 18 10:05:59 oradb slapd[20911]: conn=0 op=4 UNBIND

Feb 18 10:05:59 oradb slapd[20912]: conn=0 op=3 RESULT tag=105 err=0 text=

Feb 18 10:05:59 oradb slapd[20912]: conn=-1 fd=9 closed

3)再殺掉slapd程式時候的日誌輸出:

#killall slapd

相關的ldap.log日誌輸出如下:

Feb 18 10:20:09 oradb slapd[20909]: slapd shutdown: waiting for 0 threads to terminate

Feb 18 10:20:09 oradb slapd[20907]: slapd stopped.

4)啟動slapd的時候使用-h指定繫結引數

#libexec/slapd -h ldap://127.0.0.1:9009/

# netstat -anp | grep 9009

tcp 0 0 127.0.0.1:9009 0.0.0.0:* LISTEN 20980/slapd

# lsof -i:9009

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

slapd 20980 root 6u IPv4 40630 TCP localhost.localdomain:9009 (LISTEN)

slapd 20981 root 6u IPv4 40630 TCP localhost.localdomain:9009 (LISTEN)

slapd 20982 root 6u IPv4 40630 TCP localhost.localdomain:9009 (LISTEN)

相關的ldap.log日誌輸出如下:

Feb 18 10:22:50 oradb slapd[20980]: slapd starting

5)在slapd使用-h繫結引數後,再使用ldapadd新增相應的資料.

# bin/ldapadd -f ff.ldif -W -x -D 'cn=root,dc=myhome,dc=com' -h ldap://127.0.0.1:9009

Enter LDAP Password:

ldap_bind: Can't contact LDAP server

開始的時候使用-h指定ldap繫結的URL進行新增,提示無法連線LDAP伺服器。

# ps -ef | grep slapd

root 20980 1 0 10:22 ? 00:00:00 libexec/slapd -h ldap://127.0.0.

root 20981 20980 0 10:22 ? 00:00:00 libexec/slapd -h ldap://127.0.0.

root 20982 20981 0 10:22 ? 00:00:00 libexec/slapd -h ldap://127.0.0.

root 20997 20749 0 10:27 pts/1 00:00:00 grep slapd

# bin/ldapadd -f ff.ldif -W -x -D 'cn=root,dc=myhome,dc=com' -H ldap://127.0.0.1:9009

Enter LDAP Password:

adding new entry "dc=myhome,dc=com"

ldap_add: Already exists

ldif_record() = 68

相關的ldap.log日誌輸出如下,因為提示Alread exists所以這次新增是不成功:

Feb 18 10:28:08 oradb slapd[20982]: daemon: conn=0 fd=9 connection from IP=127.0.0.1:32772 (IP=127.0.0.1:12579) accepted.

Feb 18 10:28:08 oradb slapd[20999]: conn=0 op=0 BIND dn="CN=ROOT,DC=MYHOME,DC=COM" method=128

Feb 18 10:28:08 oradb slapd[20999]: conn=0 op=0 RESULT tag=97 err=0 text=

Feb 18 10:28:08 oradb slapd[21000]: conn=0 op=1 ADD dn="DC=MYHOME,DC=COM"

Feb 18 10:28:08 oradb slapd[21000]: conn=0 op=1 RESULT tag=105 err=68 text=

Feb 18 10:28:08 oradb slapd[20999]: conn=0 op=2 UNBIND

Feb 18 10:28:08 oradb slapd[20999]: conn=-1 fd=9 closed

ff.ldif檔案如下所示:

dn: uid=aaa,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: aaa

sn: aaa

cn: aaa

telephoneNumber: 111-111-111

mail: aaa@myhome.com

dn: uid=ddd,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ddd

sn: ddd

cn: ddd

telephoneNumber: 222-111-111

mail: ddd@myhome.com

# bin/ldapadd -f ff.ldif -W -x -D 'cn=root,dc=myhome,dc=com' -H ldap://127.0.0.1:9009

Enter LDAP Password:

adding new entry "uid=aaa,dc=myhome,dc=com"

adding new entry "uid=ddd,dc=myhome,dc=com"

提示已經新增成功,下面為相關的新增成功的日誌記錄。

Feb 18 10:28:28 oradb slapd[20982]: daemon: conn=1 fd=9 connection from IP=127.0.0.1:32773 (IP=127.0.0.1:12579) accepted.

Feb 18 10:28:28 oradb slapd[21000]: conn=1 op=0 BIND dn="CN=ROOT,DC=MYHOME,DC=COM" method=128

Feb 18 10:28:28 oradb slapd[21000]: conn=1 op=0 RESULT tag=97 err=0 text=

Feb 18 10:28:28 oradb slapd[20999]: conn=1 op=1 ADD dn="UID=AAA,DC=MYHOME,DC=COM"

Feb 18 10:28:28 oradb slapd[20999]: conn=1 op=1 RESULT tag=105 err=0 text=

Feb 18 10:28:28 oradb slapd[21000]: conn=1 op=2 ADD dn="UID=DDD,DC=MYHOME,DC=COM"

Feb 18 10:28:28 oradb slapd[20999]: conn=1 op=3 UNBIND

Feb 18 10:28:28 oradb slapd[21000]: conn=1 op=2 RESULT tag=105 err=0 text=

Feb 18 10:28:28 oradb slapd[21000]: conn=-1 fd=9 closed

上面的-h-H兩個引數的解釋如下:

-h host LDAP server

-H URI LDAP Uniform Resource Indentifier(s)

5)ldapsearch的查詢過程和及相關的日誌輸出

#bin/ldapsearch -LLL -b 'dc=myhome,dc=com' -W -x

Enter LDAP Password:

dn: dc=myhome,dc=com

objectClass: dcObject

objectClass: organization

o: myhome

dc: myhome

description: this is myhome domain

dn: uid=bbb,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: bbb

sn: bbb

cn: bbb

telephoneNumber: 111-111-111

mail: bbb@myhome.com

dn: uid=ccc,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ccc

sn: ccc

cn: ccc

telephoneNumber: 222-111-111

mail: ccc@myhome.com

dn: uid=aaa,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: aaa

sn: aaa

cn: aaa

telephoneNumber: 111-111-111

mail: aaa@myhome.com

dn: uid=ddd,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ddd

sn: ddd

cn: ddd

telephoneNumber: 222-111-111

mail: ddd@myhome.com

相關的日誌輸出如下:

Feb 18 10:48:39 oradb slapd[21021]: daemon: conn=0 fd=9 connection from IP=127.0.0.1:32774 (IP=0.0.0.0:34049) accepted.

Feb 18 10:48:39 oradb slapd[21034]: conn=0 op=0 BIND dn="" method=128

Feb 18 10:48:39 oradb slapd[21034]: conn=0 op=0 RESULT tag=97 err=0 text=

Feb 18 10:48:39 oradb slapd[21035]: conn=0 op=1 SRCH base="dc=myhome,dc=com" scope=2 filter="(objectClass=*)"

Feb 18 10:48:39 oradb slapd[21035]: conn=0 op=1 SEARCH RESULT tag=101 err=0 text=

Feb 18 10:48:39 oradb slapd[21034]: conn=0 op=2 UNBIND

Feb 18 10:48:39 oradb slapd[21034]: conn=-1 fd=9 closed

上面是查詢dc=myhome,dc=com下面的所有項

下面為只查詢一個單項的所有內容。

#bin/ldapsearch -LLL -b 'uid=ccc,dc=myhome,dc=com' -W -x

Enter LDAP Password:

dn: uid=ccc,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ccc

sn: ccc

cn: ccc

telephoneNumber: 222-111-111

mail: ccc@myhome.com

下面為相關查詢日誌的輸出:

Feb 18 10:51:33 oradb slapd[21021]: daemon: conn=2 fd=9 connection from IP=127.0.0.1:32776 (IP=0.0.0.0:34049) accepted.

Feb 18 10:51:33 oradb slapd[21034]: conn=2 op=0 BIND dn="" method=128

Feb 18 10:51:33 oradb slapd[21034]: conn=2 op=0 RESULT tag=97 err=0 text=

Feb 18 10:51:33 oradb slapd[21035]: conn=2 op=1 SRCH base="uid=ccc,dc=myhome,dc=com" scope=2 filter="(objectClass=*)"

Feb 18 10:51:33 oradb slapd[21035]: conn=2 op=1 SEARCH RESULT tag=101 err=0 text=

Feb 18 10:51:33 oradb slapd[21034]: conn=2 op=2 UNBIND

Feb 18 10:51:33 oradb slapd[21034]: conn=-1 fd=9 closed

# bin/ldapsearch -LLL -b 'uid=ccc,dc=myhome,dc=com' -W -x -A

Enter LDAP Password:

dn: uid=ccc,dc=myhome,dc=com

objectClass:

uid:

sn:

cn:

telephoneNumber:

mail:

-A選項引數查詢所有項,返回屬性值,但不返回他們的值。這個命令的輸出日誌與上面的輸出類似,沒有特別的地方。

6)透過/home/openldap/var/slapd.args檔案內容得知啟動slapd程式時所帶的引數。

# ../libexec/slapd -h ldap://127.0.0.1:9009/

# ls -l

total 20

drwx------ 2 root root 4096 Feb 18 10:05 myhome

drwx------ 2 root root 4096 Feb 18 09:36 openldap-ldbm

drwx------ 2 root root 4096 Feb 18 09:36 openldap-slurp

-rw-r--r-- 1 root root 44 Feb 19 11:07 slapd.args

-rw-r--r-- 1 root root 4 Feb 19 11:07 slapd.pid

# more slapd.args

../libexec/slapd -h ldap://127.0.0.1:9009/

# pwd

/home/openlda-2.0.10/var

#

Slapd.pid檔案記錄的是當前slapd執行的pid值。

7)使用sbin/slapdpasswd生成SSHA密碼。

使用slapdpasswd命令生成加密密碼,新增到slapd.conf檔案當中

# sbin/slappasswd

New password:

Re-enter new password:

{SSHA}xngCMl1VTdywMSGlERVBg1wulbsyLE8I

# vi etc/openldap/slapd.conf

database

ldbm

suffix "dc=myhome,dc=com"

rootdn "cn=root,dc=myhome,dc=com"

#rootpw secret

rootpw {SSHA}xngCMl1VTdywMSGlERVBg1wulbsyLE8I

directory /home/openlda-2.0.10/var/myhome

lastmod on

# libexec/slapd 啟動slapd程式,新增相應的ldif檔案

# bin/ldapadd -W -x -D 'cn=root,dc=myhome,dc=com' -f ff.ldif

Enter LDAP Password:

adding new entry "uid=a1,dc=myhome,dc=com"

adding new entry "uid=a2,dc=myhome,dc=com"

#

8)把LDBM資料庫轉換成LDIF格式

sbin/slapcat -n /home/openldap-2.0.10/var/myhome/id2entry.dbb > oo.ldif

詳細的slapcat請看man手冊

9)使用ldapmodify對資料加進行修改

下面是已經新增好的資料,透過ldapsearch進行相關的查詢輸出

# bin/ldapsearch -LLL -b 'dc=gogo,dc=com' -W -x

Enter LDAP Password:

dn: dc=gogo,dc=com

objectClass: dcObject

objectClass: organization

o: gogo

dc: gogo

telephoneNumber: 110-110

postalCode: 0451

description: this is gogo domain

dn: cn=aaa,dc=gogo,dc=com

objectClass: Person

objectClass: inetOrgPerson

sn: aaa

cn: aaa

telephoneNumber: 111-111-111

mail: aaa@gogo.com

dn: cn=bbb,dc=gogo,dc=com

objectClass: Person

objectClass: inetOrgPerson

sn: bbb

cn: bbb

telephoneNumber: 222-111-111

mail: bbb@gogo.com

下面的ldif是準備要修改的檔案內容

# more mod.ldif

dn: cn=bbb,dc=gogo,dc=com

changetype: modify

add: homePhone

homePhone: 112-233

#bin/ldapmodify -D 'cn=root,dc=gogo,dc=com' -f ./mod.ldif -W -x

Enter LDAP Password:

modifying entry "cn=bbb,dc=gogo,dc=com"

透過ldapsearch查詢,下面標紅色的是已經新增修改生效的部分:

# bin/ldapsearch -LLL -b 'dc=gogo,dc=com' -W -x

Enter LDAP Password:

dn: dc=gogo,dc=com

objectClass: dcObject

objectClass: organization

o: gogo

dc: gogo

telephoneNumber: 110-110

postalCode: 0451

description: this is gogo domain

dn: cn=aaa,dc=gogo,dc=com

objectClass: Person

objectClass: inetOrgPerson

sn: aaa

cn: aaa

telephoneNumber: 111-111-111

mail: aaa@gogo.com

dn: cn=bbb,dc=gogo,dc=com

objectClass: Person

objectClass: inetOrgPerson

sn: bbb

cn: bbb

telephoneNumber: 222-111-111

mail: bbb@gogo.com

homePhone: 112-233

ldapmodify修改相應記錄的日誌為:

Feb 19 07:04:34 vm-252 slapd[1840]: daemon: conn=14 fd=9 connection from IP=127.0.0.1:1083 (IP=0.0.0.0:34049) accepted.

Feb 19 07:04:34 vm-252 slapd[1849]: conn=14 op=0 BIND dn="CN=ROOT,DC=GOGO,DC=COM" method=128

Feb 19 07:04:34 vm-252 slapd[1849]: conn=14 op=0 RESULT tag=97 err=0 text=

Feb 19 07:04:34 vm-252 slapd[1844]: conn=14 op=1 MOD dn="cn=bbb,dc=gogo,dc=com"

Feb 19 07:04:34 vm-252 slapd[1844]: conn=14 op=1 RESULT tag=103 err=0 text=

Feb 19 07:04:34 vm-252 slapd[1848]: conn=14 op=2 UNBIND

Feb 19 07:04:34 vm-252 slapd[1848]: conn=-1 fd=9 closed

===============================================================

10client限制從ldap server查詢的entry數目

[root@oradb openlda-2.0.10]# bin/ldapsearch -b 'dc=myhome,dc=com' -W -x -LLL

Enter LDAP Password:

dn: dc=myhome,dc=com

objectClass: dcObject

objectClass: organization

o: myhome

dc: myhome

description: this is myhome domain

dn: uid=bbb,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: bbb

sn: bbb

cn: bbb

telephoneNumber: 111-111-111

mail: bbb@myhome.com

dn: uid=ccc,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ccc

sn: ccc

cn: ccc

telephoneNumber: 222-111-111

mail: ccc@myhome.com

dn: uid=aaa,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: aaa

sn: aaa

cn: aaa

telephoneNumber: 111-111-111

mail: aaa@myhome.com

dn: uid=ddd,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ddd

sn: ddd

cn: ddd

telephoneNumber: 222-111-111

mail: ddd@myhome.com

dn: uid=ppp,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ppp

sn: ppp

cn: ppp

telephoneNumber: 111-111-111

mail: ppp@myhome.com

dn: uid=ttt,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ttt

sn: ttt

cn: ttt

telephoneNumber: 222-111-111

mail: ttt@myhome.com

dn: uid=a1,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: a1

sn: a1

cn: a1

telephoneNumber: 111-111-111

mail: a1@myhome.com

dn: uid=a2,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: a2

sn: a2

cn: a2

telephoneNumber: 222-111-111

mail: a2@myhome.com

正常查詢結果如上所示,下面使用-z 引數限制返回的匹配數目。

[root@oradb openlda-2.0.10]# bin/ldapsearch -b 'dc=myhome,dc=com' -W -x -LLL -z 3

Enter LDAP Password:

dn: dc=myhome,dc=com

objectClass: dcObject

objectClass: organization

o: myhome

dc: myhome

description: this is myhome domain

dn: uid=bbb,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: bbb

sn: bbb

cn: bbb

telephoneNumber: 111-111-111

mail: bbb@myhome.com

dn: uid=ccc,dc=myhome,dc=com

objectClass: Person

objectClass: inetOrgPerson

uid: ccc

sn: ccc

cn: ccc

telephoneNumber: 222-111-111

mail: ccc@myhome.com

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1806527/viewspace-1006988/,如需轉載,請註明出處,否則將追究法律責任。

相關文章