http://os.51cto.com/art/201102/245850.htm

Debian所系统目前采用的是 Linux 内核。Debian 支持内核的工作也正在进行。最主要的就是 Hurd,Hurd 是一组在微内核 (例如 Mach) 上运行的提供各种不同功能的守护进程。本文讲述的是Debian中proftpd+mysql+虚拟用户+匿名用户+磁盘限额的配置。

用惯了proftpd,本来打算用pureftpd的,后来想想算了。proftpd虚拟用户的配置其实很早就解决了,只是虚拟用户和本地用户同时登录,一直没有解决。于是趁这个机会仔细研究了下。依然是debian下面的配置。

安装mysql和phpmyadmin,其中phpmyadmin不是必需的

 
  
  1. apt-get install mysql-server mysql-client libmysqlclient15-dev phpmyadmin apache2  
  2.  

为mysql设置root密码

 
  
  1. mysqladmin -u root password yourrootsqlpassword  
  2.  

如果需要其他人访问本机的mysql,同样需要设置密码

 
  
  1. mysqladmin -h server1.example.com -u root password yourrootsqlpassword  
  2.  

安装带mysql支持的proftpd,注意选择proftpd工作在standalone模式

 
  
  1. apt-get install proftpd-mysql  
  2.  

建立虚拟用户组,这个是为了把proftpd用户虚拟到本机的一个用户上。注意下面的2001修改为自定义的。

 
  
  1. groupadd -g 2001 ftpgroup  
  2.  
  3. useradd -u 2001 -s /bin/false -d /bin/null -c "proftpd user" -g ftpgroup ftpuser  
  4.  

建立proftpd使用的mysql数据库,并创建数据表。

 
  
  1. bt:~# mysql -u root -p  
  2.  
  3. Enter password:  
  4.  
  5. Welcome to the MySQL monitor. Commands end with ; or \g.  
  6.  
  7. Your MySQL connection id is 18  
  8.  
  9. Server version: 5.0.32-Debian_7etch1-log Debian etch distribution  
  10.  
  11. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  12.  
  13. mysql> create database ftp;  
  14.  
  15. Query OK, 1 row affected (0.00 sec)  
  16.  
  17. mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON ftp.* TO 'proftpd'@'localhost' IDENTIFIED BY 'password';  
  18.  
  19. Query OK, 0 rows affected (0.00 sec)  
  20.  
  21. mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON ftp.* TO 'proftpd'@'localhost.localdomain' IDENTIFIED BY 'password';  
  22.  
  23. Query OK, 0 rows affected (0.00 sec)  
  24.  
  25. mysql> USE ftp;  
  26.  
  27. Database changed  
  28.  
  29. mysql> 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';  
  30.  
  31. Query OK, 0 rows affected, 1 warning (0.06 sec)  
  32.  
  33. mysql> CREATE TABLE ftpquotalimits (  
  34.  
  35. -> name varchar(30) default NULL,  
  36.  
  37. -> quota_type enum('user','group','class','all') NOT NULL default 'user',  
  38.  
  39. -> per_session enum('false','true') NOT NULL default 'false',  
  40.  
  41. -> limit_type enum('soft','hard') NOT NULL default 'soft',  
  42.  
  43. -> bytes_in_avail int(10) unsigned NOT NULL default '0',  
  44.  
  45. -> bytes_out_avail int(10) unsigned NOT NULL default '0',  
  46.  
  47. -> bytes_xfer_avail int(10) unsigned NOT NULL default '0',  
  48.  
  49. -> files_in_avail int(10) unsigned NOT NULL default '0',  
  50.  
  51. -> files_out_avail int(10) unsigned NOT NULL default '0',  
  52.  
  53. -> files_xfer_avail int(10) unsigned NOT NULL default '0'  
  54.  
  55. -> ) TYPE=MyISAM;  
  56.  
  57. Query OK, 0 rows affected, 1 warning (0.03 sec)  
  58.  
  59. mysql> CREATE TABLE ftpquotatallies (  
  60.  
  61. -> name varchar(30) NOT NULL default '',  
  62.  
  63. -> quota_type enum('user','group','class','all') NOT NULL default 'user',  
  64.  
  65. -> bytes_in_used int(10) unsigned NOT NULL default '0',  
  66.  
  67. -> bytes_out_used int(10) unsigned NOT NULL default '0',  
  68.  
  69. -> bytes_xfer_used int(10) unsigned NOT NULL default '0',  
  70.  
  71. -> files_in_used int(10) unsigned NOT NULL default '0',  
  72.  
  73. -> files_out_used int(10) unsigned NOT NULL default '0',  
  74.  
  75. -> files_xfer_used int(10) unsigned NOT NULL default '0'  
  76.  
  77. -> ) TYPE=MyISAM;  
  78.  
  79. Query OK, 0 rows affected, 1 warning (0.03 sec)  
  80.  
  81. mysql> CREATE TABLE ftpuser (  
  82.  
  83. -> id int(10) unsigned NOT NULL auto_increment,  
  84.  
  85. -> userid varchar(32) NOT NULL default '',  
  86.  
  87. -> passwd varchar(32) NOT NULL default '',  
  88.  
  89. -> uid smallint(6) NOT NULL default '5500',  
  90.  
  91. -> gid smallint(6) NOT NULL default '5500',  
  92.  
  93. -> homedir varchar(255) NOT NULL default '',  
  94.  
  95. -> shell varchar(16) NOT NULL default '/sbin/nologin',  
  96.  
  97. -> count int(11) NOT NULL default '0',  
  98.  
  99. -> accessed datetime NOT NULL default '0000-00-00 00:00:00',  
  100.  
  101. -> modified datetime NOT NULL default '0000-00-00 00:00:00',  
  102.  
  103. -> PRIMARY KEY (id),  
  104.  
  105. -> UNIQUE KEY userid (userid)  
  106.  
  107. -> ) TYPE=MyISAM COMMENT='ProFTP user table';  
  108.  
  109. Query OK, 0 rows affected, 1 warning (0.03 sec)  
  110.  
  111. mysql> quit;  
  112.  
  113. Bye  
  114.  

