Proftpd mit MySQL-Backend und Quota

Ich habe darauf Wert gelegt, dass die Passwörter verschlüsselt in der Datenbank liegen!

Wer Klartextpasswörter haben möchte soll bitte auf → howtoforge gehen.

Written by — Maximilian Thoma 2007/12/16 19:19

Anforderungen

  • Apache 2
  • PHP (mind. 4.x)
  • PHPmyAdmin
  • MySQL Server (mind. 4.x)
  • OpenSSL

Installation von proftpd und Konfiguration

Installation der Pakete

sudo apt-get install proftpd-mysql

Während der Installation taucht folgende Frage auf:

Run proftpd from inetd or standalone?

Bitte wählen Sie „standalone“ aus. (In Deutsch heisst die Auswahl „Daemon“)

User und Gruppe für proftpd Dateien anlegen

groupadd -g 2001 ftpgroup
useradd -u 2001 -s /bin/false -d /bin/null -c "proftpd user" -g ftpgroup ftpuser

MySQL User anlegen für proftpd

mysql -u root -p
CREATE DATABASE ftp;
GRANT SELECT, INSERT, UPDATE, DELETE ON ftp.* TO 'ftp'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, UPDATE, DELETE ON ftp.* TO 'ftp'@'localhost.localdomain' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Die MySQL Shell noch nicht verlassen !!!

MySQL Tabellen für proftpd anlegen

USE ftp;
 
CREATE TABLE ftpgroup (
groupname VARCHAR(16) NOT NULL DEFAULT '',
gid SMALLINT(6) NOT NULL DEFAULT '5500',
members VARCHAR(16) NOT NULL DEFAULT '',
KEY groupname (groupname)
) TYPE=MyISAM COMMENT='ProFTP group table';
 
CREATE TABLE ftpquotalimits (
name VARCHAR(30) DEFAULT NULL,
quota_type ENUM('user','group','class','all') NOT NULL DEFAULT 'user',
per_session ENUM('false','true') NOT NULL DEFAULT 'false',
limit_type ENUM('soft','hard') NOT NULL DEFAULT 'soft',
bytes_in_avail INT(10) UNSIGNED NOT NULL DEFAULT '0',
bytes_out_avail INT(10) UNSIGNED NOT NULL DEFAULT '0',
bytes_xfer_avail INT(10) UNSIGNED NOT NULL DEFAULT '0',
files_in_avail INT(10) UNSIGNED NOT NULL DEFAULT '0',
files_out_avail INT(10) UNSIGNED NOT NULL DEFAULT '0',
files_xfer_avail INT(10) UNSIGNED NOT NULL DEFAULT '0'
) TYPE=MyISAM;
 
CREATE TABLE ftpquotatallies (
name VARCHAR(30) NOT NULL DEFAULT '',
quota_type ENUM('user','group','class','all') NOT NULL DEFAULT 'user',
bytes_in_used INT(10) UNSIGNED NOT NULL DEFAULT '0',
bytes_out_used INT(10) UNSIGNED NOT NULL DEFAULT '0',
bytes_xfer_used INT(10) UNSIGNED NOT NULL DEFAULT '0',
files_in_used INT(10) UNSIGNED NOT NULL DEFAULT '0',
files_out_used INT(10) UNSIGNED NOT NULL DEFAULT '0',
files_xfer_used INT(10) UNSIGNED NOT NULL DEFAULT '0'
) TYPE=MyISAM;
 
CREATE TABLE ftpuser (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
userid VARCHAR(32) NOT NULL DEFAULT '',
passwd VARCHAR(32) NOT NULL DEFAULT '',
uid SMALLINT(6) NOT NULL DEFAULT '5500',
gid SMALLINT(6) NOT NULL DEFAULT '5500',
homedir VARCHAR(255) NOT NULL DEFAULT '',
shell VARCHAR(16) NOT NULL DEFAULT '/sbin/nologin',
count INT(11) NOT NULL DEFAULT '0',
accessed DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
modified DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id),
UNIQUE KEY userid (userid)
) TYPE=MyISAM COMMENT='ProFTP user table';

Jetzt können wir die MySQL Shell verlassen.

quit;

/etc/proftpd/modules.conf bearbeiten

Die Zeile LoadModule mod_sql_postgres.c muss auskommentiert sein !!!

[...]
LoadModule mod_sql_postgres.c
[...]

Auskommentiert:

[...]
# LoadModule mod_sql_postgres.c
[...]

/etc/proftpd/proftpd.conf anpassen

#
# /etc/proftpd/proftpd.conf -- This is a basic ProFTPD configuration file.
# To really apply changes reload proftpd after modifications.
#

