FTP服務系列之vsftpd+pam+mysql實現虛擬用戶認證

 vsftpd+pam+mysql

 

一、安裝所需要程序

  1. 1、事先安裝好開發環境和mysql數據庫; 
  2. # yum -y install mysql-server mysql-devel 
  3. # yum -y groupinstall "Development Tools" "Development Libraries" 
  4.  
  5. 2.安裝pam_mysql-0.7RC1 
  6. [root@localhost ~]# tar xf pam_mysql-0.7RC1.tar.gz  
  7. [root@localhost ~]# cd pam_mysql-0.7RC1 
  8. [root@localhost pam_mysql-0.7RC1]# ./configure --with-mysql=/usr --with-openssl 
  9. [root@localhost pam_mysql-0.7RC1]# make 
  10. [root@localhost pam_mysql-0.7RC1]# make install 
  11.  
  12. 編譯選項解釋: 
  13. --with-mysql=/usr            #mysql的安裝路徑 
  14. --with-openssl               #表示連接mysql數據庫時,可以實現加密的方式通信(可用可不用) 
  15.  
  16. 3.安裝vsftpd 
  17. # yum -y install vsftpd 
 

二、創建虛擬用戶賬號

1.準備數據庫及相關表

首先請確保mysql服務已經正常啓動。而後,按需要建立存儲虛擬用戶的數據庫即可,這裏將其創建爲vsftpd數據庫。

mysql> create database vsftpd;

mysql> grant select on vsftpd.* to vsftpd@localhost identified by 'vsftpd';

mysql> grant select on vsftpd.* to [email protected] identified by 'vsftpd';

mysql> flush privileges;

mysql> use vsftpd;

mysql> create table users (

    -> id int AUTO_INCREMENT NOT NULL,

    -> name char(20) binary NOT NULL,

    -> password char(48) binary NOT NULL,

    -> primary key(id)

    -> );

2、添加測試的虛擬用戶

根據需要添加所需要的用戶,需要說明的是,這裏將其密碼採用明文格式存儲,原因是pam_mysql的password()函數與MySQL的password()函數可能會有所不同。

mysql> insert into users(name,password) values('tom','redhat');

mysql> insert into users(name,password) values('jerry','redhat');