然后就是修改proftpd的配置文件了,nano /etc/proftpd/proftpc.conf

首先关闭对ipv6支持

UseIPv6 off

然后增加对mysql的验证支持

 

-------------------

 
  
  1. DefaultRoot ~  
  2.  
  3. # The passwords in MySQL are encrypted using CRYPT  
  4.  
  5. SQLAuthTypes Plaintext Crypt  
  6.  
  7. SQLAuthenticate users groups  
  8.  
  9. # used to connect to the database  
  10.  
  11. # databasename@host database_user user_password  
  12.  
  13. SQLConnectInfo ftp@localhost proftpd password  
  14.  
  15. # Here we tell ProFTPd the names of the database columns in the "usertable"  
  16.  
  17. # we want it to interact with. Match the names with those in the db  
  18.  
  19. SQLUserInfo ftpuser userid passwd uid gid homedir shell  
  20.  
  21. # Here we tell ProFTPd the names of the database columns in the "grouptable"  
  22.  
  23. # we want it to interact with. Again the names match with those in the db  
  24.  
  25. SQLGroupInfo ftpgroup groupname gid members  
  26.  
  27. # set min UID and GID - otherwise these are 999 each  
  28.  
  29. SQLMinID 500  
  30.  
  31. # create a user's home directory on demand if it doesn't exist  
  32.  
  33. SQLHomedirOnDemand on  
  34.  
  35. # Update count every time user logs in  
  36.  
  37. SQLLog PASS updatecount  
  38.  
  39. SQLNamedQuery updatecount UPDATE "countcount=count+1, accessed=now() WHERE userid='%u'" ftpuser  
  40.  
  41. # Update modified everytime user uploads or deletes a file  
  42.  
  43. SQLLog STOR,DELE modified  
  44.  
  45. SQLNamedQuery modified UPDATE "modified=now() WHERE userid='%u'" ftpuser  
  46.  
  47. # User quotas  
  48.  
  49. # ===========  
  50.  
  51. QuotaEngine on  
  52.  
  53. QuotaDirectoryTally on  
  54.  
  55. QuotaDisplayUnits Mb  
  56.  
  57. QuotaShowQuotas on  
  58.  
  59. 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}'"  
  60.  
  61. 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}'"  
  62.  
  63. SQLNamedQuery update-quota-tally UPDATE "bytes_in_usedbytes_in_used = bytes_in_used + %{0}, bytes_out_usedbytes_out_used = bytes_out_used + %{1}, bytes_xfer_usedbytes_xfer_used = bytes_xfer_used + %{2}, files_in_usedfiles_in_used = files_in_used + %{3}, files_out_usedfiles_out_used = files_out_used + %{4}, files_xfer_usedfiles_xfer_used = files_xfer_used + %{5} WHERE name = '%{6}' AND quota_type = '%{7}'" ftpquotatallies  
  64.  
  65. SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" ftpquotatallies  
  66.  
  67. QuotaLimitTable sql:/get-quota-limit  
  68.  
  69. QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally  
  70.  
  71. RootLogin off  
  72.  
  73. RequireValidShell off  
  74.  

