使用 PAM 集成 OpenLDAP 实现 Linux 统一管理系统用户-2
 
- UID
- 1066743
|

使用 PAM 集成 OpenLDAP 实现 Linux 统一管理系统用户-2
到现在为止,服务器基本就配置完成,在启动 slapd 之前可以使用 slaptest 测试一下,slaptest 在/usr/sbin 目录下。出现 config file testing succeeed 就表示配置没有问题了。
清单 4. 测试 OpenLDAP 配置:1
2
3
| #/usr/sbin/slaptest
bdb_monitor_db_open: monitoring disabled; configure monitor database to enable
config file testing succeeded
|
清单 5. 启动 OpenLDAP:1
2
| #/usr/local/openldap/libexec/slapd
root@localhost.loca ldomain:/usr/local/src/openldap-2.4.30/servers/slapd
|
可以用 ps -aux 查看,或用以下命令查询服务器,测试服务启动是否成功:
1
| ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts
|
如果命令执行成功,返回一些信息,则说明服务器正常运行了。如果启动不成功,它会提示一些出错信息,多数是 slapd.conf 配置出错。仔细核查一下配置文件。如果 OpenLDAP 配置为非匿名访问,请在 ldapsearch 命令中加入-D 和-w 参数。
配置 OpenLDAP 客户机手工配置 OpenLDAP 客户机,编辑/etc/openldap/ldap.conf。
清单 6. ldap.conf 示例:1
2
3
4
5
6
7
8
9
10
11
| TLS_REQCERT never
URI ldap://localhost:389
base dc=base,dc=com
binddn cn=manager,dc=base,dc=com
bindpw crypt{ae2ec15710d547a0}
searchfilter (&(uid=%v)(objectclass=person))
pam_login_attribute uid
ldap_version 3
timelimit 30
bind_timelimit 30
pam_lookup_policy yes
|
URI :访问LDAP server的URI,默认端口为389;启用SSL/TLS时,使用ldaps://host,端口为636。
base :指定默认的base DN。
binddn: 指定默认的bind DN。
bindpw: 认证用户的密码,建议使用密文。
基于 PAM 的 LDAP 用户身份验证基于 PAM 的 LDAP 身份验证要想正常的工作,还需要配置其他服务:系统命名服务和身份验证服务。
系统命名服务(NSS)需要配置为使用 LDAP 来解析诸如用户和组帐号之类的资源,即将用户的 uid 解析为用户名。由于未来用户都存储在 LDAP 目录中,因此系统需要配置成同时对 /etc/passwd 文件和 LDAP 目录中的帐号进行解析。这种功能是通过 /usr/lib/libnss_ldap.so 库提供的。
身份验证服务是实际执行 LDAP 验证用户身份的服务。Linux-PAM 提供了本地 Linux 身份验证服务。基于 PAM 的 LDAP 身份认证首先在本地的 /etc/passwd 文件检查用户帐号,如果用户在/etc/passwd 中存在,则 PAM LDAP 模块可以将身份验证重定向到 LDAP 目录上。并进行密码的校验,/lib/security/pam_ldap.so PAM 模块提供了 LDAP 身份验证功能。如果用户在/etc/passwd 中不存在,则对 LDAP 服务器进行检查,用户在 LDAP 上存在,且用户所在的组有权限登录 Linux 操作系统,则 PAM 身份认证程序自动在 Linux 操作系统上创建同名的用户,并将用户加入默认的组。以后用户只需要在 LDAP 上维护,不需要在 Linux 操作系统上维护。从而减轻了管理员的维护工作,也是用户免去了记录多个用户 ID 和密码的烦恼。
图 1. 基于 PAM 的 LDAP 身份验证流程 PAM 身份验证服务程序Linux 系统成功配置 OpenLDAP 后,为了与 PAM 集成统一管理用户,需要开发 PAM 相关的 so 文件实现用户的身份验证,在本文中 so 文件命名为 pam_ldap_union.so。当 LDAP 用户首次登陆 Linux terminal 时,首先判断用户在 Linux 操作系统中是否存在。如果用户在操作系统中存在,则直接返回 PAM_SUCCESS。如果用户不存在,则查询 OpenLDAP server。如果用户在 OpenLDAP server 存在,并且用户所在的组具有登陆 Linux 操作系统的权限,则在本地创建同名的用户,并给用户分配相应的权限。
清单 7. pam_ldap_union.so 代码示例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
| //判断用户在 Linux 操作系统中是否存在。用户存在,直接返回 PAM_SUCCESS;否则,查询 OpenLDAP server。
int pam_sm_authenticate(pam_handle_t *pamh,int flags,int argc,const char **argv)
{
rc = pam_get_user(pamh, &user, NULL);// the user name is in the authentication handle – pamh
rc = pam_get_item(pamh, PAM_SERVICE, (const void**)&service);// get current user service.
localuser = getpwnam(user); // get user from /etc/passwd
if ( localuser != NULL ) {
return PAM_SUCCESS;
}
……
}
//从文件/etc/openldap/ldap.conf 获取 binddn 的 bindpw 值,作为 ldapsearch 命令的参数。
FILE *fp = fopen("/etc/openldap/ldap.conf", "r");
int rc=0;
char ldapsearch[300];
//通过 ldapsearch 命令查询当前用户是否在 LDAP server 中。
sprintf(ldapsearch, "/usr/bin/ldapsearch -x -D \"%s\" -w \"%s\" cn=%s | grep \"=%s,\" 2>&1",
binddn, bindpw, group, user);
rc = system(ldapsearch);
if(rc == 0){
printf ("pam_ldap_union: user %s found in LDAP directory", user);
break;
}else {
printf("pam_ldap_union: user %s not found in LDAP directory ,local account will not be created",
user, user);
if(i == 5){
return(PAM_AUTH_ERR);
}
}
char useradd[100];
char *homeDir = "-m";
char *primarygroup = "-g users";
char *supplgroup = "-G \'\'";
char *shell = "-s /bin/smrsh";
//如果用户在 LDAP server 中,通过 useradd 命令在 Linux 操作系统创建同名用户。
sprintf(useradd, "/usr/sbin/useradd %s %s %s %s %s >/dev/null 2>&1", homeDir, primarygroup,
supplgroup, shell, user);
rc = system(useradd);
if ( rc != 0 ) {
printf("pam_ldap_union: useradd command failed with return code %d", rc);
}
return PAM_SUCCESS;
|
PAM 身份验证配置要让 PAM 身份验证服务使用 OpenLDAP 服务器中的用户,将 pam_ldap 和 pam_ldap_union 加入到 PAM 配置文件/etc/pam.d/login 中,/etc/pam.d/login 中包含了 common-auth、common-account、common-password。将 pam_ldap 和 pam_ldap_union 按照以下方式加入到相应配置文件中。
清单 8. login 文件清单1
2
3
4
5
6
7
8
9
10
11
| #%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
|
清单 9. common-auth 文件清单1
2
3
4
5
| auth required pam_tally2.so onerr=fail deny=20 unlock_time=3600 silent
auth required /lib/security/pam_ldap_union.so
auth sufficient /lib/security/pam_ldap.so config=/etc/openldap/ldap.conf
ignore_authinfo_unavail ignore_unknown_user
auth required pam_unix_auth.so try_first_pass delay #nullok set_setrpc
|
清单 10. common-account 文件清单:1
2
3
| account required pam_tally2.so
account required pam_unix_acct.so
account sufficient /lib/security/pam_ldap.so config=/etc/openldap/ldap.conf
|
清单 11. common-password 文件清单1
2
3
| Password required pam_cracklib.so type=#
password sufficient /lib/security/pam_ldap.so config=/etc/openldap/ldap.conf use_authtok
password required pam_unix_passwd.so use_first_pass sha512
|
清单 12. common-session 文件清单1
2
3
| session required pam_limits.so
session required pam_unix_session.so debug # none or trace
session optional pam_umask.so
|
LDAP 用户的统一管理经过以上 PAM 程序的开发和配置,LDAP 用户以后可以直接登陆 Linux 操作系统。企业中的用户只需要记录一套用户名和密码,就可以登录企业其他应用服务。当 LDAP server 上的用户出现新增、修改、删除等变化时,用户可以开发一套脚本,实现 Linux 操作系统和 LDAP server 上用户的同步。主要使用的命令如下:
新增用户使用的命令:
LDAP:ldapadd,例如 ldapadd -x -D <bindDN> -w <password>
Linux:useradd,例如 useradd –m –g <group> -s <shell> <name>
修改用户使用的命令:
LDAP:ldapmodify 或者 ldappasswd,ldappasswd 用于设置密码。
Linux:usermod,修改用户相关信息。
删除用户使用的命令:
LDAP:ldapdelete,例如 ldapdelete -x -D <bindDN> -w secret <userinfo>
Linux:userdel <username>
用户也可以不开发此类脚本,这样在操作系统上会存在一些冗余的用户,但是不影响用户的登陆,凡是在 LDAP server 上作废的用户,是无法在操作系统上登陆的。
基于 PAM 的 LDAP 用户统一管理的应用随着企业业务的不断发展,企业的信息系统随之增加,用户信息相对分散,为保障系统的安全正常使用和信息的一致性,需要将分散的用户信息整合集中进行统一管理,LDAP 是用户首选的用户管理工具,由 LDAP 统一管理企业的所有用户,能够快速构建出符合企业自身用户信息管理的需求,消除应用系统间用户信息孤岛,实现人员集中管理和企业各级分散的授权管理,形成人员和应用统一入口、统一管理。Linux-PAM 与 LDAP 的集成,可以用来管理企业的 Linux 服务器的用户。LDAP 服务器上有非常多的用户,属于不同组的用户具有不同的权限,只要将用户添加到相应的组,用户便有权限登陆操作系统或维护企业的 Linux 操作系统服务器,虽然用户可以在 LDAP 上维护,但是用户的权限信息还是维护在各自的应用系统上。在一些拥有几十万员工和以及拥有数以千计的服务器的企业中,这种应用能够简化管理员的维护工作,也能免去用户繁琐的记录多套用户名和密码的烦恼。
结束语本文通过对 Linux-PAM 的介绍,和 OpenLDAP 的安装与配置,详细讲述了如果通过将 Linux-PAM 和 OpenLDAP 结合来统一管理企业的用户的过程,企业在未来的发展中,应用系统、服务器、以及企业的用户一定越来越多,通过 Linux-PAM 和 OpenLDAP 的集成可以有效地管理企业的用户,既减轻了管理员维护的工作,也是用户免去记录多套用户名和密码的烦恼,减少了企业的维护成本。 |
|
|
|
|
|