diff --git a/fakeftp.py b/fakeftp.py index 5c21057..f709969 100644 --- a/fakeftp.py +++ b/fakeftp.py @@ -136,6 +136,9 @@ class Directory(FSNode): return f return None + def add_file(self,f): + self.files.append(f) + class File(FSNode): def __init__(self,name,owner="nobody",group="nobody",rights=384,size=4096, content=None): FSNode.__init__(self, name=name, owner=owner, group=group,rights=rights, size=size, iun=1) @@ -259,7 +262,7 @@ class QuitMessage(FTPMessage): class CommandNotImplementedMessage(FTPMessage): ''' Message sent to client when command not implemented''' def __init__(self): - FTPMessage.__init__(self,"501","Command not implemented") + FTPMessage.__init__(self,"502","Command not implemented") class InvalidCommandMessage(FTPMessage): '''Message sent to client when server received message it didn't understand''' @@ -286,6 +289,11 @@ class WelcomeMessage(FTPMessage): def __init__(self): FTPMessage.__init__(self,"220","FTP Server ready") +class FileActionCompletedMessage(FTPMessage): + '''Message sent to client after file action has been completed''' + def __init__(self): + FTPMessage.__init__(self,"250","Requested file action okay, completed.") + class EnteringPassiveModeMessage(FTPMessage): def __init__(self,(ip,port)): addr = ip.split(".") @@ -403,6 +411,17 @@ class RetrieveHandler: self.node.send_content(socket) self.handler.send( EndFileRetrieve() ) +class VoidStoreHandler: + def __init__(self, handler): + self.handler = handler + + def handle(self, socket): + data = socket.recv(1024) + while len(data) != 0: + data = socket.recv(1024) + + self.handler.send( FileActionCompletedMessage() ) + # CommandHandler classes class CommandHandler: cmd = None @@ -494,9 +513,17 @@ class RenameToCommandHandler(ForbiddenCommandHandler): cmd = "RNTO" log = "Attempt to rename to %s" -class StoreCommandHandler(ForbiddenCommandHandler): +class StoreCommandHandler(CommandHandler): cmd = "STOR" - log = "Attempt to store file %s" + def process(self, handler, message): + node = handler.get_node(message.parameter) + if node is None or node.is_file(): + if handler.runDataConnection( VoidStoreHandler() ): + handler.log( "Writing file %s (voided)" ) + else: + handler.send( UnableToBuildConnectionMessage() ) + else: + handler.send( OperationNotPermittedMessage() ) class RetrieveCommandHandler(CommandHandler): cmd = "RETR"