Beispiel #1
0
    def mkd_service(self, req, user):
        mypath = os.path.join(self.base, user.dir)
        if len(req["args"]) == 0:
            return Res(501)

        # #check if address is local
        # if req["args"][0][0] == '/':
        #     dirpath = os.path.join(self.base, req["args"][0][1:])
        # else:
        #     dirpath = os.path.join(mypath, req["args"][0])
        dirpath = Path().join(self.base, req["args"][0], user.dir)

        if req["flags"] == ['-i']:
            # make file
            try:
                os.mknod(dirpath)
                print("File ", dirpath, " Created ")
            except FileExistsError:
                print("File ", dirpath, " already exists")
                return Res(500, msg="File already exists")
        else:
            #make dir
            try:
                os.mkdir(dirpath)
                print("Directory ", dirpath, " Created ")
            except FileExistsError:
                print("Directory ", dirpath, " already exists")
                return Res(500, msg="Dir already exists")

        # print("This dir contains:",[f for f in listdir(mypath)])
        return Res(257, dirpath, user.sid)
Beispiel #2
0
    def cwd_service(self, req, user):
        last_dir = user.dir
        if len(req["args"]) == 0:
            user.dir = "."
            return Res(212, msg="Successful change.", sid=user.sid)
        
        # #check if address is local 
        # if req["args"][0][0] == '/':
        #     norm_path = os.path.join(".", req["args"][0][1:])
        # else:
        #     norm_path = os.path.join(user.dir, req["args"][0])
        # # Normalizes the path -> ../..
        # norm_path = os.path.normpath(norm_path)
        dirpath = Path().join(self.base, req["args"][0], user.dir) #final destination
        norm_path = Path().join_user_dir(req["args"][0], user.dir)
        user.dir = norm_path

        mypath = os.path.join(self.base, user.dir)
        # check if it is a valid dir
        if os.path.exists(mypath) == 0:
            user.dir = last_dir
            return Res(214, msg="Dir doesnt exist")
        # elif user.dir[:2] == "..":
        #     user.dir = "."
        #     mypath = os.path.join(self.base, user.dir)
        #     return Res(214)
        elif os.path.isdir(mypath) == 0:
            user.dir = last_dir
            return Res(214, msg="is not a Dir")
            
        # print("This dir contains:",[f for f in listdir(mypath)])
        return Res(212, msg="Successful change.", sid=user.sid)
Beispiel #3
0
    def check_not_logged_in(self, req, user):
        if (user.username is not None) and (user.password is not None):
            raise Res(536, "Already logged in", user.sid)

        if "sid" not in req:
            return
        if req["sid"] in self.sids_:
            raise Res(536, "Already logged in", user.sid)
Beispiel #4
0
 def user_service(self, req, user):
     self.check_request(req, user)
     self.check_not_logged_in(req, user)
     if user.username is not None:
         return Res(536)
     if req["args"][0] in self.__dict__:
         user.username = req["args"][0]
         return Res(331)
     else:
         return Res(430)
Beispiel #5
0
 def pass_service(self, req, user):
     self.check_request(req, user)
     self.check_not_logged_in(req, user)
     if user.username is None:
         return Res(503)
     if user.password is not None:
         return Res(536, "Already logged in", user.sid)
     if req["args"][0] == self.__dict__[user.username]:
         user.password = req["args"][0]
         self.assign_sid(user)
         return Res(230, None, user.sid)
     else:
         return Res(430)
Beispiel #6
0
 def load_file(self):
     try:
         with open(self.path) as file:
             self.str = file.read()
             self.name = os.path.basename(file.name)
     except:
         raise Res(500, "invalid file name")
Beispiel #7
0
    def pwd_service(self, req, user):
        mypath = os.path.join(self.base, user.dir)
        if os.path.exists(mypath) == 0:
            user.dir = '.'

        my_dir = user.dir
        return Res(214, my_dir)
Beispiel #8
0
 def rcv_file(self, req, res):
     msg = self.s_data_sock.recv(res["file"])
     file = Res(msg)
     # check stuff
     self.s_data_sock.send("ACK".encode())
     self.s_data_sock.recv(1024)
     self.file_service(req, file)
Beispiel #9
0
    def list_service(self, req, user):
        mypath = os.path.join(self.base, user.dir)

        if os.path.exists(mypath) == 0:
            user.dir = '.'
            return Res(214, msg="Dir doesnt exist")

        # mypath = self.base
        file_and_dirs = listdir(mypath)
        onlyfiles = [f for f in file_and_dirs if isfile(join(mypath, f))]
        names = ""
        for f in file_and_dirs:
            names += f
            names += "\n"
        # return Res(226)
        return Res(226, file=File(str=names))
