<s> from opencanary . modules import CanaryService <EOL> from twisted . application import internet <EOL> from twisted . protocols . ftp import FTPFactory , FTPRealm , FTP , USR_LOGGED_IN_PROCEED , GUEST_LOGGED_IN_PROCEED , IFTPShell , AuthorizationError <EOL> from twisted . cred . portal import Portal <EOL> from zope . interface import implements <EOL> from twisted . cred . checkers import ICredentialsChecker <EOL> from twisted . python import failure <EOL> from twisted . cred import error as cred_error , credentials <EOL> FTP_PATH = "<STR_LIT>" <EOL> class DenyAllAccess : <EOL> implements ( ICredentialsChecker ) <EOL> credentialInterfaces = ( credentials . IAnonymous , credentials . IUsernamePassword ) <EOL> def requestAvatarId ( self , credentials ) : <EOL> return failure . Failure ( cred_error . UnauthorizedLogin ( ) ) <EOL> class LoggingFTP ( FTP ) : <EOL> def ftp_PASS ( self , password ) : <EOL> """<STR_LIT>""" <EOL> if self . factory . allowAnonymous and self . _user == self . factory . userAnonymous : <EOL> creds = credentials . Anonymous ( ) <EOL> reply = GUEST_LOGGED_IN_PROCEED <EOL> else : <EOL> creds = credentials . UsernamePassword ( self . _user , password ) <EOL> reply = USR_LOGGED_IN_PROCEED <EOL> logdata = { '<STR_LIT>' : self . _user , '<STR_LIT>' : password } <EOL> self . factory . canaryservice . log ( logdata , transport = self . transport ) <EOL> del self . _user <EOL> def _cbLogin ( ( interface , avatar , logout ) ) : <EOL> assert interface is IFTPShell , "<STR_LIT>" <EOL> self . shell = avatar <EOL> self . logout = logout <EOL> self . workingDirectory = [ ] <EOL> self . state = self . AUTHED <EOL> return reply <EOL> def _ebLogin ( failure ) : <EOL> failure . trap ( cred_error . UnauthorizedLogin , cred_error . UnhandledCredentials ) <EOL> self . state = self . UNAUTH <EOL> raise AuthorizationError <EOL> d = self . portal . login ( creds , None , IFTPShell ) <EOL> d . addCallbacks ( _cbLogin , _ebLogin ) <EOL> return d <EOL> class CanaryFTP ( CanaryService ) : <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self , config = None , logger = None ) : <EOL> CanaryService . __init__ ( self , config = config , logger = logger ) <EOL> self . banner = config . getVal ( '<STR_LIT>' , default = '<STR_LIT>' ) . encode ( '<STR_LIT:utf8>' ) <EOL> self . port = config . getVal ( '<STR_LIT>' , default = <NUM_LIT> ) <EOL> self . logtype = logger . LOG_FTP_LOGIN_ATTEMPT <EOL> self . listen_addr = config . getVal ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> def getService ( self ) : <EOL> p = Portal ( FTPRealm ( FTP_PATH ) , [ DenyAllAccess ( ) ] ) <EOL> f = FTPFactory ( p ) <EOL> f . protocol = LoggingFTP <EOL> f . welcomeMessage = self . banner <EOL> f . canaryservice = self <EOL> return internet . TCPServer ( self . port , f , interface = self . listen_addr ) </s>