# Includes DSO modules
Include /etc/proftpd/modules.conf

# Set off to disable IPv6 support which is annoying on IPv4 only boxes.

#################################################
### IPv6 abschalten NEU NEU NEU
#################################################
UseIPv6                         off
#################################################

ServerName                      "Debian"
ServerType                      standalone
DeferWelcome                    off

MultilineRFC2228                on
DefaultServer                   on
ShowSymlinks                    on

TimeoutNoTransfer               600
TimeoutStalled                  600
TimeoutIdle                     1200

DisplayLogin                    welcome.msg
DisplayFirstChdir               .message
ListOptions                     "-l"

DenyFilter                      \*.*/

# Port 21 is the standard FTP port.
Port                            21

# In some cases you have to specify passive ports range to by-pass
# firewall limitations. Ephemeral ports can be used for that, but
# feel free to use a more narrow range.
# PassivePorts                    49152 65534

# 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 that the server normally runs at.
User                            proftpd
Group                           nogroup

# Umask 022 is a good standard umask to prevent new files and dirs
# (second parm) from being group and world writable.
Umask                           022  022
# Normally, we want files to be overwriteable.
AllowOverwrite                  on

# Uncomment this if you are using NIS or LDAP to retrieve passwords:
# PersistentPasswd              off

# Be warned: use of this directive impacts CPU average load!
#
# Uncomment this if you like to see progress and transfer rate with ftpwho
# in downloads. That is not needed for uploads rates.
# UseSendFile                   off

TransferLog /var/log/proftpd/xferlog
SystemLog   /var/log/proftpd/proftpd.log

<IfModule mod_tls.c>
TLSEngine off
</IfModule>

<IfModule mod_quota.c>
QuotaEngine on
</IfModule>

<IfModule mod_ratio.c>
Ratios on
</IfModule>


# Delay engine reduces impact of the so-called Timing Attack described in
# http://security.lss.hr/index.php?page=details&ID=LSS-2004-10-02
# It is on by default.
<IfModule mod_delay.c>
DelayEngine on
</IfModule>

<IfModule mod_ctrls.c>
ControlsEngine        on
ControlsMaxClients    2
ControlsLog           /var/log/proftpd/controls.log
ControlsInterval      5
ControlsSocket        /var/run/proftpd/proftpd.sock
</IfModule>

<IfModule mod_ctrls_admin.c>
AdminControlsEngine on
</IfModule>

# A basic anonymous configuration, no upload directories.

# <Anonymous ~ftp>
#   User                                ftp
#   Group                               nogroup
#   # We want clients to be able to login with "anonymous" as well as "ftp"
#   UserAlias                   anonymous ftp
#   # Cosmetic changes, all files belongs to ftp user
#   DirFakeUser on ftp
#   DirFakeGroup on ftp
#
#   RequireValidShell           off
#
#   # 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
#   <Directory *>
#     <Limit WRITE>
#       DenyAll
#     </Limit>
#   </Directory>
#
#   # Uncomment this if you're brave.
#   # <Directory incoming>
#   #   # Umask 022 is a good standard umask to prevent new files and dirs
#   #   # (second parm) from being group and world writable.
#   #   Umask                           022  022
#   #            <Limit READ WRITE>
#   #            DenyAll
#   #            </Limit>
#   #            <Limit STOR>
#   #            AllowAll
#   #            </Limit>
#   # </Directory>
#
# </Anonymous>

##################################################################
#### NEU NEU NEU NEU NEU
##################################################################

## Nur virt. User dürfen sich anmelden !!!!

<Limit LOGIN>
AllowUser ftpuser
AllowGroup ftpgroup
DenyAll
</Limit>

DefaultRoot ~


# The passwords in MySQL are encrypted using CRYPT
SQLAuthTypes            OpenSSL
SQLAuthenticate         users groups


# used to connect to the database
# databasename@host database_user user_password
SQLConnectInfo  ftp@localhost ftp password


# Here we tell ProFTPd the names of the database columns in the "usertable"
# we want it to interact with. Match the names with those in the db
SQLUserInfo     ftpuser userid passwd uid gid homedir shell

# Here we tell ProFTPd the names of the database columns in the "grouptable"
# we want it to interact with. Again the names match with those in the db
SQLGroupInfo    ftpgroup groupname gid members

# set min UID and GID - otherwise these are 999 each
SQLMinID        2000

# create a user's home directory on demand if it doesn't exist
SQLHomedirOnDemand on

# Update count every time user logs in
SQLLog PASS updatecount
SQLNamedQuery updatecount UPDATE "count=count+1, accessed=now() WHERE userid='%u'" ftpuser