Beispiel #10
0
 def __call__(self, user, file):
     if not self.enable: return
     if not self.has_user(user): return
     info = self.get_user_info(user)
     if self.can_pay(info, file):
         self.pay(info, file)
         self.update_info()
         self.check_th(info)
     else: raise Res(425)
Beispiel #11
0
    def ath_check(self, req):
        if req["sid"] is not None:
            self.routines["USER"].check(req)

        elif (req["routine"] == "USER") or (req["routine"]
                                            == "PASS") or (req["routine"]
                                                           == "HELP"):
            pass

        else:
            raise Res(530)
Beispiel #12
0
    def __init__(self, c_cmnd_sock, c_data_sock, dir = "."):
        self.username = None
        self.password = None
        self.sid = None

        self.cmnd_sock = c_cmnd_sock
        self.data_sock = c_data_sock
        self.ip = c_cmnd_sock.getsockname()[0]
        self.port = c_cmnd_sock.getsockname()[1]
        # each thread needs emulated working directory
        self.dir = dir
        self.end_res = Res(230, "Logged out")
Beispiel #13
0
    def service(self, req):
        self.s_cmnd_sock.send(req.__repr__())
        msg = self.s_cmnd_sock.recv(1024)
        res = Res(msg)
        # check stuff
        self.s_cmnd_sock.send("ACK".encode())
        self.s_cmnd_sock.recv(1024)

        self.update_sid(res)
        print(res)

        if "file" in res:
            self.rcv_file(req, res)

        if res["code"] == 221:
            return 1
        return 0
Beispiel #14
0
    def rmd_service(self, req, user):
        mypath = os.path.join(self.base, user.dir)
        if len(req["args"]) == 0:
            return Res(501)

        # #check if address is local
        # if req["args"][0][0] == '/':
        #     dirpath = os.path.join(self.base, req["args"][0][1:])
        # else:
        #     dirpath = os.path.join(mypath, req["args"][0])
        dirpath = Path().join(self.base, req["args"][0], user.dir)

        if req["flags"] == ['-f']:
            # rmv dir
            try:
                if os.path.isdir(dirpath):
                    if not os.listdir(dirpath):
                        os.rmdir(dirpath)
                        print("Dir ", dirpath, " Deleted ")
                    else:
                        print(dirpath, " is not a empty")
                        return Res(500, msg="is not a empty")
                else:
                    print(dirpath, " is not a directory")
                    return Res(500, msg="is not a directory")

            except FileNotFoundError:
                print("Dir ", dirpath, " Dir doesnt exist or is not empty")
                return Res(500, msg="Dir doesnt exist or is not empty")
        else:
            #rmv file
            try:
                if os.path.isfile(dirpath):
                    os.remove(dirpath)
                    print("Dir ", dirpath, " Deleted ")
                else:
                    print(dirpath, " is not a file")
                    return Res(500, msg="is not a file")

            except FileExistsError:
                print("Dir ", dirpath, " doesnt exist")
                return Res(500, msg="File doesnt exist")

        # print("This dir contains:",[f for f in listdir(mypath)])
        return Res(250, dirpath, user.sid)
Beispiel #15
0
 def dl_service(self, req, user):
     path = self.get_file_path(req, user)
     file = File(path)
     self.check_athor(req, user)
     self.check_accounting(user, file)
     return Res(226, file=file)
Beispiel #16
0
 def check_syntx(self, req):
     if len(req["args"]) > 1: raise Res(501)
     if len(req["flags"]) > 0: raise Res(501)
Beispiel #17
0
 def if_exceed_base(self, base, dst):
     dst = os.path.normpath(dst)
     base = os.path.normpath(base)
     if len(dst) < len(base):
         raise Res(500, "invalid path")
Beispiel #18
0
 def check_quit_request(self, req, user):
     if len(req["args"]) >= 1 or len(req["flags"]) >= 1:
         raise Res(501, None, user.sid)
Beispiel #19
0
 def help_service(self, req):
     help_msg = "\n"
     for routine in self.routines:
         help_msg += routine.help_str()
     return Res(214, help_msg)
Beispiel #20
0
 def check_request(self, req, user):
     if not len(req["args"]) == 1:
         raise Res(501, None, user.sid)
Beispiel #21
0
 def routine_handler(self, req, user):
     if req["routine"] in self.routines:
         return (self.routines[req["routine"]]).service(req, user)
     else:
         return Res(502)
Beispiel #22
0
 def check(self, req):
     if not req["sid"] in self.sids_:
         raise Res(535)
 def __call__(self, path, user):
     if not self.enable: return
     if self.is_path_protected(path) and not self.is_user_athorized(user):
         raise Res(550)
Beispiel #24
0
 def quit_service(self, req, user):
     # check logged in
     self.check_quit_request(req, user)
     self.check(req)
     self.sids_.remove(user.sid)
     return Res(221)