Centos7中配置vsftpd對(duì)接LDAP(域控)
背景與需求
公司內(nèi)部使用ftp進(jìn)行文件共享,之前使用的是公共賬號(hào),導(dǎo)致賬號(hào)密碼到處傳播,權(quán)限難以管控。因此,計(jì)劃將FTP服務(wù)對(duì)接LDAP(公司內(nèi)部的域控), 實(shí)現(xiàn)用個(gè)人域賬號(hào)登錄。
具體需求
- 域賬號(hào)登錄
- 未經(jīng)授權(quán)的,可以登錄FTP,但無(wú)權(quán)限訪問(wèn)(共享的)內(nèi)容
實(shí)施
環(huán)境描述
操作系統(tǒng)
# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.9.2009 (Core)
Release: 7.9.2009
Codename: Core
具體實(shí)施步驟
步驟概述
(1) 安裝基礎(chǔ)包
(2) 配置vsftp配置
(3) 調(diào)試連接ldap配置
步驟1 安裝基礎(chǔ)包
# yum install nscd nss-pam-ldapd -y
# yum -y install vsftpd ftp openldap-clients
步驟2 配置vsftp配置
先貼出我的配置
# cat vsftpd/vsftpd.conf
use_localtime=YES
listen=YES
connect_from_port_20=YES
ftpd_banner=Welcome to virtual FTP service.
anonymous_enable=NO
local_enable=YES
write_enable=NO
anon_upload_enable=NO
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
chroot_local_user=YES
local_umask=022
guest_enable=YES
guest_username=compiler
tcp_wrappers=YES
use_localtime=yes
max_per_ip=20
pasv_enable=YES
pasv_min_port=3001
pasv_max_port=5100
idle_session_timeout=600
allow_writeable_chroot=YES
pam_service_name=vsftpd # 這個(gè)指重點(diǎn)配置,對(duì)應(yīng)/etc/pam.d/vsftpd 文件
xferlog_enable=YES
xferlog_file=/var/log/vsftpd/xferlog.log
dual_log_enable=yes
#虛擬用戶和本地用戶有相同的權(quán)限
virtual_use_local_privs=YES
user_sub_token=$USER
local_root=/home/anonymous # 沒(méi)有授權(quán)的用戶,統(tǒng)一的root目錄(無(wú)內(nèi)容)
user_config_dir=/etc/vsftpd/vusers # 這里放已授權(quán)用戶的配置,控制用戶的root目錄
anon_world_readable_only=YES
anon_upload_enable=NO
anon_mkdir_write_enable=YES
anon_other_write_enable=NO
# cat /etc/pam.d/vsftpd # 配置使用ldap驗(yàn)證
#%PAM-1.0
auth required /usr/lib64/security/pam_ldap.so
account required /usr/lib64/security/pam_ldap.so
我先在/etc/vsftpd/vusers 創(chuàng)建一個(gè)測(cè)試賬號(hào)文件,假定共享的目錄為/opt, 也就是所有授權(quán)的用戶的root目錄是/opt
# mkdir -p /etc/vsftpd/vusers
# cd /etc/vsftpd/vusers
# vim pcan1250
local_root=/opt
下面就是配置去連接LDAP了。
步驟3 配置,調(diào)試連接LDAP
直接啟動(dòng)vsftpd的話,現(xiàn)在訪問(wèn)ftp, 在操作系統(tǒng)日志里可以看到以下報(bào)錯(cuò)
# tail -f /var/log/secure
Sep 18 15:30:21 szvxl15487 vsftpd[311630]: pam_ldap(vsftpd:auth): Authentication failure; user=pcan1254
Sep 18 15:48:51 szvxl15487 vsftpd[316172]: pam_ldap(vsftpd:auth): error opening connection to nslcd: No such file or directory
那應(yīng)該就是要用到nslcd 服務(wù)了。先配置一下這個(gè)服務(wù), 配置文件為/etc/nslcd.conf, 以下是我的配置
# cat /etc/nslcd.conf
uid nslcd
gid ldap
uri ldap://xxx # 域控地址,根據(jù)自己實(shí)際情況填
#ldap_version 3
base DC=mydc,DC=xyz # 根據(jù)自己的實(shí)際情況填寫(xiě)
binddn CN=xxxx,OU=vApps,DC=mydc,DC=xyz # 可以理解為管理員賬號(hào)(只讀賬號(hào)就可以), 根據(jù)自己實(shí)際情況填寫(xiě)
bindpw xxxx # 上面賬號(hào)的密碼
ssl no
filter passwd (objectClass=user) # 這里的話,根據(jù)自己的實(shí)際情況填寫(xiě),其實(shí)我也不大懂,我是經(jīng)過(guò)調(diào)試填的這個(gè)
map passwd uid sAMAccountName # 這里也是,配合上面fiter進(jìn)行填寫(xiě)的,下面我會(huì)說(shuō)一下 我是怎么調(diào)試的
(1) 沒(méi)配置filter和map, 用正確的賬號(hào)密碼登錄不了FTP
一開(kāi)始我沒(méi)有配置filter和map, 登錄ftp,一直報(bào)賬號(hào)密碼錯(cuò)誤,如下
# ftp localhost
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220 Welcome to virtual FTP service.
Name (localhost:root): pcan1250
331 Please specify the password.
Password:
530 Login incorrect.
Login failed.
ftp>
我知道自己的賬號(hào)密碼沒(méi)錯(cuò),問(wèn)題肯定是映射問(wèn)題,應(yīng)該是vsftp是使用的賬號(hào)與域控的映射不一致(也就是與我的預(yù)期不一致)引起的。 但是我看了操作系統(tǒng)日志,沒(méi)有看到有相關(guān)的報(bào)錯(cuò)。問(wèn)題一下子卡住了。
(2) 調(diào)試nslcd服務(wù)
既然從/var/log/secure 知道大概是用的nslcd服務(wù)去訪問(wèn)ldap的,那就看看能不能從這里入手。通過(guò)查看nslcd幫助,發(fā)現(xiàn)有個(gè)debug選項(xiàng),可以試一下
# nslcd --help
Usage: nslcd [OPTION]...
Name Service LDAP connection daemon.
-c, --check check if the daemon already is running
-d, --debug don't fork and print debugging to stderr
--help display this help and exit
--version output version information and exit
Report bugs to <nss-pam-ldapd-users@lists.arthurdejong.org>
先停掉,然后用 -d 啟動(dòng)到調(diào)試模式
# systemctl stop nslcd.service
# nslcd -d
nslcd: DEBUG: add_uri(ldap://XXX)
nslcd: version 0.8.13 starting
nslcd: DEBUG: unlink() of /var/run/nslcd/socket failed (ignored): No such file or directory
nslcd: DEBUG: initgroups("nslcd",55) done
nslcd: DEBUG: setgid(55) done
nslcd: DEBUG: setuid(65) done
nslcd: accepting connections
很好, 有日志輸出了, 再用ftp localhost訪問(wèn)一次看看, 可以看到有日志輸出了
# nslcd -d
nslcd: DEBUG: add_uri(ldap://XXXX)
nslcd: version 0.8.13 starting
nslcd: DEBUG: unlink() of /var/run/nslcd/socket failed (ignored): No such file or directory
nslcd: DEBUG: initgroups("nslcd",55) done
nslcd: DEBUG: setgid(55) done
nslcd: DEBUG: setuid(65) done
nslcd: accepting connections
nslcd: [8b4567] DEBUG: connection from pid=321676 uid=0 gid=0
nslcd: [8b4567] <authc="pcan1250"> DEBUG: nslcd_pam_authc("pcan1250","vsftpd","***")
nslcd: DEBUG: accept() failed (ignored): Resource temporarily unavailable
nslcd: [8b4567] <authc="pcan1250"> DEBUG: myldap_search(base="DC=mydc,DC=xyz", filter="(&(objectClass=posixAccount)(uid=pcan1250))")
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_initialize(ldap://XXXX)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_rebind_proc()
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_option(LDAP_OPT_PROTOCOL_VERSION,3)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_option(LDAP_OPT_DEREF,0)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_option(LDAP_OPT_TIMELIMIT,0)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_option(LDAP_OPT_TIMEOUT,0)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT,0)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_option(LDAP_OPT_REFERRALS,LDAP_OPT_ON)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_set_option(LDAP_OPT_RESTART,LDAP_OPT_ON)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_simple_bind_s("CN=XXXX,OU=vApps,DC=mydc,DC=xyz","***") (uri="ldap://XXXX")
nslcd: [8b4567] <authc="pcan1250"> DEBUG: rebinding to ldap://DomainDnsZones.mydc.xyz/DC=DomainDnsZones,DC=mydc,DC=xyz
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_simple_bind_s("CN=XXXX,OU=vApps,DC=mydc,DC=xyz","***") (uri="ldap://DomainDnsZones.mydc.xyz/DC=DomainDnsZones,DC=mydc,DC=xyz")
nslcd: [8b4567] <authc="pcan1250"> DEBUG: rebinding to ldap://ForestDnsZones.mydc.xyz/DC=ForestDnsZones,DC=mydc,DC=xyz
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_simple_bind_s("CN=XXXX,OU=vApps,DC=mydc,DC=xyz","***") (uri="ldap://ForestDnsZones.mydc.xyz/DC=ForestDnsZones,DC=mydc,DC=xyz")
nslcd: [8b4567] <authc="pcan1250"> DEBUG: rebinding to ldap://mydc.xyz/CN=Configuration,DC=mydc,DC=xyz
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_simple_bind_s("CN=XXXX,OU=vApps,DC=mydc,DC=xyz","***") (uri="ldap://mydc.xyz/CN=Configuration,DC=mydc,DC=xyz")
nslcd: [8b4567] <authc="pcan1250"> DEBUG: ldap_result(): end of results (0 total)
nslcd: [8b4567] <authc="pcan1250"> DEBUG: "pcan1250": user not found: No such object
核心是這行 myldap_search(base="DC=mydc,DC=xyz", filter="(&(objectClass=posixAccount)(uid=pcan1250))"), 我之前用python去連接ldap的時(shí)候,有一定的經(jīng)驗(yàn),所以我知道是這個(gè)fiter的問(wèn)題,我這邊不是用的uid, 而是用sAMAccountName, 并且objectClass 也不對(duì),因此我按照我的實(shí)際情況改成了如下
filter passwd (objectClass=user)
map passwd uid sAMAccountName
整個(gè)/etc/nslcd.conf 文件內(nèi)容如上(步驟3開(kāi)頭已經(jīng)列出來(lái))所示, 再次訪問(wèn)ftp
# ftp localhost
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220 Welcome to virtual FTP service.
Name (localhost:root): pcan1250
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 (127,0,0,1,13,225).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 0 Sep 18 09:50 a
# ls -alrt /opt
total 4
dr-xr-xr-x. 21 root root 4096 Sep 18 09:50 ..
-rw-r--r-- 1 root root 0 Sep 18 09:50 a
drwxr-xr-x 2 root root 15 Sep 18 09:50 .
可以看到,已經(jīng)正常登錄,用未授權(quán)的賬號(hào)登錄測(cè)試一下
# ftp localhost
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220 Welcome to virtual FTP service.
Name (localhost:root): 11111111
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 (127,0,0,1,14,172).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 0 Sep 18 15:29 當(dāng)前沒(méi)有權(quán)限訪問(wèn)該FTP.txt
226 Directory send OK.
ftp>
ftp> quit
221 Goodbye.
# ls /home/anonymous/
當(dāng)前沒(méi)有權(quán)限訪問(wèn)該FTP.txt
正是訪問(wèn)到了/home/anonymous/ (上面vsftpd.conf配置的)下。 至此,VSFTP + LDAP已完成。
后續(xù)與總結(jié)
后續(xù)有新的用戶要授權(quán)的話,由于都是同樣的local_root, 因此,只需要用pcan1250文件做軟鏈接就行,比如說(shuō),現(xiàn)在要加一個(gè)111111賬號(hào)訪問(wèn),只需要如下命令
# cd /etc/vsftpd/vusers
# ln -s pcan1250 111111
后續(xù)可以寫(xiě)一個(gè)腳本,用來(lái)批量添加或者刪除用戶。
這里的核心是調(diào)試nslcd連接到LDAP Server, 如果沒(méi)有debug信息,可以需要耗費(fèi)很多時(shí)間去慢慢調(diào)整....

浙公網(wǎng)安備 33010602011771號(hào)