# Path: beeswarm/drones/honeypot/capabilities/handlerbase.py
# class HandlerBase(object):
#     def __init__(self, options, workdir):
#         """
#         Base class that all capabilities must inherit from.
# 
#         :param sessions: a dictionary of Session objects.
#         :param options: a dictionary of configuration options.
#         :param workdir: the directory which contains files for this
#                         particular instance of Beeswarm
#         """
#         self.options = options
#         self.sessions = {}
#         if 'users' in options:
#             self.users = options['users']
#         else:
#             self.users = {}
#         # virtual file system shared by all capabilities
#         self.vfsystem = OSFS(os.path.join(workdir, 'data/vfs'))
#         # service port
#         self.port = int(options['port'])
# 
#     def create_session(self, address):
#         protocol = self.__class__.__name__.lower()
#         session = Session(address[0], address[1], protocol, self.users)
#         self.sessions[session.id] = session
#         session.destination_port = self.port
#         logger.debug(
#             'Accepted {0} session on port {1} from {2}:{3}. ({4})'.format(protocol, self.port, address[0],
#                                                                           address[1], str(session.id)))
#         logger.debug('Size of session list for {0}: {1}'.format(protocol, len(self.sessions)))
#         return session
# 
#     def close_session(self, session):
#         logger.debug('Closing sessions')
#         session.end_session()
#         if session.id in self.sessions:
#             del self.sessions[session.id]
#         else:
#             assert False
# 
#     def handle_session(self, socket, address):
#         raise Exception('Do no call base class!')
# 
# Path: beeswarm/drones/honeypot/helpers/common.py
# def send_whole_file(sock_fd, file_fd):
#     offset = 0
#     while True:
#         sent = sendfile(sock_fd, file_fd, offset, 65536)
#         if sent == 0:
#             break
#         offset += sent
# 
# def path_to_ls(fn):
#     """ Converts an absolute path to an entry resembling the output of
#         the ls command on most UNIX systems."""
#     st = os.stat(fn)
#     full_mode = 'rwxrwxrwx'
#     mode = ''
#     file_time = ''
#     d = ''
#     for i in range(9):
#         # Incrementally builds up the 9 character string, using characters from the
#         # fullmode (defined above) and mode bits from the stat() system call.
#         mode += ((st.st_mode >> (8 - i)) & 1) and full_mode[i] or '-'
#         d = (os.path.isdir(fn)) and 'd' or '-'
#         file_time = time.strftime(' %b %d %H:%M ', time.gmtime(st.st_mtime))
#     list_format = '{0}{1} 1 ftp ftp {2}\t{3}{4}'.format(d, mode, str(st.st_size), file_time, os.path.basename(fn))
#     return list_format


import logging
import os
from gevent import socket
from fs.path import dirname
from beeswarm.drones.honeypot.capabilities.handlerbase import HandlerBase
from beeswarm.drones.honeypot.helpers.common import send_whole_file, path_to_ls
            self.respond('550 The system cannot find the file specified.')

    def do_TYPE(self, arg):
        self.transfer_mode = arg
        self.respond('200 Transfer type set to:' + self.transfer_mode)

    def getcmd(self):
        return self.conn.recv(512)

    def start_data_conn(self):
        if self.mode == 'PASV':
            self.client_sock, (self.cli_ip, self.cli_port) = self.serv_sock.accept()
        else:
            self.client_sock = socket.socket()
            self.client_sock.connect((self.cli_ip, self.cli_port))

    def stop_data_conn(self):
        self.client_sock.close()
        if self.mode == 'PASV':
            self.serv_sock.close()

    def respond(self, msg):
        msg += TERMINATOR
        self.session.transcript_outgoing(msg)
        self.conn.send(msg)

    def stop(self):
        self.session.end_session()



# Path: beeswarm/drones/honeypot/capabilities/handlerbase.py
# class HandlerBase(object):
#     def __init__(self, options, workdir):
#         """
#         Base class that all capabilities must inherit from.
# 
#         :param sessions: a dictionary of Session objects.
#         :param options: a dictionary of configuration options.
#         :param workdir: the directory which contains files for this
#                         particular instance of Beeswarm
#         """
#         self.options = options
#         self.sessions = {}
#         if 'users' in options:
#             self.users = options['users']
#         else:
#             self.users = {}
#         # virtual file system shared by all capabilities
#         self.vfsystem = OSFS(os.path.join(workdir, 'data/vfs'))
#         # service port
#         self.port = int(options['port'])
# 
#     def create_session(self, address):
#         protocol = self.__class__.__name__.lower()
#         session = Session(address[0], address[1], protocol, self.users)
#         self.sessions[session.id] = session
#         session.destination_port = self.port
#         logger.debug(
#             'Accepted {0} session on port {1} from {2}:{3}. ({4})'.format(protocol, self.port, address[0],
#                                                                           address[1], str(session.id)))
#         logger.debug('Size of session list for {0}: {1}'.format(protocol, len(self.sessions)))
#         return session
# 
#     def close_session(self, session):
#         logger.debug('Closing sessions')
#         session.end_session()
#         if session.id in self.sessions:
#             del self.sessions[session.id]
#         else:
#             assert False
# 
#     def handle_session(self, socket, address):
#         raise Exception('Do no call base class!')
# 
# Path: beeswarm/drones/honeypot/helpers/common.py
# def send_whole_file(sock_fd, file_fd):
#     offset = 0
#     while True:
#         sent = sendfile(sock_fd, file_fd, offset, 65536)
#         if sent == 0:
#             break
#         offset += sent
# 
# def path_to_ls(fn):
#     """ Converts an absolute path to an entry resembling the output of
#         the ls command on most UNIX systems."""
#     st = os.stat(fn)
#     full_mode = 'rwxrwxrwx'
#     mode = ''
#     file_time = ''
#     d = ''
#     for i in range(9):
#         # Incrementally builds up the 9 character string, using characters from the
#         # fullmode (defined above) and mode bits from the stat() system call.
#         mode += ((st.st_mode >> (8 - i)) & 1) and full_mode[i] or '-'
#         d = (os.path.isdir(fn)) and 'd' or '-'
#         file_time = time.strftime(' %b %d %H:%M ', time.gmtime(st.st_mtime))
#     list_format = '{0}{1} 1 ftp ftp {2}\t{3}{4}'.format(d, mode, str(st.st_size), file_time, os.path.basename(fn))
#     return list_format


# Path: beeswarm/drones/honeypot/capabilities/ftp.py
import logging
import os
from gevent import socket
from fs.path import dirname
from beeswarm.drones.honeypot.capabilities.handlerbase import HandlerBase
from beeswarm.drones.honeypot.helpers.common import send_whole_file, path_to_ls

            self.respond('550 The system cannot find the file specified.')

    def do_TYPE(self, arg):
        self.transfer_mode = arg
        self.respond('200 Transfer type set to:' + self.transfer_mode)

    def getcmd(self):
        return self.conn.recv(512)

    def start_data_conn(self):
        if self.mode == 'PASV':
            self.client_sock, (self.cli_ip, self.cli_port) = self.serv_sock.accept()
        else:
            self.client_sock = socket.socket()
            self.client_sock.connect((self.cli_ip, self.cli_port))

    def stop_data_conn(self):
        self.client_sock.close()
        if self.mode == 'PASV':
            self.serv_sock.close()

    def respond(self, msg):
        msg += TERMINATOR
        self.session.transcript_outgoing(msg)
        self.conn.send(msg)

    def stop(self):
        self.session.end_session()



class ftp(HandlerBase):