孤独雪鹰 发表于 2018-9-30 13:32:57

vsftp通过pam集成mysql身份验证

  1 前言
  需要解决由FTP上传的文件自动以某个应用程序的身份保存的唯一方式是使用VSFTP的虚拟用户,由于笔者对MySQL比较熟悉,所以利用PAM_MySQL模块将VSFTP与MySQL集成。另外,本文不但整理了CentOS6与CentOS7的安装配置方法,而且同时涵盖了RPM包与编译两种安装方法。
  2 理论基础
  2.1 PAM模块的介绍
  所谓虚拟用户,即通过pam模块将vsftp与数据库集成,将vsftp的传统身份验证方式变成通过数据库中保存的帐号密码验证,用户的身份并非系统用户,而需由pam模块虚拟出来的用户,故称虚拟用户。
  2.2 PAM模块的加载
vim /etc/pam.d/vsftpd  注:通过编辑pam.d目录下的规则实现加载
  2.3 PAM模块的选项
  配置例子:
auth       optional   pam_mysql.so user=root passwd=password  
account    required   pam_mysql.so user=root passwd=password
  其他选项
-- verbose 设置为1则显示详细的调试信息,默认0  
-- debug 开启调试模式
  
-- user mysql数据库认证的用户名
  
-- passwd mysql数据库认证的密码
  
-- host mysql数据库的主机名称或IP
  
-- db 包含验证信息的数据库名称
  
-- table 包含验证信息的数据库表名称
  
-- update_table 包含密码更高的数据库表名称,不配置默认赋予table的值
  
-- usercolumn 包含验证的用户名列
  
-- passwdcolumn 包含验证的密码列
  
-- statcolumn 标记用户状态的列
  
--- 0表示用户已经过期;
  
--- 1表示要求用户更改密码
  
-- crypt 密码加密方式,
  
--- 0无加密,默认为此值;
  
--- 1使用crypt(3)函数加密;
  
--- 2使用MySQL的PASSWORD函数加密;
  
--- 3使用普通十六进制md5加密;
  
--- 4使用普通十六机制SHA1加密;
  
-- md5 使用md5进行crypt(3)哈希,当crypt设置为“Y”才有效,默认false
  
-- use_323_passwd 使用MySQL版本3风格的加密功能(兼容迁移),默认false
  
-- where 查询的附加条件如
  
-- sqllog 设置为“true”或“yes”,则启用SQL日志记录,默认false
  
-- logtable 存储日志的表名称
  
-- logmsgcoumn 该列存储日志的消息
  
-- logusercolumn 该列存储产生日志的用户名
  
-- logpidcolumn 该列存储产生日志的进程PID
  注:以上是本人对原文的理解翻译(详细请参阅源码包的README文件),如果有误欢迎指正。
  3 实践部分
  3.1 环境配置
  3.1.1 安装配置工具
yum install -y wget vim  3.1.2 安装编译工具包(可选,编译安装才需要)
yum install -y gcc gcc-c++ make expat-devel  3.1.3 MySQL安装
  1)配置MySQL的源
vim /etc/yum.repos.d/mysql56-community.repo  输入如下内容:
# Enable to use MySQL 5.6  

  
name=MySQL 5.6 Community Server
  
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
  
enabled=1
  
gpgcheck=1
  
gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql
  2)安装数据库相关包
yum install -y mysql-community-server mysql-community-devel mysql-community-client  3.1.4 安装相关包
yum install -y vsftpd pam-devel httpd  3.1.5 下载pam模块(可选,编译安装才需要)
wget http://prdownloads.sourceforge.net/pam-mysql/pam_mysql-0.7RC1.tar.gz  3.1.6 开启防火墙端口
  In CentOS 6:
vim /etc/sysconfig/iptables  加入如下行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT  
-A INPUT -p tcp -m state --state NEW -m tcp --dport 50000:60000 -j ACCEPT
  重启防火墙服务:
/etc/init.d/iptables restart  In CentOS 7:
firewall-cmd --permanent --add-service ftp  
firewall-cmd --permanent --add-port 50000-60000/tcp
  
