在OpenBSD上开启ftp服务器

在OpenBSD环境下搭建各种服务器的相关讨论。

版主: lionuxchenjun天地乾坤

回复
头像
leo
帖子: 2465
注册时间: 2010-01-21 3:27

在OpenBSD上开启ftp服务器

帖子 leo » 2010-08-31 4:49

OpenBSD系统自带有ftp服务器,但默认情况下并开启,当传送大量文件时(例如几百G时),如果用OpenSSH来作为传输工具多少有些吃力,我这里在两台具有同样具有千M网卡的计算机互传时,OpenSSH的传输速率只能达到7、8M/s, 也就是说抛开硬盘速度的限制不谈(这个限制远小于OpenSSH的传输速率限制),肯定是openssh的设计或机制上有局限性,主要功能并非是作为文件服务器使用,而是安全的shell, 而且在传输文件时ssh进程占用的资源也很大,虽然在小型环境中还能应付,但是肯定不能适应多用户大流量的情况。例如我有2T的高清影视需要分享,用sftp服务器来做是不可想象的,也就是说实际上sftp服务器并不能在真正意义上取代传统的ftp服务器。
所以在传输大量文件时,我们有时候不得不回头来寻找ftp服务器,我们在以前的帖子里已经探讨了怎样在OpenBSD的环境里搭建pure-ftpd服务器,有些朋友可能觉得有些繁琐,可能就是个人需要传送一些文件,并非对外提供服务。
实际上OpenBSD基本系统已经带有ftp服务器,但是默认情况下并未开启,你只要设置一下就可以了,如果在一段时间内需要FTP服务器的服务,编辑/etc/rc.conf.local文件,加上一句(任务结束后别忘了关闭——可以先将这行注释掉,留着下次使用。):

代码: 全选

ftpd_flags="-DllUSA"
或者更保险的做法就是临时需要时再用如下的命令启用OpenBSD自带的ftp服务器:

代码: 全选

/usr/libexec/ftpd -DllUSA

还有一些其它参数,关于具体的参数,请查阅用户手册:

经实际上测试,千M网卡(廉价的8169)之间的传输速度最快可以到达总线或硬盘速度的极限,我这里新机器之间可以到60M/s,就连老的dell GX400的FTP传输速度也可以到达25M/s(应该是总线的限制吧)。
我们这里仅提供一般性的方法,请根据自己的情况调整。主要注意,用户名不能包含在 /etc/ftpusers 文件内,在这个文件里的用户会被拒绝登录.
============================================================
如果你的ftp并不对外而是自用,或者换句话说,你的PF只允许你相信的内网用户(或者只有你自己)访问FTP服务器,那么上面的设定问题不大,但是如果你的FTP服务器还对外服务的话,上面的设定就很不安全了,你还需要进行其它的设置:
正常的登录用户必须包含在/etc/ftpchroot里,这个文件里的用户会被chroot在自己的目录内。

我们下面说说对外服务的ftp的设置:

匿名帐号的设置
我们假设你要提供匿名账号anonymous和专用帐号给外网用户使用,那么你需要做以下事情:
1. 在你设置ftp帐号前需要先配置shell, 也就是给ftp用户分配一个不能访问控制台的shell,我们这里为其分配 /usr/bin/false, /usr/bin/false是一个sh脚本,我们看一下这个脚本的内容:

代码: 全选

# cat /usr/bin/false
#! /bin/sh
#       $OpenBSD: false.sh,v 1.2 1996/06/26 05:32:50 deraadt Exp $
exit 1
#
到这里你应该明白了,我们这样分配的目的就是任何分配了这个shell(也就是false)的用户在企图用自己的帐号访问控制台时都会自动退出。
这个脚本在默认安装时已经有了,但是并未包含在 /etc/shells这个文件里。我们先看一下这个文件:

代码: 全选

# cat /etc/shells
#       $OpenBSD: shells,v 1.8 2009/02/14 17:06:40 sobrado Exp $
#
# list of acceptable shells for chpass(1).
# ftpd(8) will not allow users to connect who are not using
# one of these shells, unless the user is listed in /etc/ftpchroot.
/bin/sh
/bin/csh
/bin/ksh
#
从该文件的注释里我们知道,如果ftp用户的shell没有列在这个文件里,则系统不会允许这个用户使用ftp服务器。所以我们还需要把flase这个shell放到这个文件里,我们在文件的最后加上这个shell——flase,