詳細操作過程:

  1. [root@localhost pam_mysql-0.7RC1]# mysql 
  2. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  3. Your MySQL connection id is 4 
  4. Server version: 5.0.77 Source distribution 
  5.  
  6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
  7.  
  8. mysql> create database vsftpd; 
  9. Query OK, 1 row affected (0.12 sec) 
  10.  
  11. mysql> use vsftpd; 
  12. Database changed 
  13. mysql> create table users (id int AUTO_INCREMENT NOT NULL,name char(20) binary NOT NULL,password char(48) binary NOT NULL,primary key(id)); 
  14. Query OK, 0 rows affected (0.08 sec) 
  15.  
  16. mysql> desc users; 
  17. +----------+----------+------+-----+---------+----------------+ 
  18. | Field    | Type     | Null | Key | Default | Extra          | 
  19. +----------+----------+------+-----+---------+----------------+ 
  20. | id       | int(11)  | NO   | PRI | NULL    | auto_increment |  
  21. name     | char(20) | NO   |     | NULL    |                |  
  22. password | char(48) | NO   |     | NULL    |                |  
  23. +----------+----------+------+-----+---------+----------------+ 
  24. rows in set (0.03 sec) 
  25.  
  26. mysql> grant select on vsftpd.* to vsftpd@localhost identified by 'vsftpd'
  27. Query OK, 0 rows affected (0.04 sec) 
  28.  
  29. mysql> grant select on vsftpd.* to [email protected] identified by 'vsftpd'
  30. Query OK, 0 rows affected (0.00 sec) 
  31.  
  32. mysql> flush privileges
  33. Query OK, 0 rows affected (0.00 sec) 
  34.  
  35. mysql> insert into users(name,passwordvalues('tom','redhat'),('jerry','redhat'
  36.     -> ; 
  37. Query OK, 2 rows affected (0.01 sec) 
  38. Records: 2  Duplicates: 0  Warnings: 0 
  39.  
  40. mysql> select * from users; 
  41. +----+-------+----------+ 
  42. | id | name  | password | 
  43. +----+-------+----------+ 
  44. |  1 | tom   | redhat   |  
  45. |  2 | jerry | redhat   |  
  46. +----+-------+----------+ 
  47. rows in set (0.00 sec) 
  48.  
  49. mysql> \q 
  50. Bye 
  51. [root@localhost pam_mysql-0.7RC1]#  

三、配置vsftpd

  1. 1.建立pam認證所需文件 
  2.  
  3. #vi /etc/pam.d/vsftpd.mysql 
  4. 添加如下兩行 
  5. auth required /lib/security/pam_mysql.so user=vsftpd passwd=www.magedu.com host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0 
  6. account required /lib/security/pam_mysql.so user=vsftpd passwd=www.magedu.com host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0 

  1. 2.修改vsftpd的配置文件,使其適應mysql認證 
  2.  
  3. 建立虛擬用戶映射的系統用戶及對應的目錄 
  4. #useradd -s /sbin/nologin -d /var/ftproot vuser 
  5. #chmod go+rx /var/ftproot 
  6.  
  7. 請確保/etc/vsftpd/vsftpd.conf中已經啓用了以下選項 
  8. anonymous_enable=YES 
  9. local_enable=YES 
  10. write_enable=YES 
  11. anon_upload_enable=NO 
  12. anon_mkdir_write_enable=NO 
  13. chroot_local_user=YES 
  14.  
  15. 而後添加以下選項 
  16. guest_enable=YES                #是否允許來賓賬號訪問 
  17. guest_username=vuser            #把來賓賬號映射爲vuser 
  18.  
  19. 並確保pam_service_name選項的值如下所示 
  20. pam_service_name=vsftpd.mysql 

虛擬用戶登錄成功

四、啓動vsftpd服務

# service vsftpd start

# chkconfig vsftpd on

五、配置虛擬用戶具有不同的訪問權限

  1. vsftpd可以在配置文件目錄中爲每個用戶提供單獨的配置文件以定義其ftp服務訪問權限,每個虛擬用戶的配置文件名同虛擬用戶的用戶名。配置文件目錄可以是任意未使用目錄,只需要在vsftpd.conf指定其路徑及名稱即可。 
  2.  
  3. 1、配置vsftpd爲虛擬用戶使用配置文件目錄 
  4.  
  5. # vim /etc/vsftpd/vsftpd.conf 
  6. 添加如下選項 
  7. user_config_dir=/etc/vsftpd/vusers  
  8.  
  9. 2、創建所需要目錄,併爲虛擬用戶提供配置文件 
  10.  
  11. # mkdir /etc/vsftpd/vusers/ 
  12. # cd /etc/vsftpd/vusers/ 
  13. # touch tom jerry 
  14.  
  15. 3、配置虛擬用戶的訪問權限 
  16.  
  17. 虛擬用戶對vsftpd服務的訪問權限是通過匿名用戶的相關指令進行的。比如,如果需要讓tom用戶具有上傳文件的權限,可以修改/etc/vsftpd/vusers/tom文件,在裏面添加如下選項即可。 
  18. anon_upload_enable=YES 
  19. anon_mkdir_write_enable=YES 
  20. anon_other_write_enable=YES 


在tom問件裏配置如下內容,可以使tom用戶上傳下載和刪除文件,jerry用戶沒配置

tom用戶可以上傳和刪除等權限

  1. [root@localhost vusers]# ftp 172.16.25.11 
  2. Connected to 172.16.25.11. 
  3. 220 (vsFTPd 2.0.5) 
  4. 530 Please login with USER and PASS. 
  5. 530 Please login with USER and PASS. 
  6. KERBEROS_V4 rejected as an authentication type 
  7. Name (172.16.25.11:root): tom 
  8. 331 Please specify the password
  9. Password
  10. 230 Login successful. 
  11. Remote system type is UNIX. 
  12. Using binary mode to transfer files. 
  13. ftp> ls 
  14. 227 Entering Passive Mode (172,16,25,11,78,168) 
  15. 150 Here comes the directory listing. 
  16. 226 Directory send OK. 
  17. ftp> lcd /etc 
  18. Local directory now /etc 
  19. ftp> put inittab 
  20. local: inittab remote: inittab 
  21. 227 Entering Passive Mode (172,16,25,11,96,175) 
  22. 150 Ok to send data. 
  23. 226 File receive OK. 
  24. 1666 bytes sent in 0.00068 seconds (2.4e+03 Kbytes/s) 
  25. ftp> ls 
  26. 227 Entering Passive Mode (172,16,25,11,193,191) 
  27. 150 Here comes the directory listing. 
  28. -rw-------    1 502      502          1666 Apr 14 11:00 inittab 
  29. 226 Directory send OK. 
  30. ftp> delete inittab 
  31. 250 Delete operation successful. 
  32. ftp> ls 
  33. 227 Entering Passive Mode (172,16,25,11,113,4) 
  34. 150 Here comes the directory listing.
  35. 226 Directory send OK. 
jerry用戶沒有權限:
  1. [root@localhost vusers]# ftp 172.16.25.11 
  2. Connected to 172.16.25.11. 
  3. 220 (vsFTPd 2.0.5) 
  4. 530 Please login with USER and PASS. 
  5. 530 Please login with USER and PASS. 
  6. KERBEROS_V4 rejected as an authentication type 
  7. Name (172.16.25.11:root): jerry 
  8. 331 Please specify the password
  9. Password
  10. 230 Login successful. 
  11. Remote system type is UNIX. 
  12. Using binary mode to transfer files. 
  13. ftp> lcd /etc 
  14. Local directory now /etc 
  15. ftp> put inittab   
  16. local: inittab remote: inittab 
  17. 227 Entering Passive Mode (172,16,25,11,78,3) 
  18. 550 Permission denied. 
  19. ftp>  
這就是基於pam的虛擬用戶認證,大家都來試試吧!




 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章