# Update modified everytime user uploads or deletes a file
SQLLog  STOR,DELE modified
SQLNamedQuery modified UPDATE "modified=now() WHERE userid='%u'" ftpuser

# User quotas
# ===========
QuotaEngine on
QuotaDirectoryTally on
QuotaDisplayUnits Mb
QuotaShowQuotas on

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 ftpquotalimits 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 ftpquotatallies 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}'" ftpquotatallies

SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" ftpquotatallies

QuotaLimitTable sql:/get-quota-limit
QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally

RootLogin off
RequireValidShell off

Neustart von proftpd durchführen:

/etc/init.d/proftpd restart

Sollte bei Debian Etch folgender Fehler auftreten:

Starting ftp server: proftpd - Fatal: unknown configuration directive 'SQLAuthTypes' on line 42 of '/etc/proftpd/proftpd.conf'
 failed!

behebt folgende Zeile behebt das Problem.

Zeile in die proftpd.conf einfügen und restarten:

Include /etc/proftpd/modules.conf 

Test von proftpd

User / Gruppe in der Datenbank anlegen

MySQL Shell einloggen

mysql -u root -p

Datenbank wechseln

USE ftp;

Gruppe anlegen

Dies muss nur einmal gemacht werden da die gleiche Gruppe für alle User gilt!!!

INSERT INTO `ftpgroup` (`groupname`, `gid`, `members`) VALUES ('ftpgroup', 2001, 'ftpuser');

User und Quota anlegen

Das Passwort ist “password“.

 
INSERT INTO `ftpquotalimits` (`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`) VALUES ('testuser', 'user', 'true', 'hard', 15728640, 0, 0, 0, 0, 0);
INSERT INTO `ftpuser` (`id`, `userid`, `passwd`, `uid`, `gid`, `homedir`, `shell`, `count`, `accessed`, `modified`) VALUES (1, 'testuser', '{md5}X03MO1qnZdYdgyfeuILPmQ==', 2001, 2001, '/home/testuser', '/sbin/nologin', 0, '', '');

Ausloggen

quit;

Test

>> vmsrv01:/# ftp localhost
<< Connected to localhost.
<< 220 ProFTPD 1.3.0 Server (Debian) [127.0.0.1]
>> Name (localhost:root): testuser
<< 331 Password required for testuser.
>> Password:
<< 230 User testuser logged in.
<< Remote system type is UNIX.
<< Using binary mode to transfer files.
<< ftp>
>> ftp> quit
<< 221 Goodbye.
<< vmsrv01:/#

Das Verzeichnis für testuser sollte jetzt unter /home angelgt sein und ftpuser:ftpgroup gehören.

>> vmsrv01:/# ls -la /home
<< drwxr-xr-x  6 root     root     4096 2007-12-17 13:10 .
<< drwxr-xr-x 23 root     root     4096 2007-10-25 14:09 ..
<< drwxr-xr-x  2 ftpuser  ftpgroup 4096 2007-12-17 13:10 testuser
<< vmsrv01:/#

Verschlüsselte Passwörter für proftpd erzeugen

Welche Verschlüsselungsverfahren möglich sind können Sie mit dem Kommando

openssl list-message-digest-commands

abfragen.

Ergebnis:

vmsrv01:/# openssl list-message-digest-commands
md2
md4
md5
rmd160
sha
sha1
vmsrv01:/#

auf der Konsole

MD5

Kommando
/bin/echo "{md5}"`/bin/echo -n "password" | openssl dgst -binary -md5 | openssl enc -base64`
Ergebnis
{md5}X03MO1qnZdYdgyfeuILPmQ==

Alle möglichen Typen

Kommando
for c in `openssl list-message-digest-commands`; do
    /bin/echo "{$c}"`/bin/echo -n "password" | openssl dgst -binary -$c | openssl enc -base64`
  done
Ergebnis
{md2}8DiBqIxuORNfDsxg79YJuQ==
{md4}ip0JPxT4cB3xdzKyuxgsdA==
{md5}X03MO1qnZdYdgyfeuILPmQ==
{rmd160}LAjo9YhHUKe5n28vNC/GONsl/zE=
{sha}gAclaL6zshAjJesgP20P+S9c744=
{sha1}W6ph5Mm5Pz8GgiULbPgzG37mj9g=

mit PHP

MD5

Kommando
// $password contains the cleartext password before
$password = "{md5}".base64_encode(pack("H*", md5($password)));
// $password now contains the encrypted, encoded password
Ergebnis
{md5}X03MO1qnZdYdgyfeuILPmQ==

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.