您好,登錄后才能下訂單哦!
實(shí)驗(yàn)環(huán)境:
IP地址 | 描述 |
---|---|
192.168.5.181 | CentOS7系統(tǒng),base源安裝好了mariadb,作為ftp服務(wù)端,作為認(rèn)證服務(wù)端 |
192.168.5.121 | CentOS6系統(tǒng),作為ftp客戶端 |
認(rèn)證模塊pam_mysql.so的安裝
需要從網(wǎng)上下載pam_mysql.so的源碼包,pam_mysql-0.7RC1.tar.gz
在解壓安裝之前,確保在CentOS7上面的開發(fā)組包已經(jīng)安裝,如果沒有安裝,則需要運(yùn)行如下命令:
$ yum groupinstall "Development Tools" -y
之后安裝mariadb和pam的開發(fā)包:
$ yum install mariadb-devel pam-devel -y
解壓pam_mysql的源碼包,進(jìn)入源碼目錄,進(jìn)行編譯安裝。其中–with-mysql引用了mariadb的頭文件以及l(fā)ib,–with-pam引用了pam的頭文件以及l(fā)ib。–with-pam-mods-dir指明將模塊安裝的位置。
$ ./configure --with-mysql=/usr --with-pam=/usr --with-pam-mods-dir=/usr/lib64/security $ make $ make install
安裝完畢之后,在/usr/lib64/security目錄下面,可以查看到新的pam_mysql.so模塊。
$ ls /usr/lib64/security/ | grep mysql.so pam_mysql.so
mariadb創(chuàng)建數(shù)據(jù)
下面規(guī)劃一下mariadb里面的用戶。建立一個(gè)名為vsftpd的數(shù)據(jù)庫,在這個(gè)數(shù)據(jù)庫里面建立一個(gè)名為auth的數(shù)據(jù)表,在數(shù)據(jù)表里面建立兩個(gè)用戶作為vsftpd的虛擬用戶:user1,密碼為user1;user2,密碼為user2。密碼采用mysql自帶的PASSWORD()函數(shù)進(jìn)行加密。使用名為vsftpd@’127.0.0.1’的用戶進(jìn)行登錄查詢,只授予該用戶select權(quán)限,登錄密碼為vsftpd。建立之后的結(jié)果如下:
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> use vsftpd; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [vsftpd]> show tables; +------------------+ | Tables_in_vsftpd | +------------------+ | auth | +------------------+ 1 row in set (0.00 sec) MariaDB [vsftpd]> desc auth; +----------+-----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-----------+------+-----+---------+-------+ | name | char(20) | YES | | NULL | | | password | char(100) | YES | | NULL | | +----------+-----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) MariaDB [vsftpd]> select * from auth; +-------+-------------------------------------------+ | name | password | +-------+-------------------------------------------+ | user1 | *34D3B87A652E7F0D1D371C3DBF28E291705468C4 | | user2 | *12A20BE57AF67CBF230D55FD33FBAF5230CFDBC4 | +-------+-------------------------------------------+ 2 rows in set (0.00 sec) MariaDB [vsftpd]> select host,user,password from mysql.user where user='vsftpd'; +-----------+--------+-------------------------------------------+ | host | user | password | +-----------+--------+-------------------------------------------+ | 127.0.0.1 | vsftpd | *653E55BC34328FD9504096B9DFB2434DE24AAE86 | +-----------+--------+-------------------------------------------+ 1 row in set (0.00 sec)
建立來賓賬戶
所有mysql里面存儲(chǔ)的虛擬用戶在登錄之后都會(huì)被映射為本地的來賓用戶,這里建立一個(gè)名為vuser的來賓賬戶,家目錄為/ftproot/vuser,修改其權(quán)限為544,即去除所有的’寫’權(quán)限。在里面新建一個(gè)pub目錄,用setfacl
給pub目錄賦予vuser用戶的讀寫執(zhí)行權(quán)限。
$ mkdir ftproot $ cd ftproot $ useradd -d /ftproot/vuser vuser $ chmod 544 /ftproot/vuser $ mkdir /ftproot/vuser/pub $ setfacl -m u:vuser:rwx /ftproot/vuser/pub
配置pam文件
新建一個(gè)/etc/pam.d/ftp-mysql的文件,在里面添加兩行如下內(nèi)容,詳細(xì)的配置項(xiàng),請參見pam_mysql.so源碼包里面的README文檔:
auth required /usr/lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=127.0.0.1 db=vsftpd table=auth usercolumn=name passwdcolumn=password crypt=2 account required /usr/lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=127.0.0.1 db=vsftpd table=auth usercolumn=name passwdcolumn=password crypt=2
配置vsftpd.conf文件
新建一個(gè)vsftpd.conf文件,配置如下所示。注意pam_service_name由默認(rèn)的vsftpd替換為剛才建立的ftp-mysql,啟用來賓賬戶guest_enable=YES,使用來賓賬戶vuser,并且配置虛擬用戶user1和user2的權(quán)限文件到/etc/vsftpd/vusers_config目錄下面:
anonymous_enable=YES local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES xferlog_std_format=YES listen=NO listen_ipv6=YES pam_service_name=ftp-mysql userlist_enable=YES tcp_wrappers=YES guest_enable=YES guest_username=vuser user_config_dir=/etc/vsftpd/vusers_config/
/etc/vsftpd/vusers_config目錄下面的user1和user2的權(quán)限配置如下所示,給予user1上傳的權(quán)限,但是給予user2上傳、刪除目錄、刪除文件的權(quán)限。配置完畢后,用systemctl start mariadb.service vsftpd.service
命令重啟mariadb和vsftpd服務(wù):
$ cat /etc/vsftpd/vusers_config/user1 anon_upload_enable=YES anon_other_write_enable=NO $ cat /etc/vsftpd/vusers_config/user2 anon_upload_enable=YES anon_mkdir_write_enable=YES anon_other_write_enable=YES
客戶端測試
在客戶端上面,確保安裝了ftp客戶端工具:
yum install ftp
利用上述工具和服務(wù)端進(jìn)行通信,對user1進(jìn)行測試,可以看到,登錄成功,并且user1有上傳的權(quán)限,但是并沒有刪除的權(quán)限:
$ ftp 192.168.5.181 Connected to 192.168.5.181 (192.168.5.181). 220 (vsFTPd 3.0.2) Name (192.168.5.181:root): user1 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> ls 227 Entering Passive Mode (192,168,5,181,187,35). 150 Here comes the directory listing. drwxrwxr-x 2 0 0 6 Jun 05 18:33 pub 226 Directory send OK. ftp> cd pub 250 Directory successfully changed. ftp> ls 227 Entering Passive Mode (192,168,5,181,180,167). 150 Here comes the directory listing. 226 Directory send OK. ftp> lcd /etc Local directory now /etc ftp> put hosts local: hosts remote: hosts 227 Entering Passive Mode (192,168,5,181,142,11). 150 Ok to send data. 226 Transfer complete. 256 bytes sent in 0.000155 secs (1651.61 Kbytes/sec) ftp> ls 227 Entering Passive Mode (192,168,5,181,108,36). 150 Here comes the directory listing. -rw------- 1 1001 1001 256 Jun 06 05:06 hosts 226 Directory send OK. ftp> delete hosts 550 Permission denied. ftp> exit 221 Goodbye.
下面對user2進(jìn)行測試,可以看到,user2登錄成功,并且有上傳權(quán)限,刪除權(quán)限,創(chuàng)建目錄的權(quán)限。:
$ ftp 192.168.5.181 Connected to 192.168.5.181 (192.168.5.181). 220 (vsFTPd 3.0.2) Name (192.168.5.181:root): user2 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> cd pub 250 Directory successfully changed. ftp> ls 227 Entering Passive Mode (192,168,5,181,96,57). 150 Here comes the directory listing. 226 Directory send OK. ftp> lcd /etc Local directory now /etc ftp> put hosts local: hosts remote: hosts 227 Entering Passive Mode (192,168,5,181,36,41). 150 Ok to send data. 226 Transfer complete. 256 bytes sent in 0.000145 secs (1765.52 Kbytes/sec) ftp> ls 227 Entering Passive Mode (192,168,5,181,141,235). 150 Here comes the directory listing. -rw------- 1 1001 1001 256 Jun 06 05:10 hosts 226 Directory send OK. ftp> delete hosts 250 Delete operation successful. ftp> ls 227 Entering Passive Mode (192,168,5,181,56,230). 150 Here comes the directory listing. 226 Directory send OK. ftp> mkdir dir 257 "/pub/dir" created ftp> ls 227 Entering Passive Mode (192,168,5,181,208,106). 150 Here comes the directory listing. drwx------ 2 1001 1001 6 Jun 06 05:10 dir 226 Directory send OK.
下面對于系統(tǒng)用戶ftpuser以及一個(gè)不存在的用戶abc進(jìn)行登錄測試,發(fā)現(xiàn)無法登錄,證明只用mysql數(shù)據(jù)庫里面存在的用戶才能夠進(jìn)行認(rèn)證:
$ ftp 192.168.5.181 Connected to 192.168.5.181 (192.168.5.181). 220 (vsFTPd 3.0.2) Name (192.168.5.181:root): ftpuser 331 Please specify the password. Password: 530 Login incorrect. Login failed. $ ftp 192.168.5.181 Connected to 192.168.5.181 (192.168.5.181). 220 (vsFTPd 3.0.2) Name (192.168.5.181:root): abc 331 Please specify the password. Password: 530 Login incorrect. Login failed. ftp>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。