FreeBSD
本文根據網上流行的一篇文章整理改編而成,並修正了原文的一些小錯誤。
由於為每一個需要使用ftp服務的用戶建立系統帳戶會影響系統的安全,所以我們使用數據庫用戶進行ftp用戶驗證。
系統組成:
Proftpd1.2.7
proftpd-mod-quotatab-1.2.4
mysql3.23
因為ports裡沒有proftpd-mod-quotatab-1.2.4模塊,我們下載源代碼安裝。
下載源代碼包:(必須下載相同版本的源代碼包)
到 http://www.proftpd.org 或 ftp://ftp.servus.at/ProFTPD/distrib/source/proftpd-1.2.7.tar.gz
去下載proftpd-1.2.7.tar.gz。
到 http://www.castaglia.org/proftpd/ 去下載proftpd-mod-quotatab-1.2.4.tar.gz。(1.2.5 版本的mod_quotatab是用在1.2.8rc1上的)
用ftp將它們上傳到/home/ylf/app目錄。
然後解壓縮源代碼包:
# cd /home/ylf/app
# tar zxvf proftpd-1.2.7.tar.gz
# tar zxvf proftpd-mod-quotatab-1.2.4.tar.gz
進入mod-quotatab目錄
# cd mod_quotatab
把mod_quotatab中的文件拷貝到proftpd 中的modules 目錄中
# cp * ../proftpd-1.2.7/modules
在開始運行configure之前,我們要先改動一個文件
進入 proftpd-1.2.7/contrib 目錄
# cd /home/ylf/app/proftpd-1.2.7/contrib
修改 mod_sql_mysql.c
# vi mod_sql_mysql.c
找到#include 把他改為你的實際路徑,這裡是:
#include
然後編譯安裝:
# cd /home/ylf/app/proftpd-1.2.7
#./configure --prefix=/usr/local/proftpd --with-modules=mod_sql:mod_sql_mysql:mod_quotatab:mod_quotatab_sql --with-includes=/usr/local/include/mysql --with-libraries=/usr/local/lib/mysql
# make
# make install
進入到proftpd配置文件所在目錄
# cd /usr/local/proftpd/etc
備份原配置文件
# mv proftpd.conf proftpd.conf.bak
然後編輯新的配置文件proftpd.conf
# vi proftpd.conf
我的proftpd.conf內容如下:
# This is a basic ProFTPD configuration file (rename it to
# 'proftpd.conf' for actual use. It establishes a single server
# and a single anonymous login. It assumes that you have a user/group
# "nobody" and "ftp" for normal operation and anon.
ServerName "ftpx.3322.org"
ServerType standalone
DefaultServer on
# 用戶登陸時不顯示ftp服務器版本信息
ServerIdent off
# Port 21 is the standard FTP port.
Port 21
# Umask 022 is a good standard umask to prevent new dirs and files
# from being group and world writable.
Umask 022
MaxLoginAttempts 3
TimeoutLogin 120
TimeoutIdle 600
TimeoutNoTransfer 900
TimeoutStalled 3600
MaxClients 100
# 設置每台主機最多並發連接數
MaxClientsPerHost 3
AllowOverwrite no
AllowStoreRestart on
UseReverseDNS off
# 設置如果shell為空時允許用戶登錄
RequireValidShell off
# 將用戶限制在自己的主目錄下
DefaultRoot ~ ftpusers
DefaultRoot ~ FTPGRP
# To prevent DoS attacks, set the maximum number of child processes
# to 30. If you need to allow more than 30 concurrent connections
# at once, simply increase this value. Note that this ONLY works
# in standalone mode, in inetd mode you should use an inetd server
# that allows you to limit maximum number of processes per service
# (such as xinetd).
MaxInstances 30
# Set the user and group under which the server will run.
User FTPUSR
Group FTPGRP
# Normally, we want files to be overwriteable.
AllowOverwrite on
# A basic anonymous configuration, no upload directories.
# 匿名登錄設置。匿名用戶目錄為/ftp
User ftp
Group ftpusers
# We want clients to be able to login with "anonymous" as well as "ftp"
UserAlias anonymous ftp
# Limit the maximum number of anonymous logins
MaxClients 10
# We want 'welcome.msg' displayed at login, and '.message' displayed
# in each newly chdired directory.
DisplayLogin welcome.msg
DisplayFirstChdir .message
# Limit WRITE everywhere in the anonymous chroot
#
# DenyAll
#
# 數據庫聯接的信息,FTP是數據庫名,localhost是主機名,root是連接數據庫的用戶名,#123456是密碼
#(如果沒有密碼留空)
SQLConnectInfo FTP@localhost root 123456
# 數據庫認證的類型
SQLAuthTypes Backend Plaintext
# 指定用來做用戶認證的表的有關信息。("FTPUSERS"和"FTPGRPS"是數據表名字,等一會
# 在下面建立)
SQLUserInfo FTPUSERS userid passwd uid gid homedir shell
SQLGroupInfo FTPGRPS groupname gid members
# 數據庫的鑒別
SQLAuthenticate users groups usersetfast groupsetfast
# 如果home目錄不存在,則系統會根據它的home項新建一個目錄
SQLHomedirOnDemand on
# 啟用磁盤限額
QuotaDirectoryTally on
# 磁盤限額單位 b"|"Kb"|"Mb"|"Gb"
QuotaDisplayUnits "Kb"
QuotaEngine on
# 磁盤限額日志記錄
QuotaLog "/var/log"
# 打開磁盤限額信息,當登陸FTP帳戶後,使用命令 "quote SITE QUOTA" 後可顯示當前用#戶的磁盤限額
QuotaShowQuotas on
# SQL調用語句,不用修改
SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail,files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = '%{0}' AND quota_type = '%{1}'"
SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = '%{0}' AND quota_type = '%{1}'"
SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = '%{6}' AND quota_type = '%{7}'" quotatallies
SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies
QuotaLimitTable sql:/get-quota-limit
QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally
下面為ftp用戶建立相應的數據庫和表
進入mysql數據庫命令狀態:
# mysql –p
提示輸入密碼
建立數據庫FTP(注意大小寫和每句話後面的“;”)
CREATE DATABASE FTP;
然後在這個數據庫中建立一個用戶表FTPUSERS,這個表是必須的:
use FTP;
create table FTPUSERS (
userid TEXT NOT NULL,
passwd TEXT NOT NULL,
uid INT NOT NULL,
gid INT NOT NULL,
homedir TEXT,
shell TEXT
);
此表格是為了用戶認證所需要的,其中userid、passwd是必不可少的,userid是用做FTP服務的用戶名;passwd是指此用戶的密碼; uid是系統用戶的ID,也就是所映射的系統用戶;gid是所屬系統組的ID;homedir是該用戶所在的HOME目錄;shell可以為該用戶指定相 應的shell。當然你可以建立更多的字段,例如:用來記錄用戶登錄次數的count,或者是日期的date,如果你對配置熟悉了之後,你可以根據自己的 喜歡添加更多的功能。在此就不多講。
為了提供更多的功能,我們添加另外一個需要的表:FTPGRPS,也就是確定組的表格,這裡是它的格式:
create table FTPGRPS (
groupname TEXT NOT NULL,
gid SMALLINT NOT NULL,
members TEXT NOT NULL
);
其中groupname是組的名稱,gid是系統組的ID,members是組的成員。注意:多成員,他們之間要用逗號隔開,不能使用空格。
為FTP