代码: 全选

# /usr/bin/false >> /etc/shells

调整后的文件像这样:

代码: 全选

# cat /etc/shells
#       $OpenBSD: shells,v 1.8 2009/02/14 17:06:40 sobrado Exp $
#
# list of acceptable shells for chpass(1).
# ftpd(8) will not allow users to connect who are not using
# one of these shells, unless the user is listed in /etc/ftpchroot.
/bin/sh
/bin/csh
/bin/ksh
[B][color=red]/usr/bin/false[/color][/B]
#

2. 创建匿名帐号
OpenBSD默认不存在匿名用户,我们要创建一个:

代码: 全选

# adduser
Use option ``-silent'' if you don't want to see all warnings and questions.
Reading /etc/shells
Check /etc/master.passwd
Check /etc/group
Ok, let's go.
Don't worry about mistakes. There will be a chance later to correct any input.
Enter username []: [B][color=royalblue]ftp[/color][/B]
Enter full name []: [B][color=royalblue]anonymous ftp[/color][/B]
Enter shell csh false ksh nologin sh [ksh]: [B][color=royalblue]false[/color][/B]
Uid [1002]: [color=royalblue][B][I]enter[/I][/B][/color]
Login group ftp [ftp]: [color=royalblue][B][I]enter[/I][/B][/color]
Login group is ``ftp''. Invite ftp into other groups: guest no
[no]: [color=royalblue][B][I]enter[/I][/B][/color]
Login class authpf daemon default mysql staff [default]: [color=royalblue][B][I]enter[/I][/B][/color]
Enter password []: [color=royalblue][B][I]enter[/I][/B][/color]
Set the password so that user cannot logon? (y/n) [n]: [color=royalblue][B]y[/B][/color]
Name:        ftp
Password:    ****
Fullname:    anonymous ftp
Uid:         1002
Gid:         1002 (ftp)
Groups:      ftp
Login Class: default
HOME:        /home/ftp
Shell:       /usr/bin/false
OK? (y/n) [y]: [color=royalblue][B][I]enter[/I][/B][/color]
Added user ``ftp''
Copy files from /etc/skel to /home/ftp
Add another user? (y/n) [y]: [B][color=royalblue]n[/color][/B]
Goodbye!
#
读者在这里不要害怕不给其设密码的目的就是不让其使用除ftp以外其它的方式登录系统。

3. 设置权限
这个 /home/ftp 目录必须属于root, 且属性必须为555,我们在里面再设置一个对外服务目录pub, 在 pub 下再设置两个子目录 download 和 upload 分别放置上传和下载的资源。

代码: 全选

# mkdir -p /home/ftp/pub/upload
# mkdir -p /home/ftp/pub/download
# chown -R root:wheel /home/ftp
# chmod -R 555 /home/ftp
因为我们要允许匿名用户在upload这个目录内创建目录和文件,所以这个目录的权限要单独设置,我们这里设置为目录的所有者还是root, 但是目录的权限是773, 也就是说匿名用户可以创建目录和文件,但是不能下载和删除其他匿名用户上传的内容(因为对ftp这个用户来说这个目录的内容不可见),如此一来,在一个匿名用户在上传文件时就没有其他的匿名用户进行下载了——这可能造成文件锁定无法继续上传,也就避免了上传时的干扰,同时还可以避免某些恶意的匿名用户删除其他人的文件。

代码: 全选

chmod -R 773 /home/ftp/pub/upload

用户上传完毕后,可以通知管理员将upload目录内传完内容移到download目录下公开下载。——实际不用通知,在每个上传的目录内加上一个目录@done就可以了表示已经传完了。:)


到这里匿名用户的配置基本上就结束了,因为你多半也不会给这个用户太多的权限。此外需要说明的是这个匿名用户ftp你不用设置在/etc/ftpchroot 文件内,因为系统已经禁止其随意溜达了。:cool:

剩下的就是建立一个 /etc/ftpwelcome 文件,在里面写入欢迎信息。
下面我们再说一下,系统内普通用户的设置:

4.8里非匿名用户的username必须加入到ftpchroot里,否则系统会拒绝其登录。

(未完待续!)

这里附上ftpd用户手册:

代码: 全选