firewall-cmd --reload
  
firewall-cmd --list-all
  3.1.7 关闭selinux
setenforce 0  
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  3.2 安装pam模块
  3.2.1 yum方式安装
  In CentOS 6:
yum install -y http://dl.fedoraproject.org/pub/epel/6/i386/pam_mysql-0.7-0.12.rc1.el6.i686.rpm  注:下载页面,
  http://dl.fedoraproject.org/pub/epel/6/i386/
  In CentOS 7:
yum install -y ftp://ftp.pbone.net/mirror/archive.fedoraproject.org/fedora/linux/releases/20/Everything/x86_64/os/Packages/p/pam_mysql-0.7-0.16.rc1.fc20.x86_64.rpm  注:下载页面,
  http://rpm.pbone.net/index.php3/stat/4/idpl/25165183/dir/fedora_20/com/pam_mysql-0.7-0.16.rc1.fc20.x86_64.rpm.html
  3.2.2 编译安装方式(可选,编译安装才需要)
  1)解压安装包
tar -xf pam_mysql-0.7RC1.tar.gz  2)编译并安装
cd pam_mysql-0.7RC1  
./configure --with-mysql=/usr/bin/mysql_config
  
make && make install
  如果提示错误以下错误:
configure: error: Your system doesn't appear to be configured to use PAM. Perhaps you need to specify the correct location where the PAM modules reside.  可增加参数解决:
./configure --with-mysql=/usr/bin/mysql_config --with-pam-mods-dir=/usr/lib64/security  3.3 配置数据库
  3.3.1 启动服务并配置自动启动
  In CentOS 6:
/etc/init.d/mysqld start  
chkconfig mysqld on
  In CentOS 7:
systemctl start mysqld  
systemctl enable mysqld
  3.3.2 初始化数据库
mysql_secure_installation  向导如下:
[...]  
Enter current password for root (enter for none):
  
OK, successfully used password, moving on...
  
[...]
  
Set root password? y
  
New password:
  
Re-enter new password:
  
Password updated successfully!
  
Reloading privilege tables..
  
... Success!
  
[...]
  
Remove anonymous users? y
  
... Success!
  
[...]
  
Disallow root login remotely? n
  
... skipping.
  
[...]
  
Remove test database and access to it? y
  
- Dropping test database...
  
... Success!
  
- Removing privileges on test database...
  
... Success!
  
[...]
  
Reload privilege tables now? y
  
... Success!
  
[...]
  3.3.3 创建验证数据库
mysql -uroot -p  
create database vsftpd;
  3.3.4 创建验证数据表
use vsftpd;  
create table users (
  
id int auto_increment not null,
  
name char(20) not null unique key,
  
passwd char(48) not null,
  
primary key(id)
  
);
  3.3.5 添加测试数据
insert into vsftpd.users(name,passwd) values ('test1',password('123456'));  查询数据库中的账号:
select * from vsftpd.users;  3.3.6 配置验证账号
grant select on vsftpd.* to vsftpd@localhost identified by 'abc123';  
grant select on vsftpd.* to vsftpd@127.0.0.1 identified by 'abc123';
  
flush privileges;
  3.3.7 测试验证账号
mysql -uvsftpd -pabc123  3.4 配置vsftp
  3.4.1 备份配置文件
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.defalut  
vim /etc/vsftpd/vsftpd.conf
  输入如下配置:
listen=YES  
anonymous_enable=NO
  
local_enable=YES
  
virtual_use_local_privs=YES
  
write_enable=YES
  
connect_from_port_20=YES
  
pasv_min_port=50000
  
pasv_max_port=60000
  
pam_service_name=vsftpd
  
guest_enable=YES
  
guest_username=apache
  
chroot_local_user=YES
  
allow_writeable_chroot=YES
  
user_sub_token=$USER
  
local_root=/var/www/$USER
  
hide_ids=YES
  
xferlog_enable=YES
  
xferlog_file=/var/log/vsftpd.log
  重要代码解析:
第9行声明调用PAM_MySQL模块  
第11行决定上传代码的属主是apache,也可以映射到其他本地用户,请灵活运用
  
