linux 域

用 Linux 上的 LDAP 将 Samba 安装和配置为主域控制器来源: ChinaUnix博客 日期: 2007.04.23 16:15 : 作者:Keith Robertson 来源: 发表

用 Linux 上的 LDAP 将 Samba 安装和配置为主域控制器

来源: ChinaUnix博客 日期: 2007.04.23 16:15 : 作者:Keith Robertson 来源: 发表时间:2006-03-28

内容摘要

本教程讲解如何使用安全的基于 LDAP 的身份验证机制将 Samba 安装和配置为主域控制器。还描述如何为基于 PAM 的身份验证配置

LDAP 服务器 OpenLDAP,以及如何用 Transport Layer Security(TLS )保护 Samba 和

OpenLDAP 之间的链路。完整的系统包括安全的文件共享和打印共享设置,以及健壮的 LDAP 服务器,这些设施支持的功能甚至超出了

Samba 的需要。另外,Windows® 客户机能够登录到作为主域控制器的 Samba

服务器,并能够根据它们的组成员关系共享自动挂装的驱动器。

关于本教程

本教程讲解如何使用安全的基于 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 ;创建所需的目录和共享驱动器;配置 smb.conf 文件并设置 LDAP 数据库访问密码;填充数据库;添加 PAM

和其他用户并将 Windows 工作站添加到域中;以及对不工作的 Samba 安装进行调试。

∙ 最后,讨论安全问题以及如何为这个系统启用安全性,包括为 OpenLDAP、

PAM 和 Samba 启用 Transport Layer Security ,以及如何测试系统的安全性。 ∙

完整的系统包括安全的文件共享和打印共享设置,以及健壮的 LDAP 服务器,这些设施支持的功能甚至超出了 Samba

的需要。另外,Microsoft Windows 客户机能够登录到作为主域控制器的 Samba 服务器,并能够根据它们的组成员关系共享自动挂装的驱动器。

本教程的读者最好具有中等程度的 UNIX 或 Linux 技能和经验,并了解基本的 IP 网络概念。作者使用 Fedora Core 3

,

作为 Linux 发行版,但是其他 Linux 发行版或 UNIX 变体(比如 AIX、Solaris 或

HP-UX )也可以应用本教程中描述的设置。本教程中使用的所有应用程序和实用程序都是开放源码的,可以从 Linux 厂商或应用程序厂商的主页获得。

前提条件

Linux 发行版是 Fedora Core 3;但是这里描述的设置也可以应用于其他 Linux 发行版或 UNIX 变体(比如

AIX 、Solaris 或 HP-UX)。这种软件可以以许多方式免费获得。建议从 Linux 厂商的 ftp 镜像站点获得预编译的版本(比如

RPM )。

下面是本教程中使用的软件的清单。不需要事先按照这张清单进行准备,因为本教程会描述如何下载和安装这些软件。

∙ OpenSSL OpenLDAP Samba Perl module Crypt::SmbHash Perl module Digest::SHA1 Perl module IO::Socket::SSL Perl module Net::SSLeay IDEALX Samba LDAP 工具

注意:本教程指出了各种经过测试的软件组件的特定版本。采用以前的软件版本也可能可以,但是我不能保证会成功。一般来说,使用比本教程中使用的版本更新的软件应该没问题。

教程的网络布局

本教程中描述的网络比较小,所以很容易在您自己的家庭或实验室网络上重现这里的示例。对于这个设置,我使用一个典型的家用宽带路由器,其中内置了防火墙。下图描述了物理网络布局。

图 1. 教程的网络配置

这个 Microsoft Windows 网络包含三类用户 ——

市场营销、工程和管理。工程和市场营销用户各有一个共享的驱动器,每个组的用户可以分别将文件放在这里,供同组的其他用户查看;但是,一个组的成员不能查

看另一个组的共享驱动器上的文件。例如,市场营销职员不能查看工程驱动器上的文件。管理用户也有一个共享驱动器,只有经理能够看到这个驱动器。另外,

,

经理

具有特殊特权,他们还能够查看工程和市场营销组的文件。

安装 LDAP

现在介绍 LDAP,讲解它如何与 Samba 集成,并讨论安全问题。

LDAP 简介

LDAP 是一种流行的身份验证机制,并可以作为存储个人可识别信息的存储库。与传统的基于平面文件的身份验证机制(例如

/etc/passwd)相比,它有几个优点。优点之一是 LDAP 可以用来实现密码管理的理想形式 ——

单点登录。能够实现单点登录是因为软件应用程序可以通过 TCP/IP 网络针对通用的基于 LDAP 的用户存储库进行远程身份验证。LDAP

身份验证方法与基于平面文件的身份验证方案正好相反,后者通常只使用一台计算机,并不是分布式的。

LDAP 将数据组织为目录信息树(directory information tree)。这个树按照分类对数据进行组织。许多

LDAP 服务器使用 SQL 数据库存储数据,因为这是一种自然的选择。与传统的 SQL 数据库一样,LDAP

