def handler_timeout(self): self.tout.cancel() if self.pgdebug > 0: print("in handler_timeout %s" % self.name) #print( "handler_timeout log %d", self.pglog) if self.pglog > 0: pysyslog.syslog("Timeout on " + " " + str(self.par.client_address)) #print(dir(self)) #print(dir(self.par)) rrr = ["ERR", "Timeout occured, disconnecting."] #print( self.par.client_address, self.par.server.socket) try: #print("ekey", self.par.ekey) self.putencode(rrr, self.par.ekey) except: support.put_exception("putdata") # Force closing connection time.sleep(1) try: self.par.request.shutdown(socket.SHUT_RDWR) except: support.put_exception("on sending timeout shutdown")
def terminate(arg1, arg2): global mydata, server if mydata != {}: print("Dumping connection info:") print(mydata) try: server.socket.shutdown(socket.SHUT_RDWR) server.socket.close() server.shutdown() except: #print("Shutdown exception") pass if not quiet: print("Terminated pyvserv.py.") if conf.pglog > 0: pysyslog.syslog("Terminated Server") support.unlock_process(pyservsup.globals.lockfname) # Attempt to unhook all pending clients sys.exit(2)
def usersig(arg1, arg2): global mydata, server #print("usersig", arg1, arg2) if conf.pglog > 0: pysyslog.syslog("Got user signal %d" % arg1) print("Current clients:") print(mydata)
def get_fget_func(self, strx): dname = "" if len(strx) == 1: response = ["ERR", "Must specify file name."] self.resp.datahandler.putencode(response, self.resp.ekey) return dname = support.unescape(strx[1]) dname2 = self.resp.cwd + "/" + self.resp.dir + "/" + dname dname2 = support.dirclean(dname2) if not os.path.isfile(dname2): response = ["ERR", "File does not exist.", dname] self.resp.datahandler.putencode(response, self.resp.ekey) return flen = 0 try: flen = os.stat(dname2)[stat.ST_SIZE] fh = open(dname2, "rb") except: support.put_exception("fget") response = ["ERR", "Cannot open file.", dname] self.resp.datahandler.putencode(response, self.resp.ekey) return response = ["OK", str(flen)] self.resp.datahandler.putencode(response, self.resp.ekey) # Loop, break when file end or transmission error while 1: try: buff = fh.read(pyservsup.buffsize) blen = len(buff) except: print("Cannot read local file", sys.exc_info()) break try: ret = self.resp.datahandler.putencode([ str(blen), buff, ], self.resp.ekey, False) except: break if ret == 0: break if blen == 0: break # Lof and set state to IDLE xstr = "Sent file: '" + dname + \ "' " + str(flen) + " bytes" #print(xstr) pysyslog.syslog(xstr)
def finish(self): global mydata cli = str(mydata[self.name].client_address) usr = str(mydata[self.name].user) #print( "Logoff '" + usr + "'", cli) del mydata[self.name] if verbose: print("Closed socket on", self.name) if conf.pglog > 0: pysyslog.syslog("Logoff '" + usr + "' " + cli)
def get_data_func(self, strx): if self.resp.fname == "": response = ["ERR", "No filename for data"] self.resp.datahandler.putencode(response, self.resp.ekey) return try: self.resp.dlen = int(strx[1]) except: response = ["ERR", "Must specify file name"] self.resp.datahandler.putencode(response, self.resp.ekey) return try: fh = open(self.resp.fname, "wb") except: response = ["ERR", "Cannot save file on server"] self.resp.datahandler.putencode(response, self.resp.ekey) return self.resp.datahandler.putencode(["OK", "Send data"], self.resp.ekey) # Consume buffers until we got all mylen = 0 while mylen < self.resp.dlen: need = min(pyservsup.buffsize, self.resp.dlen - mylen) need = max(need, 0) data = self.resp.datahandler.handle_one(self.resp) if self.resp.ekey != "": data2 = bluepy.bluepy.decrypt(data, self.resp.ekey) else: data2 = data try: fh.write(data2) except: response = "ERR Cannot write data on server" self.resp.datahandler.putdata(response, self.resp.ekey, False) fh.close() return mylen += len(data) # Faulty transport, abort if len(data) == 0: break fh.close() if mylen != self.resp.dlen: response = "ERR faulty amount of data arrived" self.resp.datahandler.putdata(response, self.resp.ekey) return xstr = "Received file: '" + self.resp.fname + \ "' " + str(self.resp.dlen) + " bytes" print(xstr) pysyslog.syslog(xstr) self.resp.datahandler.putdata("OK Got data", self.resp.ekey)
def get_chpass_func(self, strx): ret = "" retval = True if len(strx) < 2: self.resp.datahandler.putdata("ERR must specify new_pass", self.resp.ekey) return True #print("chpass", strx[1], strx[2]) # Make sure there is a trace of the attempt stry = "chpass '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) #xret = pyservsup.passwd.auth(self.resp.user, strx[1], 0, pyservsup.USER_AUTH) #if xret[0] != 1: # self.resp.datahandler.putdata("ERR old pass must match", self.resp.ekey) # return True xret = pyservsup.passwd.auth(self.resp.user, strx[1], 0, pyservsup.USER_CHPASS) if xret[0] == 5: stry = "Pass changed '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) ret = "OK Pass changed" elif xret[0] == 3: stry = "No such user '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) ret = "ERR No such user" elif xret[0] == 1: stry = "Successful logon '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) ret = "OK " + self.resp.user + " Authenticated." retval = False else: stry = "Error on logon '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) ret = "ERR " + xret[1] self.resp.datahandler.putdata(ret, self.resp.ekey) return retval
def setup(self): global mydata cur_thread = threading.currentThread() self.name = cur_thread.getName() #print( "Logoff '" + usr + "'", cli) self.verbose = conf.verbose if self.verbose: print("Connection from ", self.a2, "as", self.name) #if pgdebug > 1: # put_debug("Connection from %s" % self.a2) self.statehandler = pystate.StateHandler(self) self.statehandler.verbose = conf.verbose self.statehandler.pglog = conf.pglog self.statehandler.pgdebug = conf.pgdebug self.datahandler = pydata.DataHandler() self.datahandler.pgdebug = conf.pgdebug self.datahandler.pglog = conf.pglog self.datahandler.verbose = conf.verbose self.datahandler.par = self mydata[self.name] = self #if conf.verbose: # print("Connected " + " " + str(self.client_address)) if conf.pglog > 0: pysyslog.syslog("Connected " + " " + str(self.client_address)) response = ["OK", "pyvserv %s ready" % version] # Connected, acknowledge self.datahandler.putencode(response, "")
def get_pass_func(self, strx): ret = "" retval = True # Make sure there is a trace of the attempt stry = "Logon '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) ret = pyservsup.passwd.perms(self.resp.user) if int(ret[2]) & pyservsup.PERM_DIS: rrr = ["ERR", "this user is temporarily disabled"] self.resp.datahandler.putencode(rrr, self.resp.ekey) return retval xret = pyservsup.passwd.auth(self.resp.user, strx[1], 0, pyservsup.USER_AUTH) rrr = [] if xret[0] == 3: stry = "No such user '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) rrr = ["ERR", "No such user"] elif xret[0] == 1: stry = "Successful logon '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) rrr = ["OK", self.resp.user + " Authenticated."] retval = False else: stry = "Error on logon '" + self.resp.user + "' " + \ str(self.resp.client_address) pysyslog.syslog(stry) rrr = ["ERR", xret[1]] self.resp.datahandler.putencode(rrr, self.resp.ekey) return retval
pysfunc.pgdebug = conf.pgdebug pysfunc.pglog = conf.pglog if conf.pglog > 0: pysyslog.openlog("pyvserv.py") # Port 0 would mean to select an arbitrary unused port HOST, PORT = "", 9999 try: server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler) except: print("Cannot start server. ", sys.exc_info()[1]) if conf.pglog > 0: pysyslog.syslog("Cannot start server " + str(sys.exc_info()[1])) #print("Try again later.") terminate(None, None) #sys.exit(1) server.allow_reuse_address = True ip, port = server.server_address server.allow_reuse_address = True server.verbose = verbose # Start a thread with the server -- that thread will then start one # or more threads for each request server_thread = threading.Thread(target=server.serve_forever) # Exit the server thread when the main thread terminates