然后nano /etc/proftpd/modules.conf,注释掉没用的部分,然后重新启动proftpd

#LoadModule mod_sql_postgres.c

/etc/init.d/proftpd restart

建立数据库并测试,强烈推荐这些通过phpmyadmin来进行操作

 
  
  1. mysql -u root -p  
  2.  
  3. USE ftp;  
  4.  
  5. INSERT INTO `ftpgroup` (`groupname`, `gid`, `members`) VALUES ('ftpgroup', 2001, 'ftpuser');  
  6.  
  7. 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 ('exampleuser', 'user', 'true', 'hard', 15728640, 0, 0, 0, 0, 0);  
  8.  
  9. INSERT INTO `ftpuser` (`id`, `userid`, `passwd`, `uid`, `gid`, `homedir`, `shell`, `count`, `accessed`, `modified`) VALUES (1, 'exampleuser', 'secret', 2001, 2001, '/home/www.example.com', '/sbin/nologin', 0, '', '');  
  10.  
  11. quit;  
  12.  

下面是配置匿名用户登录

依然是增加一个用户组

 
  
  1. groupadd -g 2002 anonymous_ftp  
  2.  
  3. useradd -u 2002 -s /bin/false -d /home/anonymous_ftp -m -c "Anonymous FTP User" -g anonymous_ftp anonymous_ftp  
  4.  

建立登录文件夹,如果想匿名用户和虚拟用户登录到同一个地方,这一步可以略过,在proftpd的配置文件中指定就行了

 
  
  1. mkdir /home/anonymous_ftp/incoming  
  2.  
  3. chown anonymous_ftp:nogroup /home/anonymous_ftp/incoming  
  4.  

然后编辑proftpd的配置文件,增加下面部分,然后重新启动。配置文件具体含义不说了

 

 
  
  1.    
  2.  
  3. User anonymous_ftp  
  4.  
  5. Group nogroup  
  6.  
  7. # We want clients to be able to login with "anonymous" as well as "ftp"  
  8.  
  9. UserAlias anonymous anonymous_ftp  
  10.  
  11. # Cosmetic changes, all files belongs to ftp user  
  12.  
  13. DirFakeUser on anonymous_ftp  
  14.  
  15. DirFakeGroup on anonymous_ftp  
  16.  
  17. RequireValidShell off  
  18.  
  19. # Limit the maximum number of anonymous logins  
  20.  
  21. MaxClients 10  
  22.  
  23. # We want 'welcome.msg' displayed at login, and '.message' displayed  
  24.  
  25. # in each newly chdired directory.  
  26.  
  27. DisplayLogin welcome.msg  
  28.  
  29. DisplayFirstChdir .message  
  30.  
  31. # Limit WRITE everywhere in the anonymous chroot   
  32.  
  33. DenyAll  
  34.  
  35. # Uncomment this if you're brave.    
  36.  
  37. # Umask 022 is a good standard umask to prevent new files and dirs  
  38.  
  39. # (second parm) from being group and world writable.  
  40.  
  41. Umask 022 022  
  42. DenyAll  
  43.  
  44. AllowAll  
  45.  

这样配置Debian中proftpd+mysql+虚拟用户+匿名用户+磁盘限额的配置 就ok了。