# man ftpd
FTPD(8)                 OpenBSD System Manager's Manual                FTPD(8)
 
NAME
     ftpd - Internet File Transfer Protocol server
 
SYNOPSIS
     ftpd [-46ADdlMnPSU] [-T maxtimeout] [-t timeout] [-u mask]
 
DESCRIPTION
     ftpd is the Internet File Transfer Protocol server process.  The server
     uses the TCP protocol and listens at the port specified in the ``ftp''
     service specification; see services(5).
 
     The options are as follows:
 
     -4      When -D is specified, forces ftpd to use IPv4 addresses only.
 
     -6      When -D is specified, forces ftpd to use IPv6 addresses only.
 
     -A      Permit only anonymous FTP connections (unless the -n option is
             specified), accounts listed in /etc/ftpchroot or users in a login
             class with the ``ftp-chroot'' variable set (see below).  Other
             connection attempts are refused.
 
     -D      With this option set, ftpd will detach and become a daemon,
             accepting connections on the FTP port and forking child processes
             to handle them.  This has lower overhead than starting ftpd from
             inetd(8) and is thus useful on busy servers to reduce load.
 
     -d      Debugging information is written to the syslog using LOG_FTP.
 
     -l      Each successful and failed FTP session is logged using syslog
             with a facility of LOG_FTP.  If this option is specified twice,
             the retrieve (get), store (put), append, delete, make directory,
             remove directory and rename operations and their filename
             arguments are also logged.
 
     -M      Enables multihomed mode.  Instead of simply using ~ftp for
             anonymous transfers, a directory matching the fully qualified
             name of the IP number the client connected to, and located inside
             ~ftp, is used instead.
 
     -n      Do not permit anonymous FTP logins.  Normally they are permitted.
 
     -P      Permit illegal port numbers or addresses for PORT command
             initiated connects.  By default ftpd violates the RFC and thus
             constrains the PORT command to non-reserved ports and requires it
             use the same source address as the connection came from.  This
             prevents the "FTP bounce attack" against services on both the
             local machine and other local machines.
 
     -S      With this option set, ftpd logs all anonymous downloads to the
             file /var/log/ftpd when this file exists.
 
     -T maxtimeout
             A client may also request a different timeout period; the maximum
             period allowed may be set to maxtimeout seconds with the -T
             option.  The default limit is 2 hours.
 
     -t timeout
             The inactivity timeout period is set to timeout seconds (the
             default is 15 minutes).
 
     -U      Each concurrent FTP session is logged to the file /var/run/utmp,
             making them visible to commands such as who(1).
 
     -u mask
             Force the umask to mask, instead of the default specified in
             /etc/login.conf (usually 022).  Also disallows chmod.
 
     The file /etc/nologin can be used to disable FTP access.  If the file
     exists, ftpd displays it and exits.  Note: this method will disable all
     non-root logins; see login(1) for further details.  If the file
     /etc/ftpwelcome exists, ftpd prints it before issuing the ``ready''
     message.  If the welcome file exists (/etc/motd by default), ftpd prints
     it after a successful login.  If the file .message exists in a directory,
     ftpd prints it when that directory is entered.
 
     The FTP server currently supports the following FTP requests.  The case
     of the requests is ignored.
 
           Request    Description
           ABOR       abort previous command
           ACCT       specify account (not implemented)
           ALLO       allocate storage (vacuously)
           APPE       append to a file
           CDUP       change to parent of current working directory
           CWD        change working directory
           DELE       delete a file
           EPRT       specify data connection port
           EPSV       prepare for server-to-server transfer
           HELP       give help information
           LIST       give list of files in a directory (ls -lgA)
           LPRT       specify data connection port
           LPSV       prepare for server-to-server transfer
           MDTM       show last modification time of file
           MKD        make a directory
           MODE       specify data transfer mode
           NLST       give name list of files in directory
           NOOP       do nothing
           PASS       specify password
           PASV       prepare for server-to-server transfer
           PORT       specify data connection port
           PWD        print the current working directory
           QUIT       terminate session
           REIN       reinitialize (not implemented)
           REST       restart incomplete transfer
           RETR       retrieve a file
           RMD        remove a directory
           RNFR       specify rename-from file name
           RNTO       specify rename-to file name
           SITE       non-standard commands (see next section)
           SIZE       return size of file
           SMNT       structure mount (not implemented)
           STAT       return status of server
           STOR       store a file
           STOU       store a file with a unique name
           STRU       specify data transfer structure
           SYST       show operating system type of server system
           TYPE       specify data transfer type
           USER       specify user name; not valid after login
           XCUP       change to parent of current working directory (deprec.)
           XCWD       change working directory (deprecated)
           XMKD       make a directory (deprecated)
           XPWD       print the current working directory (deprecated)
           XRMD       remove a directory (deprecated)
 
     The following non-standard or UNIX specific commands are supported by the
     SITE request:
 
           Request    Description
           CHMOD      change mode of a file, e.g., SITE CHMOD 755 filename
           HELP       give help information
           IDLE       set idle-timer, e.g., SITE IDLE 60
           UMASK      change umask, e.g., SITE UMASK 002
 
     The remaining FTP requests specified in Internet RFC 959 are recognized,
     but not implemented.  MDTM and SIZE are not specified in RFC 959, but
     will appear in the next updated FTP RFC.
 
     The FTP server will abort an active file transfer only when the ABOR
     command is preceded by a Telnet "Interrupt Process" (IP) signal and a
     Telnet "Synch" signal in the command Telnet stream, as described in
     Internet RFC 959.  If a STAT command is received during a data transfer,
     preceded by a Telnet IP and Synch, transfer status will be returned.
 
     ftpd interprets file names according to the ``globbing'' conventions used
     by csh(1).  This allows users to utilize the metacharacters ``*?[]{}~''.
 
     ftpd authenticates users by using the service and type of ftp, as defined
     in the /etc/login.conf file (see login.conf(5)).  An authentication style
     may be specified by appending with a colon (`:') following the
     authentication style, i.e. ``joe:skey''.  The allowed authentication
     styles for ftpd may be explicitly specified by the ``auth-ftp'' entry in
     /etc/login.conf.
 
     ftpd authenticates users according to five rules.
 
           1.   The login name must be in the password database and not have a
                null password.  In this case a password must be provided by
                the client before any file operations may be performed.
 
           2.   The login name must not appear in the file /etc/ftpusers.
 
           3.   The user must have a standard shell as described by shells(5).
 
           4.   If the user name appears in the file /etc/ftpchroot, the
                session's root will be changed to the user's login directory
                by chroot(2) as for an ``anonymous'' or ``ftp'' account (see
                next item).  However, the user must still supply a password.
                This feature is intended as a compromise between a fully
                anonymous account and a fully privileged account.  The account
                should also be set up as for an anonymous account.
 
           5.   If the user name is ``anonymous'' or ``ftp'', an anonymous FTP
                account must be present in the password file (user ``ftp'').
                In this case the user is allowed to log in by specifying any
                password (by convention an email address for the user should
                be used as the password).
 
     Once a user is authenticated the user must be approved by any approval
     script defined (see login.conf(5)).  If a valid approval script (by
     either :approve=...: or :approve-ftp=...: for the user's class) is
     defined then it is run and must exit with a 0 (success) status.  When
     ftpd is running under the -D flag (and debugging is not turned on) then
     the approval script will be called with at least the following variables
     specified via the -v option (see login.conf(5)) to the approve script:
 
           Variable    Description
           FTPD_HOST   The server's (virtual) hostname
 
     For example (the line is broken to fit the page):
 
           /usr/libexec/auth/approve_ftpd -v FTPD_HOST=ftp.mycompany.com \
                   username class service
 
     When the user logs in to the anonymous FTP account, ftpd takes special
     measures to restrict the client's access privileges.  The server performs
     a chroot(2) to the home directory of the ``ftp'' user.  In order that
     system security is not breached, it is recommended that the ``ftp''
     subtree be constructed with care, following these rules:
 
           ~ftp      Make the home directory owned by ``root'' and unwritable
                     by anyone (mode 555).
 
           ~ftp/bin  Make this directory owned by ``root'' and unwritable by
                     anyone (mode 511).  This directory is optional unless you
                     have commands you wish the anonymous FTP user to be able
                     to run (the ls(1) command exists as a built-in).  Any
                     programs in this directory should be mode 111 (executable
                     only).
 
           ~ftp/etc  Make this directory owned by ``root'' and unwritable by
                     anyone (mode 511).  The files pwd.db (see pwd_mkdb(8))
                     and group(5) must be present for the ls(1) command to be
                     able to produce owner names rather than numbers.  The
                     password field in pwd.db is not used, and should not
                     contain real passwords.  The file motd, if present, will
                     be printed after a successful login.  These files should
                     be mode 444.
 
           ~ftp/pub  Make this directory mode 555 and owned by ``root''.  This
                     is traditionally where publicly accessible files are
                     stored for download.
 
     If logging to the /var/log/ftpd file is enabled, information will be
     written in the following format:
 
           time            The time and date of the download, in ctime(3)
                           format.
           elapsed time    The elapsed time, in seconds.
           remote host     The remote host (or IP number).
           bytes           The number of bytes transferred.
           path            The full path (relative to the FTP chroot space) of
                           the file transferred.
           type            The type of transfer; either `a' for ASCII or `b'
                           for binary.
           unused          Unused field containing a `*', for compatibility.
           unused          Unused field containing an `o', for compatibility.
           user type       The type of user; either `a' for anonymous or `r'
                           for a real user (should always be anonymous).
           name            Either a system login name or the value given for
                           ``email address'' if an anonymous user.
           unused          Unused field containing a `0', for compatibility.
           real name       The system login name if the connection is not
                           anonymous, or a `*' if it is.
 
     Although fields exist for logging information on real users, this file is
     only used for anonymous downloads.  Unused fields exist only for
     compatibility with other ftpd implementations.
 
LOGIN.CONF VARIABLES
     The ftpd daemon uses the following FTP-specific parameters:
 
     auth-ftp    The list of authentication types available to this class.
                 See login.conf(5).
 
     ftp-chroot  A boolean value.  If set, users in this class will be
                 automatically chrooted to the user's login directory.
 
     ftp-dir     A path to a directory.  This value overrides the login
                 directory for users in this class.  A leading tilde (`~') in
                 ftp-dir will be expanded to the user's home directory based
                 on the contents of the password database.
 
     welcome     The path of the file containing the welcome message.  If this
                 variable is not set, /etc/motd is used.
 
PORT ALLOCATION
     For passive mode data connections, ftpd will listen to a random high TCP
     port.  The interval of ports used are configurable using sysctl(8)
     variables net.inet.ip.porthifirst and net.inet.ip.porthilast.
 
FILES
     /etc/ftpchroot     list of normal users who should be chrooted
     /etc/ftpusers      list of unwelcome/restricted users
     /etc/ftpwelcome    welcome notice
     /etc/login.conf    authentication styles
     /etc/motd          printed after a successful login
     /etc/nologin       displayed and access refused
     /var/log/ftpd      log file for anonymous downloads
     /var/run/ftpd.pid  process ID if running in daemon mode
     /var/run/utmp      list of users on the system
 
SEE ALSO
     ftp(1), login(1), skey(1), who(1), chroot(2), ctime(3), group(5),
     login.conf(5), motd(5), services(5), shells(5), ftp-proxy(8), inetd(8),
     pwd_mkdb(8), sysctl(8), syslogd(8)
 
HISTORY
     The ftpd command appeared in 4.2BSD.
 
OpenBSD 4.8                      May 31, 2007                      OpenBSD 4.8

wkx9dragon
锌 Zn
帖子: 493
注册时间: 2010-02-02 18:00

帖子 wkx9dragon » 2010-08-31 21:58

好久没用openbsd了,主要是没有迅雷这种高速断点续传软件,还有ntfs写。

头像
leo
帖子: 2465
注册时间: 2010-01-21 3:27

帖子 leo » 2010-08-31 22:36

wkx9dragon 写了:好久没用openbsd了,主要是没有迅雷这种高速断点续传软件,还有ntfs写。
确实如此,前者是下载的利器,可惜BSD上没有,后者是闭源产品,还牵扯到数据,所以多少令破解者有些投鼠忌器。
类Unix在桌面上要追上Windows还有很长的路要走,甚至是不可能的,因为这不是发展的主要目的,而且专业公司开发的相关应用软件少之又少,不过话说回来,不挣钱人家对不起股东,所以这点上也无可厚非。

你失踪了一段时间,我以为你一直在钻研BSD的源代码:)

回复

在线用户

正浏览此版面之用户: 没有注册用户 和 32 访客