首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 PAM 集成 OpenLDAP 实现 Linux 统一管理系统-1用户

使用 PAM 集成 OpenLDAP 实现 Linux 统一管理系统-1用户

Linux-PAM 简介Linux-PAM (Linux 系统的可插入式认证模块) 是一套共享函数库,它表示一种性能健硕而且灵活方便的用户级认证方式,允许系统管理员来决定应用程序如何识别用户,即不需要 (重写和) 重新编译一个 (支持 PAM 的) 应用,就可以切换它所用的认证机制。目前,Linux-PAM 已经成为 Linux、BSD 和其它一些 Unix 操作系统的首选认证方式,特别是在 Linux 上,几乎所有的 daemon 和一些与授权有关的命令都通过 PAM 来进行验证。
Linux-PAM 主要是由一组共享库文件(share libraries, 也就是.so 文件)和一些配置文件组成。当用户请求服务时,具有 PAM 认证功能的应用程序将与这些.so 文件进行交互,以此判断是否可以授权给发起请求的用户来使用服务,比如 su、vsftp、httpd 等。如果认证成功,那么用户便可以使用该服务或命令;如果认证失败,用户将不能使用该服务,同时,PAM 将向指定的 log 文件写入警告信息。PAM 不依赖于任何应用或服务,用户完全可以升级这些应用或服务而不必管 PAM 的共享库的更新或升级,反之亦然。所以它非常的灵活。
PAM 的认证过程是通过对一些服务或应用的配置文件来控制的。在 Linux 上,PAM 配置信息通常位于目录 /etc/pam.d,但是 UNIX 系统把所有的配置都放在/etc/pam.conf 文件里。对于使用 PAM 的各项服务,/etc/pam.d 目录中都有一个对应的配置文件,PAM 的配置文件是一系列的单行配置命令构成,每行配置都说明一种在操作系统上使用的特殊 PAM 模块。
其一般格式为:
1
[service] module-type control-flag module-path [arguments]




PAM 配置文件中的字段包括:
  • service: 指定服务/应用程序的名称,如 telnet、login、ftp 等(默认值为 OTHER)。
  • module-type: 为 service 字段中的服务/应用程序指定模块类型,模块类型有四种类型(auth/account/session/password)。
  • control-flag: 指定了"配置段"里的模块应该怎么相互作用,可以理解为对 PAM 认证的流程控制,它可以获取诸如 requisite、required、sufficient 和 optional 之类的值。
  • module-path: 指定实现模块的库对象的路径名称。最好使用绝对路径,缺省路径一般是/lib/security 或/lib64/security。
  • arguments:指定可以传递给服务模块的选项或参数(可选字段)。
PAM 框架将按照配置文件中列出的条目顺序调用相应的模块,这取决于每个条目允许的 control_flag 的值。control_flag 指定了"配置段"里的模块应该怎么相互作用,生成该配置段的最终结果。control_flag 值包括:
  • required:表示这一模块的认证是必须成功的,如果认证失败,认证过程不会即刻终止,PAM 将继续下一个同类型的模块认证。
  • requisite:与 required 类似,只是如果认证失败,认证过程将立即终止。
  • sufficient:表示如果认证成功,那么对这一类的模块认证是充足的,其它的模块将不会再检验。
  • optional:表示这一模块认证是可选的,也不会对认证成功或失败产生影响。
  • include:类似于 DNS 的 include。表示将包括其他的一些认证,这样可以建立一个分离式的配置文件管理机制。
清单 1. PAM 配置文件的示例:
1
2
3
4
5
6
7
8
9
10
11
/etc/pam.d/login
#%PAM-1.0
auth          requisite      pam_nologin.so
auth          [user_unknown=ignore success=ok ignore=ignore auth_err=die default=bad]pam_securetty.so
auth          include        common-auth
account      include        common-account
password     include        common-password
session      required       pam_loginuid.so
session      include        common-session
session      required       pam_lastlog.so nowtmp
session      optional       pam_mail.so standard




PAM 配置文件来源于 SUSE Linux Enterprise Server11 操作系统。对于 PAM 的结构有了基本的了解,我们来看看 PAM 是如果控制 login 的,从上至下对于 login 的 PAM 配置文件的流程控制为:
  • login 的第一行 pam_nologin 模块检查有没有/etc/nologin 文件,如果有这个文件,除非是 root 用户,否则该模块就立即中止登陆。
  • 继续第二项 auth 认证,第二行以方括号括起来的 key/value 值的组合用来表示 control_flag 的值,pam_securetty.so 模块确保 root 用户只能从文件/etc/securetty 里列出的那些终端登陆到系统里,限制 root 用户在特殊设备上登陆。
  • 继续第三项 auth 认证,具体的验证规则包含在 common-auth 中。
  • 继续第四项 account 认证,具体的验证规则包含在 common-account 中
  • 继续第五项 password 认证,具体的验证规则在 common-password 中。
  • 继续第六项 session 的验证。注意:pam_loginuid.so 模块用来设置 loginuid 过程属性,其他的 session 验证过程,请参照 control-flag 的说明,不再一一赘述。
