当前位置: 首页 >> 应用软件 >> 网络相关 >> 用于Samba 的基于LDAP 的身份验证(1)
 

用于Samba 的基于LDAP 的身份验证(1)

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

作者:Keith Robertson      来源:www6.software.ibm.com     发表时间:2006-03-28     浏览次数:      字号:    

内容摘要 本教程讲解如何使用安全的基于 LDAP 的身份验证机制将 Samba 安装和配置为主域控制器。还描述如何为基于 PAM 的身份验证配置 LDAP 服务器 OpenLDAP,以及如何用 Transport Layer Security(TLS)保护 Samba 和 OpenLDAP 之间的链路。完整的系统包括安全的文件共享和打印共享设置,以及健壮的 LDAP 服务器,这些设施支持的功能甚至超出了 Samba 的需要。另外,Windows® 客户机能够登录到作为主域控制器的 Samba 服务器,并能够根据它们的组成员关系共享自动挂装的驱动器。

步骤 1:安装 OpenLDAP

要安装 OpenLDAP:

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

  1. 进入 IDEALX 的主页 并下载这个工具箱的 .0.9.1 版或更高版本(smbldap-tools-.0.9.1.tgz)。
  2. 在一个临时目录中,用以下命令解开这个工具箱:tar -zxf smbldap-tools-0.9.1.tgz
  3. 创建一个目录用于放置 IDEALX 脚本。在命令提示下输入:mkdir -p /var/lib/samba/sbin。然后输入:chmod -R 755 /var/lib/samba
  4. 进入 tar 命令创建的目录。输入:cd smbldap-tools-0.9.1
  5. 用以下命令将所需的脚本从临时目录复制到永久目录:cp smbldap* configure.pl /var/lib/samba/sbin
  6. 进入 /var/lib/samba/sbin 目录并依次 发出以下命令,设置正确的所属权和权限:
    1. chmod 750 *
    2. chmod 640 smbldap_bind.conf smbldap.conf smbldap_tools.pm
  7. 现在可以安全地删除临时目录。

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

  1. 首先,需要从 CPAN.org 下载所有必需的 Perl 模块。进入 CPAN.org 并在搜索框中输入以下搜索字符串。
    • 搜索 Perl 模块:Crypt::SmbHash
    • 搜索 Perl 模块:Digest::SHA1
    • 搜索 Perl 模块:IO::Socket::SSL
    • 搜索 Perl 模块:Net::SSLeay
    可以从每个搜索的结果直接导航到每个模块的主页。在这四个模块的主页上,可以找到下载 Perl 模块的链接。
  2. 下一步是对下载的 Perl 模块进行解压。在保存下载的四个模块的目录中,发出以下命令:tar -zxvf *.gz
  3. 最后一步是构建和安装这四个模块。进入每个新创建的目录并作为 root 发出以下命令。
    1. perl Makefile.PL
    2. make install

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

如果从 RPM 进行安装而且 Linux 发行版是 Fedora,那么应该有一个称为 /etc/openldap 的目录。在这个目录中有 OpenLDAP 的主配置文件 slapd.conf。在开始编辑这个文件之前,先执行一些预先设置操作。

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

  1. 寻找 Samba 的模式。它应该在安装 Samba 的目录中。为了寻找它,输入:locate samba.schema
  2. 将它复制到 /etc/openldap/schema 中。
  3. 设置正确的权限。输入:chmod 644 /etc/openldap/schema/samba.schema

为 LDAP 数据库创建目录。在本教程中,将这个目录命名为与域名相同。

  1. 输入:mkdir -p /var/lib/ldap/somedomain.com
  2. 设置正确的权限:chmod 700 /var/lib/ldap/somedomain.com
  3. 设置正确的所属权。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 并签署证书。

  1. 如果还没有这么做,那么编辑 openssl.cnf 以满足自己的特定需要。要寻找 openssl.cnf 文件,输入:locate openssl.cnf
  2. 在 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
    

  3. 在编辑器中打开 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
    

  4. 用以下命令创建 CA 证书和密钥对:openssl req -nodes -config openssl.cnf -new -x509 -keyout CA/private/cakey.pem -out CA/cacert.pem -days 3650
  5. 用以下命令为 OpenLDAP 创建密钥对:
    1. openssl req -config openssl.cnf -nodes -new -keyout /etc/openldap/slapd-key.pem -out slapd.csr
    2. openssl ca -config openssl.cnf -out /etc/openldap/slapd-cert.pem -in slapd.csr
    3. chown root:ldap /etc/openldap/slapd-key.pem
    4. chmod 640 /etc/openldap/slapd-key.pem
    5. chmod 644 /etc/openldap/slapd-cert.pem
  6. 将 CA 的证书复制到 openldap 配置目录,这样各个应用程序就能够访问它了。
    1. cp CA/cacert.pem /etc/openldap/
    2. chmod 644 /etc/openldap/cacert.pem

步骤 4:配置 slapd.conf

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

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

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

  1. 从命令行输入:slappasswd -h {SSHA} -s <your password here>
  2. 保存这个命令的输出,因为后面要使用它。它类似于:{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 <your password here>
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,sambaPwdCanChange,
sambaPwdMustChange,sambaAcctFlags,displayName,sambaHomePath,sambaHomeDrive,
sambaLogonScript,sambaProfilePath,description,sambaUserWorkstations,sambaPrimaryGroupSID,
sambaDomainName,sambaSID,sambaGroupType,sambaNextRid,sambaNextGroupRid,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 < use the password you created for Manager in "Step 4: Configure slapd.conf">

# 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,就请参考文档。

  1. 从命令行启动 authconfig。输入:authconfig
  2. 按照下图编辑第一个屏幕。
    图 2. authconfig 屏幕 1
     
  3. 按照下图编辑第二个屏幕。
    图 3. authconfig 屏幕 2
     

步骤 7:启动 OpenLDAP

现在启动 LDAP 服务器。从二进制 RPM 进行安装的 Fedora 用户应该能够执行以下命令:/etc/init.d/ldap start

配置 Samba

现在将注意力转到 Samba 上 —— 安装、创建目录和配置。

[1] [2] [3]

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •