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)
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)
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)
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)
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)
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")
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)
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)
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))
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)
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)
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")
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
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)
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)
def check_syntx(self, req): if len(req["args"]) > 1: raise Res(501) if len(req["flags"]) > 0: raise Res(501)
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")
def check_quit_request(self, req, user): if len(req["args"]) >= 1 or len(req["flags"]) >= 1: raise Res(501, None, user.sid)
def help_service(self, req): help_msg = "\n" for routine in self.routines: help_msg += routine.help_str() return Res(214, help_msg)
def check_request(self, req, user): if not len(req["args"]) == 1: raise Res(501, None, user.sid)
def routine_handler(self, req, user): if req["routine"] in self.routines: return (self.routines[req["routine"]]).service(req, user) else: return Res(502)
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)
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)