OpenLDAP 简介OpenLDAP 是轻型目录访问协议(Lightweight Directory Access Protocol,LDAP)的自由和开源的实现,是最常用的目录服务之一。在 Linux 的发型版本中提供的 OpenLDAP 软件按照客户机/服务器模式实现了轻量级目录访问协议(LDAP)。
LDAP 目录以树状的层次结构来存储数据(这很类同于 DNS),最顶层即根部称作"基准 DN",形如"dc=mydomain, dc=org"或者"o= mydomain.org",在根目录的下面有很多的文件和目录,为了把这些大量的数据从逻辑上分开,LDAP 像其它的目录服务协议一样使用 OU (Organization Unit),可以用来表示公司内部机构,如部门等,也可以用来表示设备、人员等。同时 OU 还可以有子 OU,用来表示更为细致的分类。
Linux 系统配置 OpenLDAP本文使用 OpenLDAP 2.4.30 来构建 LDAP 的服务器和客户机。操作系统使用 SUSE Linux Enterprise Server11。用户可以从 OpenLDAP 下载页http://www.openldap.org/software/download/或者ftp://ftp.openldap.org/pub/OpenLDAP/下载 OpenLDAP 软件到/usr/local,解压 OpenLDAP,并进入 OpenLDAP 的最外层目录,编译并安装 LDAP。
清单 2. 编译及安装 OpenLDAP 示例
1
2
3
4
5
6
7
8
#tarzxvfopenldap-stable-20120311.tgz
#cd /usr/local/openldap-2.4.30
#./configure −−with−wrappers
Please "make depend" to build dependencies
#make depend
#make
#make test
#make install




OpenLDAP 依赖于一些第三方软件包,因此安装配置 OpenLDAP 之前需要首先安装第三方软件包。执行"make install"命令要求用户有超级用户权限。安装成功后,可以配置 OpenLDAP 的服务器端和客户端。
配置 OpenLDAP 服务器OpenLDAP 安装成功后,用户会在/usr/local/etc/openldap 找到配置文件 slapd.conf。/usr/local/etc/openldap/slapd.conf 是 OpenLDAP 服务器最重要的配置文件,配置权限、口令、数据库类型和数据库位置等等。slapd.conf 文件中包括一系列全局配置选项,它们作为一个整体应用到 slapd 上面,后面是包含数据库特有信息的数据库后端定义。空行和以 "#" 字符开头的注释行都会被忽略。
清单 3. slapd.conf 示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#
# 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/rfc2307bis.schema
include         /etc/openldap/schema/yast.schema

# Define global ACLs to disable default read access.

# Do not enable referrals until AFTER you have a working directory
# service AND an understanding of referrals.
#referral       ldap://root.openldap.org

pidfile         /var/run/slapd/slapd.pid
argsfile        /var/run/slapd/slapd.args

# Load dynamic backend modules:
# modulepath    /usr/lib/openldap/modules
# moduleload    back_bdb.la
# moduleload    back_hdb.la
# moduleload    back_ldap.la

# Sample security restrictions
#       Require integrity protection (prevent hijacking)
#       Require 112-bit (3DES or better) encryption for updates
#       Require 63-bit encryption for simple bind
# security ssf=1 update_ssf=112 simple_bind=64

# Sample access control policy:
#       Root DSE: allow anyone to read it
#       Subschema (sub)entry DSE: allow anyone to read it
#       Other DSEs:
#               Allow self write access to user password
#               Allow anonymous users to authenticate
#               Allow read access to everything else
#       Directives needed to implement policy:
access to dn.base=""
        by * read

access to dn.base="cn=Subschema"
        by * read

access to attrs=userPassword,userPKCS12
        by self write
        by * auth

access to attrs=shadowLastChange
        by self write
        by * read

access to *
        by * read
#######################################################################
# BDB database definitions
#######################################################################

database        bdb
suffix          "dc=my-domain,dc=com"
rootdn          "cn=Manager,dc=my-domain,dc=com"
# Cleartext passwords, especially for the rootdn, should
# be avoid.  See slappasswd(8) and slapd.conf(5) for details.
# Use of strong authentication encouraged.
rootpw          secret
# The database directory MUST exist prior to running slapd AND
# should only be accessible by the slapd and slap tools.
# Mode 700 recommended.
directory       /var/lib/ldap
# Indices to maintain
index   objectClass     eq




下面解释一下配置文件中的设置:
database  bdb
定义使用的后端数据存储方式,数据库格式默认采用 Berkeley DB。其后可以跟的值有 bdb、ldbm、passwd 和 shell。bdb 指使用 Berkley DB 4 数据库。
suffix "dc=my-domain,dc=com"
suffix 是"LDAP 基准名",它是 LDAP 名字空间在这里的根。设置想要创建的子树的根的 DN(distinguished name),将 my-domain 替换成用户实际使用的 domain。
rootdn "cn=Manager,dc=my-domain,dc=com"
设置管理 LDAP 目录的超级用户的 DN。这个用户名不要出现在"/etc/passwd"文件里。
rootpw secret
设置这个数据库的超级用户的口令验证方式。也就是上面"rootdn"设置的用户的口令。千万不要用明文进行口令验证,一定要用加密的口令,可以使用的加密方式有:CRYPT、MD5、SMD5、SHA 和 SSHA。
directory  var/lib/ldap
设置 LDAP 数据库和索引文件所在的目录。在安装阶段指定 LDAP 的后台数据库。
access to dn.base=""
        by * read
access to dn.base="cn=Subschema"
        by * read
…...
用来定义访问控制列表。
attrs=userPassword  #指定"密码"属性
by self write      #用户自己可更改
by * auth  #所有访问者需要通过认证
返回列表