第12行限制用户chroot,限制FTP用户浏览其他非权限目录
  
第13和14行根据登录的账号动态指定用户根目录
  
第15行向FTP用户显示属主和属组为FTP
  3.4.2 配置pam
  确认pam_mysql.so的位置:
find / -name \*pam_mysql.so\*  假设显示如下:
/lib/security/pam_mysql.so  加载认证模块:
cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.defautl  
vim /etc/pam.d/vsftpd
  修改配置如下:
#%PAM-1.0  
session    optional   pam_keyinit.so    force revoke
  
#数据库认证
  
auth       sufficient   /lib/security/pam_mysql.so user=vsftpd passwd=abc123 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=passwd crypt=2
  
#vsftp默认的其余认证
  
auth       required   pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
  
auth       required   pam_shells.so
  
auth       include      password-auth
  
#授权和认证也是一样的
  
account    sufficient   /lib/security/pam_mysql.so user=vsftpd passwd=abc123 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=passwd crypt=2
  
account    include      password-auth
  
session    required   pam_loginuid.so
  
session    include      password-auth
  注:请根据pam_mysql.so实际路径写,不同的安装方法位置会有所不同。
  3.4.3 创建用户配置文件目录
mkdir -p /var/www/test1  3.4.4 配置映射用户目录权限
  查询映射用户的家目录
cat /etc/passwd | grep apache  显示如下:
apache:x:48:48:Apache:/var/www:/sbin/nologin  修改映射用户的属主
chown apache:apache /var/www/test1  3.4.5 重启服务
  In CentOS 6:
/etc/init.d/vsftpd restart  In CentOS 7:
systemctl restart vsftpd  3.5 测试并调试
  3.5.1 监视日志
tail -f /var/log/secure  
tail -f /var/log/vsftpd.log
  注:如果登陆成功secure中没有任何日志,但vsftpd.log中会有登陆成功的提示,这时候无论是否能正常使用都表示PAM_MySQL模块已经配置好,请检查VSFTP的配置。
  3.5.2 尝试登陆
ftp://10.168.0.51/  注:如果登陆不成功请根据监视日志的提示信息排错。
  4 用户管理工具的使用
  4.1 工具的下载
  http://down.51cto.com/data/2298015
  注:该工具是笔者自己写的,请按照自己的需求确定是否使用。
  4.2 安装配置
  4.2.1 部署工具
tar -xf vsftpcli.tar  
cp vsftpcli /usr/bin/
  
chmod 700 /usr/bin/vsftpcli
  4.2.2 工具的常量定义
vim /usr/bin/vsftpcli  修改以下常量:
mysql_user='root' #数据库连接的账号  
mysql_pass='abc123' #数据库连接的密码
  
mysql_host='localhost' #数据库主机
  
mysql_host='3306' #数据库的端口
  

  
basedir='/var/www' #网站的根目录
  
localuser='apache' #网站的属主
  
localgroup='apache' #网站的属组
  4.3 工具的使用
  4.3.1 获取帮助
vsftpcli  显示如下:
Usage: /usr/bin/vsftpcli     
       /usr/bin/vsftpcli
  
       /usr/bin/vsftpcli   
  
       /usr/bin/vsftpcli   
  
       /usr/bin/vsftpcli
  4.3.2 增加用户
vsftpcli add user1 123456  
vsftpcli add user2 123456
  
vsftpcli add user3 123456
  4.3.3 打印用户
vsftpcli print  4.3.4 删除用户
vsftpcli remove user2  4.3.5 重命名用户
vsftpcli rename user3 user2  4.3.6 更改用户密码
vsftpcli chpasswd user2 456789  参阅地址:
  ==================================
  PAM-MySQL:
  http://pam-mysql.sourceforge.net
  http://pam-mysql.sourceforge.net/Documentation/
  VSFTPD:
  https://security.appspot.com/vsftpd.html#docs
  https://security.appspot.com/vsftpd/vsftpd_conf.html


页: [1]
查看完整版本: vsftp通过pam集成mysql身份验证