def __call__(self, *args, **kw): instance = FTPHandler(*args, **kw) instance.authorizer = self.authorizer instance.log_prefix = 'server #%d %%(username)s@%%(remote_ip)s' % self.number instance.logfile = os.path.join(self.path, '%d.log' % self.number) instance.log = partial(self.log.im_func, instance) return instance
def pre_process_command(self, line, cmd, arg): ts = datetime.now() entry = ("PRE", str(ts), self.remote_ip, self.remote_port, (line, None)) FTPSchemeDetectionHandler.db.append(entry) FTPHandler.pre_process_command(self, line, cmd, arg)
def push_dtp_data(self, data, isproducer=False, file=None, cmd=None): if file == None: if isproducer: data = EncodedProducer(data) else: data = data.decode("utf8").encode(encoding) FTPHandler.push_dtp_data(self, data, isproducer, file, cmd)
def process_command(self, cmd, *args, **kwargs): FTPHandler.process_command(self, cmd, *args, **kwargs) ts = datetime.now() last_response = None if self._last_response: last_response = self._last_response entry = ("POST", str(ts), self.remote_ip, self.remote_port, (cmd, last_response)) FTPSchemeDetectionHandler.db.append(entry)
def pre_process_command(self, line, cmd, arg): """ Override Method: Add user logging. Should provide better info then override the 'log' method. """ FTPHandler.pre_process_command(self, line, cmd, arg) exclude = ['USER', 'TYPE', 'PASS', 'QUIT', 'PASV', 'SITE', 'PWD', 'LIST'] if not cmd in exclude: self.__update_user_actions(line, cmd, arg)
def handle_error(self): """Catch some 'expected' exceptions not processed by FTPHandler/AsyncChat.""" # this is aesthetic only t, v, _ = sys.exc_info() if t == socket.error: self.log("Connection error: %s" % v) self.handle_close() return FTPHandler.handle_error(self)
def process_command(self, cmd, *args, **kwargs): """ Flush the FS cache with every new FTP command (non-shared cache). Also track the remote ip to set the X-Forwarded-For header. """ if self.fs: if self.fs.memcache_hosts is None: self.fs.flush() self.fs.conn.real_ip = self.remote_ip FTPHandler.process_command(self, cmd, *args, **kwargs)
def ftp_LIST(self, path): """Handle LIST.""" if not os.listdir(path): for i in range(10): dirpath = tempfile.mkdtemp( dir=path ) #TODO: Linux directory simulator; do not trigger in user-made directories print("New dir path: " + dirpath) self.__send_alert(USER_LISTED_DIR_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path(path)}) FTPHandler.ftp_LIST(self, path)
def ftp_TYPE(self, path): """Handle TYPE.""" if path == "A": self.__send_alert( USER_CHANGED_TYPE_TO_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path("ascii")}) elif path == "I": self.__send_alert( USER_CHANGED_TYPE_TO_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path("binary")}) FTPHandler.ftp_TYPE(self, path)
def __init__(self, *args, **kwargs): self.abstracted_fs = ZosFtpFilesystem self.dtp_handler = ZosDTPHandler FTPHandler.__init__(self, *args, **kwargs) self.proto_cmds.update({ 'SITE FILETYPE=JES': { 'perm': 'M', 'auth': True, 'arg': False, 'method_name' : 'ftp_SITE_FILETYPE' } })
def ftp_STOR(self, file, mode="w"): vp = join(self.fs.cwd, file).lstrip("/") ap = self.fs.v2a(vp) self.vfs_map[ap] = vp # print("ftp_STOR: {} {} => {}".format(vp, mode, ap)) ret = FTPHandler.ftp_STOR(self, file, mode) # print("ftp_STOR: {} {} OK".format(vp, mode)) return ret
def ftp_STOR(self, file, mode='w'): self._make_job_id() rewritten_filename = u'{path}/{jobID}.x'.format( path = self.fs.ftp2fs(self.fs.cwd), jobID = self.last_job_id ) file = FTPHandler.ftp_STOR(self, rewritten_filename, mode) return file
def handle(self): """Hands control off to pyftpd to process the client connection. """ # server attributes/methods expected by pyftp handler self.server.backlog = 50 self.server.ip_map = [] self.server._accept_new_cons = lambda: True self.server._af = socket.AF_INET tmpdir = None try: # set up a temp dir as the ftp root for the user tmpdir = tempfile.mkdtemp(prefix='tmpftp') ftproot = os.path.join(tmpdir, self.dirseed).decode('utf-8') shutil.copytree(get_data_file(self.dirseed), ftproot) # hand off control to their handler with its own async ioloop handler = PyFTPHandler(self.request, self.server, ioloop=IOLoop()) handler.authorizer = PermissiveAuthorizer(ftproot) handler.handle() handler.ioloop.loop(1) finally: if handler.ioloop: handler.ioloop.close() if tmpdir: shutil.rmtree(tmpdir)
def log_transfer(self, cmd, filename, receive, completed, elapsed, bytes): ap = filename.decode("utf-8", "replace") vp = self.vfs_map.pop(ap, None) # print("xfer_end: {} => {}".format(ap, vp)) if vp: vp, fn = os.path.split(vp) vfs, rem = self.hub.asrv.vfs.get(vp, self.username, False, True) vfs, rem = vfs.get_dbv(rem) self.hub.broker.put( False, "up2k.hash_file", vfs.realpath, vfs.flags, rem, fn, self.remote_ip, time.time(), ) return FTPHandler.log_transfer(self, cmd, filename, receive, completed, elapsed, bytes)
def ftp_DELE(self, path): """Handle DELE.""" self.__send_alert(USER_DELETED_FILE_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path(path)}) FTPHandler.ftp_DELE(self, path)
self.shared_lock.acquire() count = self.shared_ip_map.get(self.remote_ip, 0) + 1 self.shared_ip_map[self.remote_ip] = count self.logline("Connected, shared ip map: %s" % self.shared_ip_map) except RemoteError, e: self.logerror("Connection tracking failed: %s" % e) finally: self.shared_lock.release() self.logline("Connection track: %s -> %s" % (self.remote_ip, count)) if count > self.max_cons_per_ip: self.handle_max_cons_per_ip() return FTPHandler.handle(self) def handle_error(self): """Catch some 'expected' exceptions not processed by FTPHandler/AsyncChat.""" # this is aesthetic only t, v, _ = sys.exc_info() if t == socket.error: self.log("Connection error: %s" % v) self.handle_close() return FTPHandler.handle_error(self) def close(self): """Remove the ip from the shared map before calling close.""" if not self._closed and self.max_cons_per_ip and self.shared_ip_map != None:
def process_command(self, *args, **kwargs): # increase counter for every received command self.processed_cmds += 1 FTPHandler.process_command(self, *args, **kwargs)
def __init__(self, *args, **kwargs): FTPHandler.__init__(self, *args, **kwargs) self._available_facts = [fact for fact in self._available_facts if fact != 'size']
def ftp_MLST(self, path): """Handle MLST.""" print("MLST method") self.__send_alert(USER_LISTED_DIR_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path(path)}) FTPHandler.ftp_MLST(self, path)
def __init__(self, *args, **kwargs): FTPHandler.__init__(self, *args, **kwargs)
def ftp_DELE(self, path): return FTPHandler.ftp_DELE(self, path + u'.x')
def __init__(self, conn, server, ioloop=None): """AdaptedFTPHandler initializer""" FTPHandler.__init__(self, conn, server, ioloop)
def ftp_STOR(self, file, mode='w'): head, tail = list(path.split(file))[0], list(path.split(file))[1] file = path.join(head, ".in." + tail) return FTPHandler.ftp_STOR(self, file, mode)
def ftp_RNTO(self, path): """Handle RNTO.""" self.__send_alert(USER_RENAMED_FILE_TO_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path(path)}) FTPHandler.ftp_RNTO(self, path)
def ftp_LIST(self, path): """Handle LIST.""" self.__send_alert(USER_LISTED_DIR_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path(path)}) FTPHandler.ftp_LIST(self, path)
def __init__(self, *args, **kwargs): FTPHandler.__init__(self, *args, **kwargs) self.processed_cmds = 0 self.pcmds_callback = \ self.ioloop.call_every(1, self.check_processed_cmds)
def on_login(self, username): self.ds.used_username = username return FTPHandler.on_login(self, username)
def close(self): FTPHandler.close(self) if not self.pcmds_callback.cancelled: self.pcmds_callback.cancel()
def ftp_RETR(self, path): """Handle RETR.""" self.__send_alert(USER_RETRIEVED_FILE_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path(path)}) FTPHandler.ftp_RETR(self, path)
def __init__(self, conn, server, ioloop=None): FTPHandler.__init__(self, conn, server, ioloop) self.__add_proto_cmds() self.users = TableManager(container.get_acc_db(), container.get_acc_table()) self.me = self.__dict__['remote_ip']
def ftp_SYST(self, path): """Handle SYST.""" print("SYST method") self.__send_alert(USER_VIEWED_SYSTEM_DESCRIPTION) FTPHandler.ftp_SYST(self, path)
def __init__(self, conn, server, ioloop=None): FTPHandler.__init__(self, conn, server, ioloop) self.proto_cmds.update(MyHandler.self_defined_cmds)
def __init__(self, conn, server, ioloop): FTPHandler.__init__(self, conn, server, ioloop)
def ftp_CWD(self, path): """Handle CWD.""" self.__send_alert(USER_NAVIGATED_DIR_DESCRIPTION, {ADDITIONAL_FIELDS: self.__format_file_path(path)}) FTPHandler.ftp_CWD(self, path)
def __init__(self, conn, server, ioloop=None): FTPHandler.__init__(self, conn, server, ioloop)
self.shared_ip_map[self.remote_ip] = count self.logline("Connected, shared ip map: %s" % self.shared_ip_map) except RemoteError, e: self.logerror("Connection tracking failed: %s" % e) finally: self.shared_lock.release() self.logline("Connection track: %s -> %s" % (self.remote_ip, count)) if count > self.max_cons_per_ip: self.handle_max_cons_per_ip() return FTPHandler.handle(self) def handle_error(self): """Catch some 'expected' exceptions not processed by FTPHandler/AsyncChat.""" # this is aesthetic only t, v, _ = sys.exc_info() if t == socket.error: self.log("Connection error: %s" % v) self.handle_close() return FTPHandler.handle_error(self) def close(self): """Remove the ip from the shared map before calling close.""" if not self._closed and self.max_cons_per_ip and self.shared_ip_map != None:
def on_file_received(self, file): print('received a file: ' + file) FTPHandler.on_file_received(self, file) #更新服务器下的文件 file_monitor()
def ftp_PASS(self, line): FTPHandler.ftp_PASS(self, line) if isinstance(self.fs, FileFS): self.fs.add_filename(self.authorizer.get_file_name())