pam和sasl

这几天使用在Postfix搭建一个Webmail的平台,用户认证这一块最终使用了PAM。想整理一下思路,让自己对PAM有个更加清晰的认识。

1.      PAM的简介

PAM全称是:Pluggable Authentication Modules。在当前的CentOS中都是用这个模块作各种认证通道。具体可以man 一下pam。

PAM的主要配置文件都集中在/etc/pam.d目录里面,里面有多个文件,每个文件表示了一种服务的认证方法,例如:sshd,login。

管理员可以根据自己的需要定义出自己的服务的认证配置,详细的配置信息可以man pam.d

2.      SASL的简介

在对PAM做测试之前,需要一个sasl的工具集。SASL是一个用于认证处理的协议,全称是Simple Authentication and Security Layer。这个协议在rfc4422中定义,具体作用大概如下:

                  SMTP    LDAP    XMPP   Other protocols ...

                     \       |    |      /

                      \      |    |     /

                     SASL abstraction layer

                      /      |    |     \

                     /       |    |      \

              EXTERNAL   GSSAPI  PLAIN   Other mechanisms ...

从上面图可以看到,不同的应用通过SASL使用各种方法来进行用户认证。SASL的作用是为各种协议产生一个认证的抽象层,让用户登录的时候不用理会最终用户认证信息的出处。

一般情况下,当前的centos系统都会安装这套工具。在我的系统中,对应的版本是:

[root@kernel pam_mysql-0.7RC1]# rpm -ql cyrus-sasl-2.1.22-5.el5_4.3

/etc/rc.d/init.d/saslauthd

/etc/sysconfig/saslauthd

/usr/lib/sasl2

/usr/lib/sasl2/libsasldb.la

/usr/lib/sasl2/libsasldb.so

/usr/lib/sasl2/libsasldb.so.2

/usr/lib/sasl2/libsasldb.so.2.0.22

/usr/sbin/dbconverter-2

/usr/sbin/pluginviewer

/usr/sbin/saslauthd

/usr/sbin/sasldblistusers2

/usr/sbin/saslpasswd2

/usr/sbin/testsaslauthd

/usr/share/doc/cyrus-sasl-2.1.22

/usr/share/doc/cyrus-sasl-2.1.22/LDAP_SASLAUTHD

/usr/share/man/man8/pluginviewer.8.gz

/usr/share/man/man8/saslauthd.8.gz

/usr/share/man/man8/sasldblistusers2.8.gz

/usr/share/man/man8/saslpasswd2.8.gz

/var/run/saslauthd

下面是SASL的配置文件,这个配置文件说明,SASL使用PAM作为获取认证数据的机制。

[root@kernel pam_mysql-0.7RC1]# cat /etc/sysconfig/saslauthd

# Directory in which to place saslauthd's listening socket, pid file, and so

# on. This directory must already exist.

SOCKETDIR=/var/run/saslauthd

# Mechanism to use when checking passwords. Run "saslauthd -v" to get a list

# of which mechanism your installation was compiled with the ablity to use.

MECH=pam

# Additional flags to pass to saslauthd on the command line. See saslauthd(8)

# for the list of accepted flags.

FLAGS=

3.      使用SASL工具对PAM进行测试

我们可以用下面方法来对sasl和pam进行测试:

[root@kernel pam.d]# testsaslauthd -u root -p xxxx -s su

0: OK "Success."

[root@kernel pam.d]# testsaslauthd -u root -p xxxx -s sshd

0: OK "Success."

[root@kernel pam.d]# testsaslauthd

testsaslauthd: usage: testsaslauthd -u username -p password

[-r realm] [-s servicename]

[-f socket path] [-R repeatnum]

在上面例子中,xxxx要修改为真正的密码。

4.      使用MYSQL作为PAM的数据源

我们还可以用mysql作为PAM存储用户数据的数据库。

如果使用这种方法,我们需要安装pam_mysql。可以在下面链接中获得:

http://sourceforge.net/projects/pam-mysql/

具体的安装方法可以看INSTALL,具体的配置方法可以看里面的README。

安装很简单,就是configure一下,然后执行make,最后把生成的库拷贝到pam的库目录中:

[root@kernel pam_mysql-0.7RC1]# cp .libs/pam_mysql.so /lib/security/

下面是一个配置例子:

[root@kernel pam.d]# testsaslauthd -u cyrus -p test -s smtp

0: OK "Success."

[root@kernel pam.d]# cat smtp

auth sufficient pam_mysql.so user=root passwd=admin host=localhost db=postfix table=accountuser usercolumn=username passwdcolumn=password

auth sufficient pam_unix_auth.so

account required pam_mysql.so user=root passwd=admin host=localhost db=postfix table=accountuser usercolumn=username passwdcolumn=password

account sufficient pam_unix_acct.so

smtp中主要是定义了mysql的连接方式和对应的库表,字段。具体可以看READM文件和man pam.d。

根据上面的配置和测试,需要创建数据库postfix,登录帐号密码是root/admin,表是accountuser,列有:username,password。

创建用户信息:cyrus/test。插入数据SQL:INSERT INTO accountuser(username, password) VALUE('cyrus','test')

5.      小结

在这里面对pam和sasl做了入门介绍,希望对大家有帮助。如果有什么问题可以给我发邮件:bljb@qq.com

下面是相关的资料:

l  http://cyrusimap.org/docs/cyrus-sasl/2.1.25/

l  http://sourceforge.net/projects/pam-mysql/files/?source=navbar

posted @ 2016-09-18 17:20  lpfuture  阅读(1895)  评论(0编辑  收藏  举报