linux 域
用 Linux 上的 LDAP 將 Samba 安裝和配置為主域控制器來源: ChinaUnix博客 日期: 2007.04.23 16:15 : 作者:Keith Robertson 來源: 發(fā)表
用 Linux 上的 LDAP 將 Samba 安裝和配置為主域控制器
來源: ChinaUnix博客 日期: 2007.04.23 16:15 : 作者:Keith Robertson 來源: 發(fā)表時間:2006-03-28
內(nèi)容摘要
本教程講解如何使用安全的基于 LDAP 的身份驗證機制將 Samba 安裝和配置為主域控制器。還描述如何為基于 PAM 的身份驗證配置
LDAP 服務(wù)器 OpenLDAP,以及如何用 Transport Layer Security(TLS )保護 Samba 和
OpenLDAP 之間的鏈路。完整的系統(tǒng)包括安全的文件共享和打印共享設(shè)置,以及健壯的 LDAP 服務(wù)器,這些設(shè)施支持的功能甚至超出了
Samba 的需要。另外,Windows® 客戶機能夠登錄到作為主域控制器的 Samba
服務(wù)器,并能夠根據(jù)它們的組成員關(guān)系共享自動掛裝的驅(qū)動器。
關(guān)于本教程
本教程講解如何使用安全的基于 LDAP 的身份驗證機制將 Samba 安裝和配置為主域控制器。在本教程中,我將:
介紹 LDAP,講解它如何與 Samba 集成,并討論安全問題。
? 介紹配置 LDAP 的步驟,包括安裝 OpenLDAP 和 IDEALX LDAP Samba 工
具箱;配置
OpenLDAP 所需的 slapd.conf 文件、/etc/ldap.conf 文件和 Pluggable Authentication
Modules (PAM );并解釋如何啟動 OpenLDAP。
? 接下來,講解如何配置 Samba,包括安裝和啟動 Samba 和 Logon Profile
Generator ;創(chuàng)建所需的目錄和共享驅(qū)動器;配置 smb.conf 文件并設(shè)置 LDAP 數(shù)據(jù)庫訪問密碼;填充數(shù)據(jù)庫;添加 PAM
和其他用戶并將 Windows 工作站添加到域中;以及對不工作的 Samba 安裝進行調(diào)試。
? 最后,討論安全問題以及如何為這個系統(tǒng)啟用安全性,包括為 OpenLDAP、
PAM 和 Samba 啟用 Transport Layer Security ,以及如何測試系統(tǒng)的安全性。 ?
完整的系統(tǒng)包括安全的文件共享和打印共享設(shè)置,以及健壯的 LDAP 服務(wù)器,這些設(shè)施支持的功能甚至超出了 Samba
的需要。另外,Microsoft Windows 客戶機能夠登錄到作為主域控制器的 Samba 服務(wù)器,并能夠根據(jù)它們的組成員關(guān)系共享自動掛裝的驅(qū)動器。
本教程的讀者最好具有中等程度的 UNIX 或 Linux 技能和經(jīng)驗,并了解基本的 IP 網(wǎng)絡(luò)概念。作者使用 Fedora Core 3
,作為 Linux 發(fā)行版,但是其他 Linux 發(fā)行版或 UNIX 變體(比如 AIX、Solaris 或
HP-UX )也可以應(yīng)用本教程中描述的設(shè)置。本教程中使用的所有應(yīng)用程序和實用程序都是開放源碼的,可以從 Linux 廠商或應(yīng)用程序廠商的主頁獲得。
前提條件
Linux 發(fā)行版是 Fedora Core 3;但是這里描述的設(shè)置也可以應(yīng)用于其他 Linux 發(fā)行版或 UNIX 變體(比如
AIX 、Solaris 或 HP-UX)。這種軟件可以以許多方式免費獲得。建議從 Linux 廠商的 ftp 鏡像站點獲得預(yù)編譯的版本(比如
RPM )。
下面是本教程中使用的軟件的清單。不需要事先按照這張清單進行準備,因為本教程會描述如何下載和安裝這些軟件。
?
?
?
?
?
?
?
? OpenSSL OpenLDAP Samba Perl module Crypt::SmbHash Perl module Digest::SHA1 Perl module IO::Socket::SSL Perl module Net::SSLeay IDEALX Samba LDAP 工具
注意:本教程指出了各種經(jīng)過測試的軟件組件的特定版本。采用以前的軟件版本也可能可以,但是我不能保證會成功。一般來說,使用比本教程中使用的版本更新的軟件應(yīng)該沒問題。
教程的網(wǎng)絡(luò)布局
本教程中描述的網(wǎng)絡(luò)比較小,所以很容易在您自己的家庭或?qū)嶒炇揖W(wǎng)絡(luò)上重現(xiàn)這里的示例。對于這個設(shè)置,我使用一個典型的家用寬帶路由器,其中內(nèi)置了防火墻。下圖描述了物理網(wǎng)絡(luò)布局。
圖 1. 教程的網(wǎng)絡(luò)配置
這個 Microsoft Windows 網(wǎng)絡(luò)包含三類用戶 ——
市場營銷、工程和管理。工程和市場營銷用戶各有一個共享的驅(qū)動器,每個組的用戶可以分別將文件放在這里,供同組的其他用戶查看;但是,一個組的成員不能查
看另一個組的共享驅(qū)動器上的文件。例如,市場營銷職員不能查看工程驅(qū)動器上的文件。管理用戶也有一個共享驅(qū)動器,只有經(jīng)理能夠看到這個驅(qū)動器。另外,
,經(jīng)理
具有特殊特權(quán),他們還能夠查看工程和市場營銷組的文件。
安裝 LDAP
現(xiàn)在介紹 LDAP,講解它如何與 Samba 集成,并討論安全問題。
LDAP 簡介
LDAP 是一種流行的身份驗證機制,并可以作為存儲個人可識別信息的存儲庫。與傳統(tǒng)的基于平面文件的身份驗證機制(例如
/etc/passwd)相比,它有幾個優(yōu)點。優(yōu)點之一是 LDAP 可以用來實現(xiàn)密碼管理的理想形式 ——
單點登錄。能夠?qū)崿F(xiàn)單點登錄是因為軟件應(yīng)用程序可以通過 TCP/IP 網(wǎng)絡(luò)針對通用的基于 LDAP 的用戶存儲庫進行遠程身份驗證。LDAP
身份驗證方法與基于平面文件的身份驗證方案正好相反,后者通常只使用一臺計算機,并不是分布式的。
LDAP 將數(shù)據(jù)組織為目錄信息樹(directory information tree)。這個樹按照分類對數(shù)據(jù)進行組織。許多
LDAP 服務(wù)器使用 SQL 數(shù)據(jù)庫存儲數(shù)據(jù),因為這是一種自然的選擇。與傳統(tǒng)的 SQL 數(shù)據(jù)庫一樣,LDAP
使用模式來定義數(shù)據(jù)應(yīng)該放在哪里以及數(shù)據(jù)應(yīng)該如何格式化。使用模式以及與傳統(tǒng) SQL 數(shù)據(jù)庫的相似性是 LDAP
的關(guān)鍵優(yōu)點,因為它們大大提高了可擴展性。
集成 Samba 和 LDAP
在 Samba 和 LDAP 服務(wù)器之間有三個主要的集成點:
第一個集成點是將 Samba 的模式包含到 LDAP 服務(wù)器中。
? 第二個集成點是將 Samba 配置為通過 LDAP 服務(wù)器進行身份驗證。 ?
身份驗證要利用 Linux 的 PAM 實用程序(Pluggable Authentication Modules )。PAM
實用程序?qū)⑸矸蒡炞C過程從 Linux 上運行的軟件應(yīng)用程序抽象出來,這樣應(yīng)用程序就不必理解特定身份驗證機制的復雜細節(jié)。因此,PAM
為軟件應(yīng)用程序提供了很大的靈活性,軟件應(yīng)用程序只需調(diào)用身份驗證 API,而 PAM 會決定是應(yīng)該使用平面文件、LDAP 還是其他身份驗證機制。
? 第三個集成點涉及一組工具,它們幫助管理 Samba 的 LDAP 目錄信息樹。這個工具箱是由第三方開發(fā)的;但是它采用 GNU Public License。
安全
LDAP 的關(guān)鍵優(yōu)點是它能夠作為分布在網(wǎng)絡(luò)上的軟件應(yīng)用程序的身份驗證機制。這個優(yōu)點的一個副作用是,在身份驗證期間密碼可能會通過網(wǎng)絡(luò)傳輸,因此可能被截獲。幸運的是,LDAP 支持 SSL(Secure Sockets Layer)和 TLS。
,在本教程中,LDAP 服務(wù)器運行在與 Samba 相同的物理服務(wù)器上;因此不必進行加密。但是,我將演示如何對 LDAP 和 Samba 之間的通道進行加密,因為這相當簡單,而且對于 Samba 和 LDAP 在不同計算機上的情況,這種措施是必要的。 本教程分為兩個階段。第一個階段詳細描述如何在非安全模式中配置 Samba 和 LDAP 。第一個階段完成之后,對 Samba 和 LDAP 服務(wù)器之間的通道啟用加密。采用兩階段方式是因為在非安全模式中往往更容易進行安裝、配置和診斷問題。 配置 LDAP
步驟 1:安裝 OpenLDAP
要安裝 OpenLDAP:
? 檢查發(fā)行版是否已經(jīng)安裝了 OpenLDAP。在一個終端中發(fā)出以下命令:rpm -qa | grep ldap。如果沒有得到響應(yīng) openldap-2.2.13(或更高的版本號),那么應(yīng)該升級或安裝 OpenLDAP(在下面說明)。
? 如果還沒有 OpenLDAP 2.2.13 或更高版本,那么到發(fā)行版的鏡像站點并下載一個二進制包。我進入
Fedora 的鏡像列表
并下載了 openldap-2.2.13-2.i386.rpm。然后發(fā)出以下命令:rpm -Uvh openldap-2.2.13-2.i386.rpm 。
步驟 2:安裝 IDEALX 的 LDAP Toolkit for Samba
需要使用來自 IDEALX 的一個工具箱使 Samba 和 LDAP 服務(wù)器之間的許多重要交互自動化。這個工具箱包含一些腳本,Samba 會自動調(diào)用它們來添加用戶、修改用戶、添加機器,等等。這些腳本是用 Perl 編寫的,也可以從命令行使用。 要安裝 IDEALX 工具箱:
? 進入
IDEALX 的主頁
并下載這個工具箱的 .0.9.1 版或更高版本(smbldap-tools-.0.9.1.tgz )。 ? 在一個臨時目錄中,用以下命令解開這個工具箱:tar -zxf
smbldap-tools-0.9.1.tgz 。
? 創(chuàng)建一個目錄用于放置 IDEALX 腳本。在命令提示下輸入:mkdir -p /var/lib/samba/sbin。然后輸入:chmod -R 755 /var/lib/samba。 ? 進入 tar 命令創(chuàng)建的目錄。輸入:cd smbldap-tools-0.9.1。
? 用以下命令將所需的腳本從臨時目錄復制到永久目錄:cp smbldap* configure.pl /var/lib/samba/sbin。
? 進入 /var/lib/samba/sbin 目錄并依次 發(fā)出以下命令,設(shè)置正確的所屬權(quán)和權(quán)限:
? chmod 750 *
? chmod 640 smbldap_bind.conf smbldap.conf smbldap_tools.pm ? 現(xiàn)在可以安全地刪除臨時目錄。
IDEALX 工具箱需要一些額外的 Perl 模塊,系統(tǒng)上可能還沒有安裝這些模塊。本節(jié)說明如何下載和安裝它們。
? 首先,需要從 CPAN.org 下載所有必需的 Perl 模塊。進入 CPAN.org
并在搜索框中輸入以下搜索字符串。
? 搜索 Perl 模塊:Crypt::SmbHash
,搜索 Perl 模塊:Digest::SHA1
? 搜索 Perl 模塊:IO::Socket::SSL
? 搜索 Perl 模塊:Net::SSLeay ?
可以從每個搜索的結(jié)果直接導航到每個模塊的主頁。在這四個模塊的主頁上,可以找到下載 Perl 模塊的鏈接。
? 下一步是對下載的 Perl 模塊進行解壓。在保存下載的四個模塊的目錄中,發(fā)出以下命令:tar -zxvf *.gz。
? 最后一步是構(gòu)建和安裝這四個模塊。進入每個新創(chuàng)建的目錄并作為 root 發(fā)出以下命令。
? perl Makefile.PL
? make install
步驟 3:配置 OpenLDAP 所需的模式、目錄和密鑰
如果從 RPM 進行安裝而且 Linux 發(fā)行版是 Fedora,那么應(yīng)該有一個稱為
/etc/openldap 的目錄。在這個目錄中有 OpenLDAP 的主配置文件 slapd.conf。在開始編輯這個文件之前,先執(zhí)行一些預(yù)先設(shè)置操作。
OpenLDAP 需要感知 Samba 的 LDAP 模式。為此,將 Samba 的模式復制到 /etc/openldap/schema 中。
? 尋找 Samba 的模式。它應(yīng)該在安裝 Samba 的目錄中。為了尋找它,輸入:locate samba.schema。
? 將它復制到 /etc/openldap/schema 中。
? 設(shè)置正確的權(quán)限。輸入:chmod 644 /etc/openldap/schema/samba.schema。 為 LDAP 數(shù)據(jù)庫創(chuàng)建目錄。在本教程中,將這個目錄命名為與域名相同。 ? 輸入:mkdir -p /var/lib/ldap/somedomain.com。
? 設(shè)置正確的權(quán)限:chmod 700 /var/lib/ldap/somedomain.com。
? 設(shè)置正確的所屬權(quán)。Fedora 應(yīng)該已經(jīng)在 /etc/passwd 中定義了用戶 LDAP。如果在其他發(fā)行版上進行安裝,那么可能需要創(chuàng)建這個用戶。輸入:chown ldap:ldap /var/lib/ldap/somedomain.com。
最后,創(chuàng)建 OpenLDAP 用于 TLS 的加密密鑰。為此,需要 OpenSSL。大多數(shù) Linux 發(fā)行版附帶有 OpenSSL;但是,如果還沒有安裝它,那么從發(fā)行版的廠商或 http://www.openssl.org/
獲得一個副本。
本教程假設(shè)用戶將不使用商業(yè)的證書機構(gòu)(CA ),比如 Verisign、Thawte 等。因此,需要成為自己的 CA 并簽署 LDAP 服務(wù)器使用的證書。下面的步驟演示如何成為 CA 并簽署證書。
? 如果還沒有這么做,那么編輯 openssl.cnf 以滿足自己的特定需要。要尋找 openssl.cnf 文件,輸入:locate openssl.cnf。
? 在 openssl.cnf 所在的目錄中,作為 root 發(fā)出以下命令。
清單 1. 成為 CA 的步驟
mkdir -p CA/certs CA/crl CA/newcerts CA/private
chmod 700 CA/private
touch CA/index.txt
echo 01 > CA/serial
,? 在編輯器中打開 openssl.cnf 并根據(jù)自己的情況修改以下參數(shù)。 清單 2. 成為 CA 的步驟
# The dir parameter is important because it tells
# openssl where to find all necessary files used to
# generate keys.
dir = ./
# default_days defines the length of time your key is valid for. default_days = 3650
# default_bits is an indicator of the strength of your key. I elected # 1024 but you can choose more or less.
default_bits = 1024
# The following parameters should be modified to fit your
# organization.
countryName_default = US
stateOrProvinceName_default = North Carolina
localityName_default = Raleigh
0.organizationName_default = somedomain.com
? 用以下命令創(chuàng)建 CA 證書和密鑰對:openssl req -nodes -config
openssl.cnf -new -x509 -keyout CA/private/cakey.pem -out CA/cacert.pem -days 3650。
? 用以下命令為 OpenLDAP 創(chuàng)建密鑰對:
? openssl req -config openssl.cnf -nodes -new -keyout
/etc/openldap/slapd-key.pem -out slapd.csr
? openssl ca -config openssl.cnf -out /etc/openldap/slapd-cert.pem -in slapd.csr
? chown root:ldap /etc/openldap/slapd-key.pem
? chmod 640 /etc/openldap/slapd-key.pem
? chmod 644 /etc/openldap/slapd-cert.pem
? 將 CA 的證書復制到 openldap 配置目錄,這樣各個應(yīng)用程序就能夠訪問它了。
? cp CA/cacert.pem /etc/openldap/
? chmod 644 /etc/openldap/cacert.pem
步驟 4:配置 slapd.conf
OpenLDAP 提供的對應(yīng)于實際 LDAP 服務(wù)器的可執(zhí)行文件稱為 slapd。slapd 守護進程從 slapd.conf 讀取它的所有配置信息。在本節(jié)中,我提供一個加了注釋的 slapd.conf 示例。對于所有帶 “# MODIFY” 注釋的部分,必須根據(jù)自己的情況進行修改。
在開始進行編輯之前,為 rootdn 生成一個密碼散列。要對 LDAP 服務(wù)器的目錄信息樹進行修改,就必須使用這個密碼。
注意:應(yīng)該選擇一個不同于 Linux 服務(wù)器 root 密碼的密碼。
? 從命令行輸入:slappasswd -h {SSHA} -s 。
? 保存這個命令的輸出,因為后面要使用它。它類似于:
{SSHA}kCuJt72QLJ2O06nFUvdre97sHT0AxlH/。
如果在 Fedora 上安裝了 OpenLDAP 的二進制版,slapd.conf 應(yīng)該以
,/etc/openldap/ 形式存在。根據(jù)自己的情況修改它。
/etc/openldap/slapd.conf 的內(nèi)容如下:
清單 3. slapd.conf 的內(nèi)容
#
# See slapd.conf(5) for details on configuration options.
# This file should NOT be world readable.
#
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/nis.schema
include /etc/openldap/schema/samba.schema
# -1 is all messages 296 is a good compromise for most debugging #loglevel -1
pidfile /var/run/slapd.pid
argsfile /var/run/slapd.args
# The following three lines are related to security. Leave them commented out now.
# We uncomment them and enable security *after* we have successfully tested Samba with
# LDAP in an unsecured configuration. Debugging is infinitely easier without encryption
# enabled.
#TLSCipherSuite HIGH
#TLSCertificateFile /etc/openldap/slapd-cert.pem
#TLSCertificateKeyFile /etc/openldap/slapd-key.pem
database bdb
# MODIFY
# Modify suffix and rootdn to match your domain name.
suffix "dc=somedomain,dc=com"
rootdn "cn=Manager,dc=somedomain,dc=com"
# MODIFY
# Use the following to generate:
# slappasswd -h {SSHA} -s
rootpw {SSHA}kCuJt72QLJ2O06nFUvdre97sHT0AxlH/
# MODIFY
# The database directory MUST exist prior to running slapd AND # should only be accessible by the slapd and slap tools.
# Mode 700 recommended with an owner of ldap and a group of ldap directory /var/lib/ldap/somedomain.com
# Indices to maintain for this database
index objectClass eq
index cn pres,sub,eq
index sn pres,sub,eq
,index uid pres,sub,eq
index displayName pres,sub,eq
index uidNumber eq
index gidNumber eq
index memberUID eq
index sambaSID eq
index sambaPrimaryGroupSID eq
index sambaDomainName eq
index default sub
# Access Control Entries
# Note these ACEs are duplicated from the IDEALX smbldap usermanual with one exception
# users can authenticate and change their password access to attrs=userPassword,
sambaNTPassword,sambaLMPassword,sambaPwdLastSet,sambaPwdMustChange by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by self write
by anonymous auth
by * none
# some attributes need to be readable anonymously so that 'id user' can answer correctly
access to
attrs=objectClass,entry,gecos,homeDirectory,uid,uidNumber,gidNumber,cn,
memberUid
by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by * read
# somme attributes can be writable by users themselves
access to attrs=description,telephoneNumber
by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by self write
by * read
# some attributes need to be writable for samba (this ACE modified from original to allow
some unix commands to work) access to
attrs=cn,sambaLMPassword,sambaNTPassword,
sambaPwdLastSet,sambaLogonTime,sambaLogoffTime,sambaKickoffTime,samba PwdCanChange,
sambaPwdMustChange,sambaAcctFlags,displayName,sambaHomePath,sambaHome Drive,
sambaLogonScript,sambaProfilePath,description,sambaUserWorkstations,s ambaPrimaryGroupSID,
sambaDomainName,sambaSID,sambaGroupType,sambaNextRid,sambaNextGroupRi d,sambaNextUserRid,
,sambaAlgorithmicRidBase,sambaLogonScript,loginShell
by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by self read
by * none
# samba need to be able to create the samba domain account
access to dn.base="dc=somedomain,dc=com"
by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by * none
# samba need to be able to create new users account
access to dn="ou=Users,dc=somedomain,dc=com"
by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by * none
# samba need to be able to create new groups account
access to dn="ou=Groups,dc=somedomain,dc=com"
by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by * none
# samba need to be able to create new computers account
access to dn="ou=Computers,dc=somedomain,dc=com"
by dn="uid=samba,ou=Users,dc=somedomain,dc=com" write
by * none
# this can be omitted but we leave it: there could be other branch # in the directory
access to *
by self read
by * none
步驟 5:配置 /etc/ldap.conf
本地系統(tǒng)上的 LDAP 客戶機使用 /etc/ldap.conf 文件。這些客戶機包含 PAM,Samba 通過這個接口利用 LDAP 服務(wù)器進行身份驗證。
有時候,系統(tǒng)上有多個 ldap.conf 實例。應(yīng)該找到 PAM 要使用的那一個。為此,輸入:strings /lib/libnss_ldap.so.2 | grep conf。返回的值常常是 /etc/ldap.conf。
在編輯器中編輯 ldap.conf 并插入以下文本。修改帶有 “# MODIFY” 注釋的部分。
/etc/ldap.conf 的內(nèi)容如下:
清單 4. /etc/ldap.conf 的內(nèi)容
## IMPORTANT
## The /etc/ldap.conf file is used by PAM. There is another ldap.conf file in
## /etc/openldap.
## The file, /etc/openldap/ldap.conf, is used by ldap tools, such as ldapsearch.
## If you intend to use those tools you will need to add a TLS_CACERT directive to that
## file also.
,# Your LDAP server. Must be resolvable without using LDAP.
# Multiple hosts may be specified, each separated by a
# space.
host 127.0.0.1
# MODIFY
# The distinguished name of the search base.
base dc=somedomain,dc=com
# MODIFY
# The distinguished name to bind to the server with.
# We will use the root dn until we can create a lesser privileged user. binddn cn=Manager,dc=somedomain,dc=com
bindpw
# MODIFY
# Note: "ou=Users" and "ou=Groups" should match what
# you entered in smb.conf for "ldap group suffix"
# and "ldap user suffix"
nss_base_passwd ou=Users,dc=somedomain,dc=com?one
nss_base_passwd ou=Computers,dc=somedomain,dc=com?one nss_base_shadow ou=Users,dc=somedomain,dc=com?one nss_base_group ou=Groups,dc=somedomain,dc=com?one ssl no
pam_password md5
# We need to tell PAM where the certificate used to authenticate the LDAP # server (i.e. is the LDAP server the one we think it is).
tls_cacertfile /etc/openldap/cacert.pem
# If you experience difficulty authenticating after enabling TLS, try uncommenting
# the next line. You will know that you are having problems if you # issue "getent group" and do not see any of the MS Windows groups # that have been created in your LDAP database.
# tls_checkpeer no
步驟 6:配置 PAM
配置 PAM 需要點兒技巧。因此,我使用 Linux 廠商提供的實用程序替我做所有這些麻煩的工作。Fedora 提供一個命令行實用程序 authconfig,它知道如何修改所有 PAM 配置文件。其他 Linux 發(fā)行版有類似的配置實用程序,所以如果不使用 Fedora,就請參考文檔。
? 從命令行啟動 authconfig。輸入:authconfig 。
? 按照下圖編輯第一個屏幕。
圖 2. authconfig 屏幕 1
? 按照下圖編輯第二個屏幕。
圖 3. authconfig 屏幕 2
步驟 7:啟動 OpenLDAP