使用模式来定义数据应该放在哪里以及数据应该如何格式化。使用模式以及与传统 SQL 数据库的相似性是 LDAP

的关键优点,因为它们大大提高了可扩展性。

集成 Samba 和 LDAP

在 Samba 和 LDAP 服务器之间有三个主要的集成点:

第一个集成点是将 Samba 的模式包含到 LDAP 服务器中。

∙ 第二个集成点是将 Samba 配置为通过 LDAP 服务器进行身份验证。 ∙

身份验证要利用 Linux 的 PAM 实用程序(Pluggable Authentication Modules )。PAM

实用程序将身份验证过程从 Linux 上运行的软件应用程序抽象出来,这样应用程序就不必理解特定身份验证机制的复杂细节。因此,PAM

为软件应用程序提供了很大的灵活性,软件应用程序只需调用身份验证 API,而 PAM 会决定是应该使用平面文件、LDAP 还是其他身份验证机制。

∙ 第三个集成点涉及一组工具,它们帮助管理 Samba 的 LDAP 目录信息树。这个工具箱是由第三方开发的;但是它采用 GNU Public License。

安全

LDAP 的关键优点是它能够作为分布在网络上的软件应用程序的身份验证机制。这个优点的一个副作用是,在身份验证期间密码可能会通过网络传输,因此可能被截获。幸运的是,LDAP 支持 SSL(Secure Sockets Layer)和 TLS。

,

在本教程中,LDAP 服务器运行在与 Samba 相同的物理服务器上;因此不必进行加密。但是,我将演示如何对 LDAP 和 Samba 之间的通道进行加密,因为这相当简单,而且对于 Samba 和 LDAP 在不同计算机上的情况,这种措施是必要的。 本教程分为两个阶段。第一个阶段详细描述如何在非安全模式中配置 Samba 和 LDAP 。第一个阶段完成之后,对 Samba 和 LDAP 服务器之间的通道启用加密。采用两阶段方式是因为在非安全模式中往往更容易进行安装、配置和诊断问题。 配置 LDAP

步骤 1:安装 OpenLDAP

要安装 OpenLDAP:

∙ 检查发行版是否已经安装了 OpenLDAP。在一个终端中发出以下命令:rpm -qa | grep ldap。如果没有得到响应 openldap-2.2.13(或更高的版本号),那么应该升级或安装 OpenLDAP(在下面说明)。

∙ 如果还没有 OpenLDAP 2.2.13 或更高版本,那么到发行版的镜像站点并下载一个二进制包。我进入

Fedora 的镜像列表

并下载了 openldap-2.2.13-2.i386.rpm。然后发出以下命令:rpm -Uvh openldap-2.2.13-2.i386.rpm 。

步骤 2:安装 IDEALX 的 LDAP Toolkit for Samba

需要使用来自 IDEALX 的一个工具箱使 Samba 和 LDAP 服务器之间的许多重要交互自动化。这个工具箱包含一些脚本,Samba 会自动调用它们来添加用户、修改用户、添加机器,等等。这些脚本是用 Perl 编写的,也可以从命令行使用。 要安装 IDEALX 工具箱:

∙ 进入

IDEALX 的主页

并下载这个工具箱的 .0.9.1 版或更高版本(smbldap-tools-.0.9.1.tgz )。 ∙ 在一个临时目录中,用以下命令解开这个工具箱:tar -zxf

smbldap-tools-0.9.1.tgz 。

∙ 创建一个目录用于放置 IDEALX 脚本。在命令提示下输入:mkdir -p /var/lib/samba/sbin。然后输入:chmod -R 755 /var/lib/samba。 ∙ 进入 tar 命令创建的目录。输入:cd smbldap-tools-0.9.1。

∙ 用以下命令将所需的脚本从临时目录复制到永久目录:cp smbldap* configure.pl /var/lib/samba/sbin。

∙ 进入 /var/lib/samba/sbin 目录并依次 发出以下命令,设置正确的所属权和权限:

∙ chmod 750 *

∙ chmod 640 smbldap_bind.conf smbldap.conf smbldap_tools.pm ∙ 现在可以安全地删除临时目录。

IDEALX 工具箱需要一些额外的 Perl 模块,系统上可能还没有安装这些模块。本节说明如何下载和安装它们。

∙ 首先,需要从 CPAN.org 下载所有必需的 Perl 模块。进入 CPAN.org

并在搜索框中输入以下搜索字符串。

∙ 搜索 Perl 模块:Crypt::SmbHash

,

搜索 Perl 模块:Digest::SHA1

∙ 搜索 Perl 模块:IO::Socket::SSL

∙ 搜索 Perl 模块:Net::SSLeay ∙

可以从每个搜索的结果直接导航到每个模块的主页。在这四个模块的主页上,可以找到下载 Perl 模块的链接。

