| 1 |
# Copyright (c) Twisted Matrix Laboratories. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
An example FTP server with minimal user authentication. |
| 6 |
""" |
| 7 |
|
| 8 |
from twisted.cred.checkers import AllowAnonymousAccess, FilePasswordDB |
| 9 |
from twisted.cred.portal import Portal |
| 10 |
from twisted.internet import reactor |
| 11 |
from twisted.protocols.ftp import FTPFactory, FTPRealm |
| 12 |
|
| 13 |
# |
| 14 |
# First, set up a portal (twisted.cred.portal.Portal). This will be used |
| 15 |
# to authenticate user logins, including anonymous logins. |
| 16 |
# |
| 17 |
# Part of this will be to establish the "realm" of the server - the most |
| 18 |
# important task in this case is to establish where anonymous users will |
| 19 |
# have default access to. In a real world scenario this would typically |
| 20 |
# point to something like '/pub' but for this example it is pointed at the |
| 21 |
# current working directory. |
| 22 |
# |
| 23 |
# The other important part of the portal setup is to point it to a list of |
| 24 |
# credential checkers. In this case, the first of these is used to grant |
| 25 |
# access to anonymous users and is relatively simple; the second is a very |
| 26 |
# primitive password checker. This example uses a plain text password file |
| 27 |
# that has one username:password pair per line. This checker *does* provide |
| 28 |
# a hashing interface, and one would normally want to use it instead of |
| 29 |
# plain text storage for anything remotely resembling a 'live' network. In |
| 30 |
# this case, the file "pass.dat" is used, and stored in the same directory |
| 31 |
# as the server. BAD. |
| 32 |
# |
| 33 |
# Create a pass.dat file which looks like this: |
| 34 |
# |
| 35 |
# ===================== |
| 36 |
# jeff:bozo |
| 37 |
# grimmtooth:bozo2 |
| 38 |
# ===================== |
| 39 |
# |
| 40 |
p = Portal(FTPRealm("./"), [AllowAnonymousAccess(), FilePasswordDB("pass.dat")]) |
| 41 |
|
| 42 |
# |
| 43 |
# Once the portal is set up, start up the FTPFactory and pass the portal to |
| 44 |
# it on startup. FTPFactory will start up a twisted.protocols.ftp.FTP() |
| 45 |
# handler for each incoming OPEN request. Business as usual in Twisted land. |
| 46 |
# |
| 47 |
f = FTPFactory(p) |
| 48 |
|
| 49 |
# |
| 50 |
# You know this part. Point the reactor to port 21 coupled with the above factory, |
| 51 |
# and start the event loop. |
| 52 |
# |
| 53 |
reactor.listenTCP(21, f) |
| 54 |
reactor.run() |