linux 中关于PAM的点滴笔记

时间:2022-07-22
本文章向大家介绍linux 中关于PAM的点滴笔记,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

pam在linux系统中是非常常用,也是非常重要的一个子系统,然而对于pam,我们可能并没有太多的关注其man 帮助文件的说明,最近读了一下pam的man page, 许多不明白的豁然开朗,这里做一个简单的笔记: A. pam.d 是一个目录,一般情况下 关于pam的配置都在这个目录下,其实还有一个配置文件 pam.conf ,不过一般都不存在,而在pam.conf这个配置文件中,关于语法的格式,在man pam.conf 或者man pam.d 的时候,有这么一段:

       The format of each rule is a space separated collection of tokens, the first three being case-insensitive:
        service type control module-path module-arguments
       The syntax of files contained in the /etc/pam.d/ directory, are identical except for the absence of any 
service field. In this case, the  service is the name of the file in the /etc/pam.d/ directory. 
This filename must be in lower case.

简单的解释下就是: 每一条规则占用一行,然后是用空格作为分隔符号,其表示格式为: service type control module-path module-arguments 而在 /etc/pam.d 下的配置文件中,则缺少了第一列 service 的指定,因为 pam.d下的配置文件的名称就是用对应的service的名字来命名的,所以在pam.d下的pam配置文件中,已经不再包含service 这一列了, 并且这个pam.d 下的有效的pam 配置文件的file name 必须是小写的. 这时候问题就来了,pam.d 下面不可能对所有的service/command 都写了一个对应的pam 配置文件,怎么办呢? 这时候 在pam.d 下面,我们可以发现一个叫做 other的配置文件,这个配置文件就是用于处理 匹配不到service name 的状况。 B. 在pam.d下的pam的配置文件中,如果对应的pam module文件不存在,是不是都会报错呢? 这个也不完全是这样,同样的在man page中找到了这么一段话:

If the "type" value from the list above is prepended with a ‘-’ character the PAM library will not log to the system log if it is not possible to
       load the module because it is missing in the system. This can be useful especially for modules which are not always installed on the system
       and are not required for correct authentication and authorization of the login session.

大致意思是: 如果type 参数有前缀“-”, 那么对于找不到pam module 的这种情况,不会记录到system log, 也就是我们通常所说的messages. 这个情形适用于: 对应的模块对正确认证 和login 的session 认证没有影响的pam module. C. 对于control value的控制,一般情况下都是一个值,比如:required,requisite, sufficient, optional, include,substack 等,但是也支持多个值的情况:

For the more complicated syntax valid control values have the following form:

                 [value1=action1 value2=action2 ...]

       Where valueN corresponds to the return code from the function invoked in the module for which the 
line is defined. It is selected from one of
       these: success, open_err, symbol_err, service_err, system_err, buf_err, perm_denied, auth_err, 
cred_insufficient, authinfo_unavail,
       user_unknown, maxtries, new_authtok_reqd, acct_expired, session_err, cred_unavail, cred_expired,
 cred_err, no_module_data, conv_err,
       authtok_err, authtok_recover_err, authtok_lock_busy, authtok_disable_aging, try_again, ignore, 
abort, authtok_expired, module_unknown,
       bad_item, conv_again, incomplete, and default.
       The last of these, default, implies 'all valueN's not mentioned explicitly. Note, the full list of PAM 
errors is available in
       /usr/include/security/_pam_types.h. The actionN can take one of the following forms:
......
......

D. 常用的pam 模块简介:

1). pam_unix

这个主要用于auth, account, paasword 的控制,通常登陆时候PAM都会调用这个模块来验证所用的account, 以及登陆的凭证,也用于凭证的更新管理. 其中值的一提的是 可以控制用户设置新密码不能和过去的n此重复,这时候用 remember 参数.

2). pam_shells

用于auth, account的控制,通过检查帐号对应的shell是否在/etc/shells来返回是否允许登陆;

3). pam_securetty

用于控制root 可以登陆的tty 终端 , 可以用于auth 类。

4). pam_listfile

这个用于访问管理,基于用户指定的配置文件,配置文件中的格式比较灵活,基于user,group, ip,network ......所以可以实现很多种访问控制, 比如可以通过这个模块来实现用户的crond服务不受密码过期的影响,只需要在crond的pam 配置文件中的添加(需要在调用pam_unix之前进行添加): account sufficient pam_listfile.so item=user sense=allow file=/etc/cron.allow onerr=succeed quiet

5). pam_cracklib

这个模块用于修改密码时候检查密码的强度. 这里简单介绍下几个参数的意义: minlen, dcredit,ucredit, lcredit, ocredit 并不简单表示对应的长度. a).后面4个参数的值可以是负数,比如-1, 表示至少需要一个对应类型的字符,也可以是-2,-3...。字符的分值默认为1. b).后面4个参数的值是正数,那么表示该类型的每个字符对应的 ”分值“,也就是credit, 而用户设置的密码是基于每个字符的分值计算出来的总数,如果满足minlen,那么表示符合要求,否则就不符合要求。 c).如果后面4个参数的值是0, 表示该类型的字符可以有,也可以没有,而如果有,那么该类型的字符对应的"分值“为1. 但是对最终密码计算出来的总分值要满足minlen.

6). pam_pwhistroy

该模块依然用于确保密码强度,不过是从password history 的角度对密码强度进行保护的;

7). pam_limits

用于控制系统资源的使用上限,root用户也会受到影响.

8).pam_access

这个模块用于访问管理,主要根据配置文件实现管理,配置文件中可以指定允许/拒绝的ip,域名,终端... 以及权限等;其和pam_listfile 比较像. 但是没有pam_listfile 强大, 所以其实用好pam_listfile 就够了:

9).pam_rootok

这个模块主要是判断当前用户的uid是否为0,如果为0, 那么就返回pam_succeed. 也就是说,如果加了下面这一条规则,只要当前服务的uid 为0, 那么就无需再次输入密码就可以直接通过认证: auth sufficient pam_rootok.so

这里需要注意的是:如果一个应用程序的权限认证基于os,并且无论什么情况都需要输入密码进行认证,那么该应用的pam配置一定不能使用 pam_rootok这个模块,否则 os层面的root用户可以没有密码的状态直接登陆应用,因为os层面的root用户登陆的时候,那么检测到的UID就是0, 所以”auth sufficient pam_rootok.so" 的存在,就导致 直接pam_success的结果,虽然登陆的ID不是root, 但是执行login的这个操作的id 是root. 而有一些应用,则需要加上这个模块,比如su的pam配置文件,我们在root 身份下,su到其他的用户不需要密码就是这个原因.
10). pam_wheel

这个模块限制了只有wheel group的用户才可以执行su 操作,其他非wheel group 的用户都被禁止. 因为su 是一个不安全的命令.

11). pam_userdb

这个模块指定用户的认证是通过Berkeley db 来实现,而不是通过传统的shadow文件. 这个认证方式的典型应用是:虚拟用户。所谓虚拟用户,就是指在os层面并不存在的用户,但是某个应用中所需要用到的用户,典型的比如 ftp 用户,这些用户不需要在os层面创建. 用这种方式进行认证比较好. 确保了系统安全,也方便管理.

12). pam_tally2

这个主要控制登陆失败的次数,通常失败三次就锁住的功能就是该模块实现的.

本文原创,转载请著名出处