
- DB2 教程
- 首頁
- DB2 - 簡介
- DB2 - 伺服器安裝
- DB2 - 例項
- DB2 - 資料庫
- DB2 - 緩衝池
- DB2 - 表空間
- DB2 - 儲存組
- DB2 - 模式
- DB2 - 資料型別
- DB2 - 表
- DB2 - 別名
- DB2 - 約束
- DB2 - 索引
- DB2 - 觸發器
- DB2 - 序列
- DB2 - 檢視
- DB2 與 XML
- DB2 - 備份和恢復
- DB2 - 資料庫安全
- DB2 - 角色
- DB2 - LDAP
- DB2 有用資源
- DB2 - 問答
- DB2 - 快速指南
- DB2 - 有用資源
- DB2 - 討論
DB2 - LDAP
介紹
LDAP 是輕量級目錄訪問協議。LDAP 是一種全域性目錄服務,行業標準協議,基於客戶端-伺服器模型,執行在 TCP/IP 協議棧之上。LDAP 提供了一種連線、訪問、修改和搜尋網際網路目錄的工具。
LDAP 伺服器包含以目錄樹形式組織的資訊。客戶端請求伺服器提供資訊或對特定資訊執行某些操作。伺服器透過提供所需資訊(如果存在)來響應客戶端,或者將客戶端引用到另一個伺服器以對所需資訊進行操作。然後,客戶端從另一個伺服器獲取所需資訊。
目錄的樹狀結構在所有參與的伺服器上保持一致。這是 LDAP 目錄服務的突出特性。因此,無論客戶端引用哪個伺服器,客戶端始終都能以無錯誤的方式獲取所需資訊。在這裡,我們使用 LDAP 來認證 IBM DB2 作為作業系統認證的替代。
LDAP 有兩種型別
- 透明的
- 外掛
讓我們看看如何配置透明的 LDAP。
配置透明 LDAP
要開始配置透明 LDAP,您需要配置 LDAP 伺服器。
LDAP 伺服器配置
建立一個 SLAPD.conf 檔案,其中包含有關 LDAP 中使用者和組物件的所有資訊。當您安裝 LDAP 伺服器時,預設情況下它會在您的機器上配置基本的 LDAP 目錄樹。
下表顯示了修改後的檔案配置。
程式碼框中用黃色突出顯示的文字表示以下內容
DBA 使用者 ID = “db2my1”,組 = “db1my1adm”,密碼 = “db2my1” 管理員使用者 ID = “my1adm”,組 = “dbmy1ctl”。
# base dn: example.com dn: dc=example,dc=com dc: example o: example objectClass: organization objectClass: dcObject # pc box db dn: dc=db697,dc=example,dc=com dc: db697 o: db697 objectClass: organization objectClass: dcObject # # Group: dbadm # dn: cn=dbmy1adm,dc=db697,dc=example,dc=com cn: dbmy1adm objectClass: top objectClass: posixGroup gidNumber: 400 objectClass: groupOfNames member: uid=db2my1,cn=dbmy1adm,dc=db697,dc=example,dc=com memberUid: db2my1 # # User: db2 # dn: uid=db2my1,cn=dbmy1adm,dc=db697,dc=example,dc=com cn: db2my1 sn: db2my1 uid: db2my1 objectClass: top objectClass: inetOrgPerson objectClass: posixAccount uidNumber: 400 gidNumber: 400 loginShell: /bin/csh homeDirectory: /db2/db2my1 # # Group: db ctl # dn: cn=dbmy1ctl,dc=db697,dc=example,dc=com cn: dbmy1ctl objectClass: top objectClass: posixGroup gidNumber: 404 objectClass: groupOfNames member: uid=my1adm,cn=dbmy1adm,dc=db697,dc=example,dc=com memberUid: my1adm # # User: adm # dn: uid=my1adm,cn=dbmy1ctl,dc=db697,dc=example,dc=com cn: my1adm sn: my1adm uid: my1adm objectClass: top objectClass: inetOrgPerson objectClass: posixAccount uidNumber: 404 gidNumber: 404 loginShell: /bin/csh homeDirectory: /home/my1adm
將上述檔案儲存為“/var/lib/slapd.conf”,然後執行以下命令將這些值新增到 LDAP 伺服器中。這是一個 Linux 命令;不是 db2 命令。
ldapadd r- -D ‘cn=Manager,dc=example,dc=com” –W –f /var/lib/slapd.conf
在 LDAP 伺服器上註冊 DB2 使用者和 DB2 組後,登入到您已安裝例項和資料庫的特定使用者。您需要配置 LDAP 客戶端以確認客戶端伺服器所在的位置,無論是遠端還是本地。
LDAP 客戶端配置
LDAP 客戶端配置儲存在“ldap.conf”檔案中。有兩個檔案可用於配置引數,一個是通用的,另一個是特定的。您應該在“/etc/ldap.conf”中找到第一個檔案,第二個檔案位於“/etc/openldap/ldap.conf”中。
以下資料在通用 LDAP 客戶端配置檔案中可用
# File: /etc/ldap.conf # The file contains lots of more entries and many of them # are comments. You show only the interesting values for now host localhost base dc=example,dc=com ldap_version 3 pam_password crypt pam_filter objectclass=posixAccount nss_map_attribute uniqueMember member nss_base_passwd dc=example,dc=com nss_base_shadow dc=example,dc=com nss_base_group dc=example,dc=com
您需要根據 DB2 配置更改伺服器和域資訊的位置。如果我們在同一系統中使用伺服器,則在“host”中將其指定為“localhost”,在“base”中,您可以配置在 LDAP 伺服器的“SLAPD.conf”檔案中提到的內容。
可插拔身份驗證模組 (PAM) 是用於身份驗證服務的 API。這是 LDAP 身份驗證與加密密碼和型別為 posixAccount 的特殊 LDAP 物件的通用介面。此型別的所有 LDAP 物件都表示具有可移植作業系統介面 (POSIX) 屬性的帳戶的抽象。
網路安全服務 (NSS) 是一組庫,用於支援安全啟用客戶端和伺服器應用程式的跨平臺開發。這包括 SSL、TLS、PKCS S/MIME 和其他安全標準等庫。
您需要為該介面指定基本 DN 和兩個附加對映屬性。OpenLDAP 客戶端配置檔案包含以下條目
host localhost base dc=example,dc=com
到目前為止,您只是定義了 LDAP 伺服器的主機和基本 DN。
驗證 OpenLDAP 環境
配置完 LDAP 伺服器和 LDAP 客戶端後,驗證兩者之間的通訊。
步驟 1:檢查您的本地 LDAP 伺服器是否正在執行。使用以下命令
ps -ef | grep -i ldap
此命令應列出表示您的 LDAP 伺服器的 LDAP 守護程式
/usr/lib/openldap/slapd -h ldap:/// -u ldap -g ldap -o slp=on
這表示您的 LDAP 伺服器正在執行並正在等待來自客戶端的請求。如果之前命令沒有此類程序,您可以使用“rcldap”命令啟動 LDAP 伺服器。
rcldap start
伺服器啟動後,您可以在“/var/log/messages/”檔案中監視它,方法是執行以下命令。
tail –f /var/log/messages
使用 ldapsearch 測試與 LDAP 伺服器的連線
ldapsearch 命令開啟到 LDAP 伺服器的連線,繫結到它並執行搜尋查詢,可以使用特殊引數“-x”透過使用 –x 引數而不是更復雜的機制(如簡單身份驗證和安全層 (SASL))來連線到您的 LDAP 伺服器,使用簡單的身份驗證機制。
ldapsearch –x
LDAP 伺服器應回覆以下響應,其中包含 LDAP 資料交換格式 (LDIF) 中的所有 LDAP 條目。
# extended LDIF # # LDAPv3 # base <> with scope subtree # filter: (objectclass=*) # requesting: ALL # example.com dn: dc=example, dc=com dc: example o: example objectClass: organization objectClass: dcObject # search result search: 2 result: 0 Success # numResponses: 2 # numEntries: 1
配置 DB2
在使用 LDAP 伺服器和客戶端之後,您需要配置我們的 DB2 資料庫以與 LDAP 一起使用。讓我們討論一下,如何安裝和配置您的資料庫以使用我們的 LDAP 環境進行 DB2 使用者身份驗證過程。
配置 DB2 和 LDAP 互動外掛
IBM 提供了一個包含 LDAP 外掛的免費 DB2 軟體包。DB2 軟體包包括以下每個的三個 DB2 安全外掛
- 伺服器端身份驗證
- 客戶端身份驗證
- 組查詢
根據您的需求,您可以使用這三個外掛中的任何一個或全部。此外掛不支援某些使用者在 LDAP 中定義而其他使用者在作業系統中定義的環境。如果您決定使用 LDAP 外掛,則需要在 LDAP 伺服器中定義與資料庫關聯的所有使用者。相同的原則適用於組外掛。
您必須決定哪些外掛對我們的系統是強制性的。客戶端身份驗證外掛用於使用者 ID 和 CONNECT 或 ATTACH 語句中提供的密碼驗證發生在客戶端系統上的場景中。因此,資料庫管理器配置引數 SRVCON_AUTH 或 AUTHENTICATION 需要設定為 CLIENT 值。客戶端身份驗證難以保證安全,通常不建議使用。伺服器外掛通常建議使用,因為它執行使用者 ID 和密碼的伺服器端驗證,如果客戶端執行 CONNECT 或 ATTACH 語句,這是安全的方式。伺服器外掛還提供了一種將 LDAP 使用者 ID 對映到 DB2 授權 ID 的方法。
現在您可以開始安裝和配置 DB2 安全外掛,您需要考慮 DB2 所需的目錄資訊樹。DB2 使用間接授權,這意味著使用者屬於一個組,並且該組被授予了較少的許可權。您需要在 LDAP 目錄中定義所有 DB2 使用者和 DB2 組。
圖片
LDIF 檔案 openldap.ldif 應包含以下程式碼
# # LDAP root object # example.com # dn: dc=example, dc=com dc: example o: example objectClass: organization objectClass: dcObject # # db2 groups # dn: cn=dasadm1,dc=example,dc=com cn: dasadm1 objectClass: top objectClass: posixGroup gidNumber: 300 objectClass: groupOfNames member: uid=dasusr1,cn=dasadm1,dc=example,dc=com memberUid: dasusr1 dn: cn=db2grp1,dc=example,dc=com cn: db2grp1 objectClass: top objectClass: posixGroup gidNumber: 301 objectClass: groupOfNames member: uid=db2inst2,cn=db2grp1,dc=example,dc=com memberUid: db2inst2 dn: cn=db2fgrp1,dc=example,dc=com cn: db2fgrp1 objectClass: top objectClass: posixGroup gidNumber: 302 objectClass: groupOfNames member: uid=db2fenc1,cn=db2fgrp1,dc=example,dc=com memberUid: db2fenc1 # # db2 users # dn: uid=dasusr1, cn=dasadm1, dc=example,dc=com cn: dasusr1 sn: dasusr1 uid: dasusr1 objectClass: top objectClass: inetOrgPerson objectClass: posixAccount uidNumber: 300 gidNumber: 300 loginShell: /bin/bash homeDirectory: /home/dasusr1 dn: uid=db2inst2,cn=db2grp1,dc=example,dc=com cn: db2inst2 sn: db2inst2 uid: db2inst2 objectClass: top objectClass: inetOrgPerson objectClass: posixAccount uidNumber: 301 gidNumber: 301 loginShell: /bin/bash homeDirectory: /home/db2inst2 dn: uid=db2fenc1,cn=db2fgrp1,dc=example,dc=com cn: db2fenc1 sn: db2fenc1 uid: db2fenc1 objectClass: top objectClass: inetOrgPerson objectClass: posixAccount uidNumber: 303 gidNumber: 303 loginShell: /bin/bash homeDirectory: /home/db2fenc1
建立一個名為“db2.ldif”的檔案並將上述示例貼上到其中。使用此檔案,將定義的結構新增到您的 LDAP 目錄中。
要將 DB2 使用者和 DB2 組新增到 LDAP 目錄,您需要將使用者繫結為“rootdn”到 LDAP 伺服器以獲得確切的許可權。
執行以下語法以使用我們定義在 LDIF 檔案“db2.ldif”中的所有物件填充 LDAP 資訊目錄
ldapadd –x –D “cn=Manager, dc=example,dc=com” –W –f <path>/db2.ldif
使用更多引數執行搜尋結果
ldapsearch –x |more
準備用於 DB2 的檔案系統
為我們的 LDAP 使用者 db2inst2 建立例項。此使用者需要一個主目錄,其中包含兩個空檔案。在建立新例項之前,您需要建立一個將成為例項所有者的使用者。
建立例項使用者後,您應該在使用者主目錄中建立“ .profile”和“.login”檔案,這些檔案將由 DB2 修改。要在目錄中建立此檔案,請執行以下命令
mkdir /home/db2inst2 mkdir /home/db2inst2/.login mkdir /home/db2inst2/.profile
您已在 LDAP 目錄中註冊了所有與 DB2 相關的使用者和組,現在您可以使用例項所有者 ID“db2inst2”建立名為“db2inst2”的例項,並使用用於執行使用者定義函式 (UDF) 或儲存過程的隔離使用者 ID“db2fenc1”。
/opt/ibm/db2/V10.1/instance/db2icrt –u db2fenc1 db2inst2 DBI1070I Program db2icrt completed successfully.
現在檢查例項主目錄。您可以看到一個名為“sqllib”的新子目錄以及為 DB2 使用而定製的“.profile”和“.login”檔案。
配置用於 LDAP 支援的 DB2 身份驗證公共外掛
將所需的 LDAP 外掛複製到相應的 DB2 目錄
cp // /v10/IBMLDAPauthserver.so /home/db2inst2/sqllib/security /plugin/server/. cp / / /v10/IBMLDAPgroups.so /home/db2inst2/sqllib/security /plugin/group/.
將外掛複製到指定目錄後,您需要登入到 DB2 例項所有者並更改資料庫管理器配置以使用這些外掛。
Su – db2inst2 db2inst2> db2 update dbm cfg using svrcon_pw_plugin IBMLDAPauthserver db2inst2> db2 update dbm cfg using group_plugin IBMLDAPgroups db2inst2> db2 update dbm cfg using authentication SERVER_ENCRYPT db2inst2> db2stop db2inst2> db2start
此修改在您啟動 DB2 例項後生效。重新啟動例項後,您需要安裝和配置名為“IBMLDAPSecurity.ini”的主要 DB2 LDAP 配置檔案以使 DB2 外掛與當前 LDAP 配置一起使用。
IBMLDAPSecurity.ini 檔案包含
;----------------------------------------------------------- ; SERVER RELATED VALUES ;----------------------------------------------------------- ; Name of your LDAP server(s). ; This is a space separated list of LDAP server addresses, ; with an optional port number for each one: ; host1[:port] [host2:[port2] ... ] ; The default port number is 389, or 636 if SSL is enabled. LDAP_HOST = my.ldap.server ;----------------------------------------------------------- ; USER RELATED VALUES ;----------------------------------------------------------- rs ; LDAP object class used for use USER_OBJECTCLASS = posixAccount ; LDAP user attribute that represents the "userid" ; This attribute is combined with the USER_OBJECTCLASS and ; USER_BASEDN (if specified) to construct an LDAP search ; filter when a user issues a DB2 CONNECT statement with an ; unqualified userid. For example, using the default values ; in this configuration file, (db2 connect to MYDB user bob ; using bobpass) results in the following search filter: OrgPerson)(uid=bob) ; &(objectClass=inet USERID_ATTRIBUTE = uid representing the DB2 authorization ID ; LDAP user attribute, AUTHID_ATTRIBUTE = uid ;----------------------------------------------------------- ; GROUP RELATED VALUES ;----------------------------------------------------------- ps ; LDAP object class used for grou GROUP_OBJECTCLASS = groupOfNames at represents the name of the group ; LDAP group attribute th GROUPNAME_ATTRIBUTE = cn ; Determines the method used to find the group memberships ; for a user. Possible values are: ; SEARCH_BY_DN - Search for groups that list the user as ; a member. Membership is indicated by the ; group attribute defined as ; GROUP_LOOKUP_ATTRIBUTE. ; USER_ATTRIBUTE - A user's groups are listed as attributes ; of the user object itself. Search for the ; user attribute defined as TRIBUTE to get the groups. ; GROUP_LOOKUP_AT GROUP_LOOKUP_METHOD = SEARCH_BY_DN ; GROUP_LOOKUP_ATTRIBUTE ; Name of the attribute used to determine group membership, ; as described above. llGroups ; GROUP_LOOKUP_ATTRIBUTE = ibm-a GROUP_LOOKUP_ATTRIBUTE = member
現在在當前例項目錄中找到檔案 IBMLDAPSecurity.ini 檔案。將上述示例內容複製到其中。
Cp //db2_ldap_pkg/IBMLDAPSecurity.ini /home/db2inst2/sqllib/cfg/
現在您需要重新啟動 DB2 例項,使用以下兩個語法
db2inst2> db2stop Db2inst2> db2start
此時,如果您嘗試使用“db2start”命令,您將收到安全錯誤訊息。因為,DB2 安全配置尚未針對您的 LDAP 環境正確配置。
自定義兩個配置
保留在 slapd.conf 檔案中配置的 LDAP_HOST 名稱。
現在編輯 IMBLDAPSecurity.ini 檔案並鍵入 LDAP_HOST 名稱。這兩個檔案中 LDAP_HOST 名稱必須相同。
檔案內容如下所示
;----------------------------------------------------------- ; SERVER RELATED VALUES ;----------------------------------------------------------- LDAP_HOST = localhost ;----------------------------------------------------------- ; USER RELATED VALUES ---------------------------- ;------------------------------- USER_OBJECTCLASS = posixAccount USER_BASEDN = dc=example,dc=com USERID_ATTRIBUTE = uid AUTHID_ATTRIBUTE = uid ;----------------------------------------------------------- ; GROUP RELATED VALUES ;----------------------------------------------------------- GROUP_OBJECTCLASS = groupOfNames GROUP_BASEDN = dc=example,dc=com GROUPNAME_ATTRIBUTE = cn GROUP_LOOKUP_METHOD = SEARCH_BY_DN GROUP_LOOKUP_ATTRIBUTE = member
更改這些值後,LDAP 會立即生效,並且您的 DB2 環境與 LDAP 完美配合。
您可以登出並重新登入到“db2inst2”使用者。
現在您的例項正在使用 LDAP 目錄。