∙ 下一步是对下载的 Perl 模块进行解压。在保存下载的四个模块的目录中,发出以下命令:tar -zxvf *.gz。

∙ 最后一步是构建和安装这四个模块。进入每个新创建的目录并作为 root 发出以下命令。

∙ perl Makefile.PL

∙ make install

步骤 3:配置 OpenLDAP 所需的模式、目录和密钥

如果从 RPM 进行安装而且 Linux 发行版是 Fedora,那么应该有一个称为

/etc/openldap 的目录。在这个目录中有 OpenLDAP 的主配置文件 slapd.conf。在开始编辑这个文件之前,先执行一些预先设置操作。

OpenLDAP 需要感知 Samba 的 LDAP 模式。为此,将 Samba 的模式复制到 /etc/openldap/schema 中。

∙ 寻找 Samba 的模式。它应该在安装 Samba 的目录中。为了寻找它,输入:locate samba.schema。

∙ 将它复制到 /etc/openldap/schema 中。

∙ 设置正确的权限。输入:chmod 644 /etc/openldap/schema/samba.schema。 为 LDAP 数据库创建目录。在本教程中,将这个目录命名为与域名相同。 ∙ 输入:mkdir -p /var/lib/ldap/somedomain.com。

∙ 设置正确的权限:chmod 700 /var/lib/ldap/somedomain.com。

∙ 设置正确的所属权。Fedora 应该已经在 /etc/passwd 中定义了用户 LDAP。如果在其他发行版上进行安装,那么可能需要创建这个用户。输入:chown ldap:ldap /var/lib/ldap/somedomain.com。

最后,创建 OpenLDAP 用于 TLS 的加密密钥。为此,需要 OpenSSL。大多数 Linux 发行版附带有 OpenSSL;但是,如果还没有安装它,那么从发行版的厂商或 http://www.openssl.org/

获得一个副本。

本教程假设用户将不使用商业的证书机构(CA ),比如 Verisign、Thawte 等。因此,需要成为自己的 CA 并签署 LDAP 服务器使用的证书。下面的步骤演示如何成为 CA 并签署证书。

∙ 如果还没有这么做,那么编辑 openssl.cnf 以满足自己的特定需要。要寻找 openssl.cnf 文件,输入:locate openssl.cnf。

∙ 在 openssl.cnf 所在的目录中,作为 root 发出以下命令。

清单 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 并根据自己的情况修改以下参数。 清单 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

∙ 用以下命令创建 CA 证书和密钥对:openssl req -nodes -config

openssl.cnf -new -x509 -keyout CA/private/cakey.pem -out CA/cacert.pem -days 3650。

∙ 用以下命令为 OpenLDAP 创建密钥对:

∙ 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 配置目录,这样各个应用程序就能够访问它了。

∙ cp CA/cacert.pem /etc/openldap/

∙ chmod 644 /etc/openldap/cacert.pem

步骤 4:配置 slapd.conf

OpenLDAP 提供的对应于实际 LDAP 服务器的可执行文件称为 slapd。slapd 守护进程从 slapd.conf 读取它的所有配置信息。在本节中,我提供一个加了注释的 slapd.conf 示例。对于所有带 “# MODIFY” 注释的部分,必须根据自己的情况进行修改。

在开始进行编辑之前,为 rootdn 生成一个密码散列。要对 LDAP 服务器的目录信息树进行修改,就必须使用这个密码。

注意:应该选择一个不同于 Linux 服务器 root 密码的密码。

∙ 从命令行输入:slappasswd -h {SSHA} -s 。

∙ 保存这个命令的输出,因为后面要使用它。它类似于:

{SSHA}kCuJt72QLJ2O06nFUvdre97sHT0AxlH/。

如果在 Fedora 上安装了 OpenLDAP 的二进制版,slapd.conf 应该以

,

/etc/openldap/ 形式存在。根据自己的情况修改它。

/etc/openldap/slapd.conf 的内容如下:

清单 3. slapd.conf 的内容

#

# 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

本地系统上的 LDAP 客户机使用 /etc/ldap.conf 文件。这些客户机包含 PAM,Samba 通过这个接口利用 LDAP 服务器进行身份验证。

有时候,系统上有多个 ldap.conf 实例。应该找到 PAM 要使用的那一个。为此,输入:strings /lib/libnss_ldap.so.2 | grep conf。返回的值常常是 /etc/ldap.conf。

在编辑器中编辑 ldap.conf 并插入以下文本。修改带有 “# MODIFY” 注释的部分。

/etc/ldap.conf 的内容如下:

清单 4. /etc/ldap.conf 的内容

## 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 发行版有类似的配置实用程序,所以如果不使用 Fedora,就请参考文档。

∙ 从命令行启动 authconfig。输入:authconfig 。

∙ 按照下图编辑第一个屏幕。

图 2. authconfig 屏幕 1

∙ 按照下图编辑第二个屏幕。

图 3. authconfig 屏幕 2

步骤 7:启动 OpenLDAP

标签: