让Postfix使用保存在MySQL中的加了密的密码字段(Ubuntu 6.06 TLS)

这两天在Ubuntu下折腾Postfix + Courier-IMAP + Cyrus-SASL + MySQL + PostfixAdmin,主要参考这个Howto(http://flurdy.com/docs/postfix/)
配置了Postfix Admin,所以邮件用户的密码用了md5的加密方式存放在MySQL数据库中的(不想用明文)。这样一来,IMAP服务器可以支持加密的密码字段,认证没有问题,但SMTP服务器不行。

Google了好久,发现网上大多数都是使用明文密码的,也有用saslauthd的pam认证方式来做的。我一开始是用明文的,觉得不好(能看到用户的密码,心里不安),又试了pam方式但没成功(不知道为什么,没深究)。最后终于找到牛人给Cyrus-SASL写的patch(针对2.1.19版本的),安装步骤参考 http://blog.matroid.org/display/26


$ cd ~
$ apt-get source libsasl2
$ sudo apt-get build-dep libsasl2
下载这个为Debian修改过的patch文件
$ wget http://blog.matroid.org/files/20060116-crypted-passwords-for-couriour-and-postfix-in-mysql/cyrus-sasl-2.1.19-checkpw.c.patch_mgr
$ cd cyrus-sasl-2.1.19.dfsg1
$ tar xvzf cyrus-sasl-2.1.19.dfsg1.tar.gz
$ ln -s cyrus-sasl-2.1.19.dfsg1 cyrus-sasl-2.1.19.orig
$ cp ../cyrus-sasl-2.1.19-checkpw.c.patch_mgr debian/patches/28_cyrus-sasl-2.1.19-checkpw.c.patch
做些修改,将patch文件里的cyrus-sasl-2.1.19.org替换为cyrus-sasl-2.1.19.dfsg1,不然会patch会失败。改好后,编译
$ fakeroot debian/rules binary
成功后,安装生成的deb包
$ cd ..
$ sudo dpkg -i *.deb

配置/etc/postfix/sasl/smptd.conf

pwcheck_method: auxprop
auxprop_plugin: sql
allowanonymouslogin: no
allowplaintext: yes
mech_list: PLAIN LOGIN
srp_mda: md5

srvtab: /dev/null
opiekeys: /dev/null

password_format: crypt

sql_user: username
sql_passwd: password
sql_hostnames: sql.example.org
sql_database: database_name
sql_select: SELECT password FROM mailbox WHERE username = '%u@%r'

重启Postfix就可以了。

Ubuntu 6.06 下SquirrelMail的中文界面问题

SquirrelMail的系统配置为中文,用户设置也是中文,可以正常查看中文邮件,但界面还是英文。

系统已经安装了language-support-zh中文支持包。

原因:Ubuntu的默认系统字符集是UTF-8,而SquirrelMail需要的是GB2312(见SquirrelMail的程序目录functions/i18n.php中第843到846行)

解决办法(从SquirrelMail官网的wiki里得到启示):


$ sudo cp /var/lib/locales/supported.d/local /var/lib/locales/supported.d/local.old
$ grep zh_CN /usr/share/i18n/SUPPORTED
zh_CN.GB18030 GB18030
zh_CN.GBK GBK
zh_CN.UTF-8 UTF-8
zh_CN GB2312

$ sudo vi /var/lib/locales/supported.d/local
把上面的内容添加到local文件里

$ grep zh_CN /var/lib/locales/supported.d/local
zh_CN.GB18030 GB18030
zh_CN.GBK GBK
zh_CN.UTF-8 UTF-8
zh_CN GB2312

$ sudo dpkg-reconfigure --force locales
生成新的中文locale,可能比较慢

这时访问SquirrelMail的界面就是中文的了。

vsftpd结合pam_mysql 安装配置总结

前一段时间,论坛里总有朋友问到pam-mysql的安装问题。比较典型的有:
vsftp1.2+mysql4.1+pam_mysql0.5在RedHat AS4(32bit)下好像是有bug
还是mysql虚拟用户和vsftpd的登陆问题?

为了解答类似的问题,我动手试验了一下vsftpd+pam-mysql的配置过程,总结如下:

系统环境:

  • RedHat AS 4
  • MySQL 4.1.15
  • pam_mysql-0.7pre3

说明一下,MySQL我是使用其官方网站的rpm包安装的,包括下面这4个:

  1. MySQL-server-standard-4.1.15-0.rhel4.i386.rpm
  2. MySQL-client-standard-4.1.15-0.rhel4.i386.rpm
  3. MySQL-devel-standard-4.1.15-0.rhel4.i386.rpm
  4. MySQL-shared-standard-4.1.15-0.rhel4.i386.rpm

vsftpd是RedHat自带的。

建立用于存放vsftpd虚拟用户的Schema的过程:
mysql> create database vsftpd;

mysql> use vsftpd;

mysql> create table users (
-> id int AUTO_INCREMENT NOT NULL,
-> name char(16) binary NOT NULL,
-> passwd char(48) binary NOT NULL,
-> primary key(id)
-> );

mysql> describe users;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| name | char(16) | | | | |
| passwd | char(48) | | | | |
+--------+----------+------+-----+---------+----------------+

mysql> create table logs (msg varchar(255),
-> user char(16),
-> pid int,
-> host char(32),
-> rhost char(32),
-> logtime timestamp
-> );

mysql> describe logs;
+---------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+-------------------+-------+
| msg | varchar(255) | YES | | NULL | |
| user | varchar(16) | YES | | NULL | |
| pid | int(11) | YES | | NULL | |
| host | varchar(32) | YES | | NULL | |
| rhost | varchar(32) | YES | | NULL | |
| logtime | timestamp | YES | | CURRENT_TIMESTAMP | |
+---------+--------------+------+-----+-------------------+-------+

这里,用户密码这个字段的长度是48。这是根据MySQL加密函数的返回值的长度确定的。关于PASSWORD函数返回值的长度,可以参考这个:

http://dev.mysql.com/doc/refman/4.1/en/password-hashing.html

mysql> select encrypt('foo');
+----------------+
| encrypt('foo') |
+----------------+
| 4Wwn2AXFYb.So |
+----------------+

mysql> select password('foo');
+-------------------------------------------+
| password('foo') |
+-------------------------------------------+
| *F3A2A51A9B0F2BE2468926B4132313728C250DBF |
+-------------------------------------------+

mysql> select md5('foo');
+----------------------------------+
| md5('foo') |
+----------------------------------+
| acbd18db4cc2f85cedef654fccc4a4d8 |
+----------------------------------+

编译安装pam_mysql
# ./configure --with-openssl
# make
# make install
加上--with-openssl可以避免make时报有关md5.h的编译错误

建立/etc/pam.d/vsftpd.mysql(因为只是想验证pam_mysql的安装过程,所以我不想覆盖原有的vsftpd这个文件)。注意只有两行,auth是一行,account是一行。

auth required /lib/security/pam_mysql.so user=root passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=passwd crypt=2 sqllog=1 logtable=logs logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=logtime verbose=1
account required /lib/security/pam_mysql.so user=root passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=passwd crypt=2 sqllog=1 logtable=logs logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=logtime verbose=1

注意这里pam_mysql.so的路径是/lib/security;指定了sqllog;加密方式是2,也就是用MySQL PASSWORD()函数;verbose=1,设置这个可以帮助调试,日志信息输出在/var/log/messages里。

建立/etc/vsftpd/vsftpd.mysql.conf(同样,不影响已有的vsftpd服务,执行service vsftpd restart时会启动两个vsftpd服务,端口不一样)
主要的设置如下:

pam_service_name=vsftpd.mysql
listen=YES
tcp_wrappers=YES
local_enable=YES
guest_enable=YES
guest_username=ftp
listen_port=2121

注意pam_service_name=vsftpd.mysql指定了使用刚才设置的pam_mysql。

插入用户信息:
mysql> insert into users (name,passwd) values('tom',password('foo'));
mysql> insert into users (name,passwd) values('jerry',password('bar'));
mysql> select * from users;
+----+-------+-------------------------------------------+
| id | name | passwd |
+----+-------+-------------------------------------------+
| 1 | tom | *F3A2A51A9B0F2BE2468926B4132313728C250DBF |
| 2 | jerry | *E8D46CE25265E545D225A8A6F1BAF642FEBEE5CB |
+----+-------+-------------------------------------------+

启动vsftpd服务,测试配置:
# ftp localhost 2121

登录失败,检查/var/log/messages,发现:
# tail -f /var/log/messages
Nov 29 14:52:04 javadev vsftpd[17683]: PAM unable to dlopen(/lib/security/pam_mysql.so)
Nov 29 14:52:04 javadev vsftpd[17683]: PAM [dlerror: /lib/security/pam_mysql.so: cannot open shared object file: No such file or directory]
Nov 29 14:52:04 javadev vsftpd[17683]: PAM adding faulty module: /lib/security/pam_mysql.so

看来是没找到pam_mysql.so,怎么会呢?
用find(也可以用locate,不过得先updatedb一下,慢)找了一下,原来make install的时候默认安装在/usr/local/lib下。修改/etc/pam.d/vsftpd.mysql
auth required /usr/lib/security/pam_mysql.so user=root passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=passwd crypt=2 sqllog=1 logtable=logs logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=logtime verbose=1
account required /usr/lib/security/pam_mysql.so user=root passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=passwd crypt=2 sqllog=1 logtable=logs logmsgcolumn=msg logusercolumn=user logpidcolumn=pid loghostcolumn=host logrhostcolumn=rhost logtimecolumn=logtime verbose=1

在登录,成功!换用其他加密方式,也都可以。

然后试验pam_mysql v0.7新加的config_file配置选项。这个选项用来指定一个配置文件,可以把所有pam_mysql的配置放在这个文件中。这样的话,/etc/pam.d/vsftpd.mysql的内容变成这样:

auth required /usr/lib/security/pam_mysql.so config_file=/etc/security/pam_mysql.conf
account required /usr/lib/security/pam_mysql.so config_file=/etc/security/pam_mysql.conf

清爽了许多吧,Laughing

/etc/security/pam_mysql.conf的内容:

users.host=localhost
users.database=vsftpd
users.db_user=root
users.db_passwd=123456
users.table=users
users.user_column=name
users.password_column=passwd
users.password_crypt=3
verbose=1
log.enabled=1
log.table=logs
log.message_column=msg
log.pid_column=pid
log.user_column=user
log.host_column=host
log.rhost_column=rhost
log.time_column=logtime

改好这些以后,用之前建好的虚拟用户登录,居然不行!而且这次/var/log/messages里没有任何错误消息。ls -ltr /var/log 发现secure这个文件最新,试着打开,果然发现了pam_mysql的调试信息:

Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option verbose is set to "1"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.enabled is set to "1 "
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.table is set to "logs"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.message_column is set to "msg"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.pid_column is set to "pid"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.user_column is set to "user"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.host_column is set to "host"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.rhost_column is set to "rhost"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - option log.time_column is set to "logtime"
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - pam_sm_authenticate() called.
Dec 26 16:18:37 javadev vsftpd[6175]: pam_mysql - pam_mysql_open_db() called.
Dec 26 16:18:42 javadev vsftpd[6175]: pam_mysql - MySQL error (Unknown MySQL server host 'localhost ' (3))
Dec 26 16:18:42 javadev vsftpd[6175]: pam_mysql - pam_mysql_open_db() returning 5.
Dec 26 16:18:42 javadev vsftpd[6175]: pam_mysql - pam_sm_authenticate() returning 9.
Dec 26 16:18:42 javadev vsftpd[6175]: pam_mysql - pam_mysql_release_ctx() called.
Dec 26 16:18:42 javadev vsftpd[6175]: pam_mysql - pam_mysql_destroy_ctx() called.
Dec 26 16:18:42 javadev vsftpd[6175]: pam_mysql - pam_mysql_close_db() called.

仔细检查,发现原因在这里:

pam_mysql - MySQL error (Unknown MySQL server host 'localhost ' (3))

原来配置文件里users.host=localhost 这行行尾多了一个空格!郁闷!修改以后就可以登录了。

最后,我在论坛里回复时说过不能用password这种方式,原因是

2 (or "mysql") = Use MySQL PASSWORD() function. It is possible
that the encryption function used by PAM-MySQL
is different from that of the MySQL server, as
PAM-MySQL uses the function defined in MySQL's
C-client API instead of using PASSWORD() SQL function
in the query.

事实证明我说错了。

还有,两个log可以帮助分析故障(当然可能仅在RedHat下),/var/log/messages和/var/log/secure