def authentication_ok(self): """Checks the authentication and sets the username of the currently connected terminal. Returns True or False""" username = None password = None if self.authCallback: if self.authNeedUser: username = self.readline(prompt=str_to_bytes(self.PROMPT_USER), use_history=False) if self.authNeedPass: password = self.readline(echo=False, prompt=str_to_bytes(self.PROMPT_PASS), use_history=False) if self.DOECHO: self.write(b"\n") try: self.authCallback(username, password) except: self.username = None return False else: # Successful authentication self.username = username return True else: # No authentication desired self.username = None return True
def handle(self): """The actual service to which the user has connected.""" if self.TELNET_ISSUE: self.writeline(self.TELNET_ISSUE) if not self.authentication_ok(): return if self.DOECHO: self.writeline(self.WELCOME) self.session_start() while self.RUNSHELL: raw_input = self.readline(prompt=str_to_bytes(self.PROMPT), echo=True) raw_input = raw_input.strip() self.input = self.input_reader(self, raw_input) self.raw_input = self.input.raw if self.input.cmd: cmd = bytes_to_str(self.input.cmd.upper()) params = [bytes_to_str(i) for i in self.input.params] if cmd in self.COMMANDS: try: self.COMMANDS[cmd](params) except: log.exception('Error calling %s.' % cmd) (t, p, tb) = sys.exc_info() if self.handleException(t, p, tb): break else: self.writeerror("Unknown command '%s'" % cmd) log.debug("Exiting handler")
def writecooked(self, text): """Put data directly into the output queue (bypass output cooker)""" self.sock.sendall(str_to_bytes(text))
def write(self, text): """Send a packet to the socket. This function cooks output.""" text = str_to_bytes(text) # eliminate any unicode or other snigglets text = text.replace(IAC, IAC + IAC) text = text.replace(chr_py3(10), chr_py3(13) + chr_py3(10)) self.writecooked(text)
def writemessage(self, text): """Write out an asynchr_py3onous message, then reconstruct the prompt and entered text.""" log.debug('writing message %r', text) self.write(chr_py3(10) + str_to_bytes(text) + chr_py3(10)) self.write(self._current_prompt + b''.join([chr_py3(i) for i in self._current_line]))
def writeline(self, text): """Send a packet with line ending.""" text = str_to_bytes(text) if isinstance(text, str) else text log.debug('writing line %r' % text) self.write(text + chr_py3(10))
def readline(self, echo=None, prompt='', use_history=True): """Return a line of text, including the terminating LF If echo is true always echo, if echo is false never echo If echo is None follow the negotiated setting. prompt is the current prompt to write (and rewrite if needed) use_history controls if this current line uses (and adds to) the command history. """ line = [] insptr = 0 ansi = 0 histptr = len(self.history) prompt = str_to_bytes(prompt) if isinstance(prompt, str) else prompt if self.DOECHO: self.write(prompt) self._current_prompt = prompt else: self._current_prompt = b'' self._current_line = b'' while True: c = self.getc(block=True) c = self.ansi_to_curses(c) if c == theNULL: continue elif c == curses.KEY_LEFT: if insptr > 0: insptr = insptr - 1 self._readline_echo(self.CODES['CSRLEFT'], echo) else: self._readline_echo(BELL, echo) continue elif c == curses.KEY_RIGHT: if insptr < len(line): insptr = insptr + 1 self._readline_echo(self.CODES['CSRRIGHT'], echo) else: self._readline_echo(BELL, echo) continue elif c == curses.KEY_UP or c == curses.KEY_DOWN: if not use_history: self._readline_echo(BELL, echo) continue if c == curses.KEY_UP: if histptr > 0: histptr = histptr - 1 else: self._readline_echo(BELL, echo) continue elif c == curses.KEY_DOWN: if histptr < len(self.history): histptr = histptr + 1 else: self._readline_echo(BELL, echo) continue line = [] if histptr < len(self.history): line.extend(self.history[histptr]) for char in range(insptr): self._readline_echo(self.CODES['CSRLEFT'], echo) self._readline_echo(self.CODES['DEOL'], echo) self._readline_echo(b''.join([chr_py3(i) for i in line]), echo) insptr = len(line) continue elif c == chr_py3(3): self._readline_echo( b'\n' + str_to_bytes(curses.ascii.unctrl(ord(c))) + b' ABORT\n', echo) return b'' elif c == chr_py3(4): if len(line) > 0: self._readline_echo( b'\n' + str_to_bytes(curses.ascii.unctrl(ord(c))) + b' ABORT (QUIT)\n', echo) return b'' self._readline_echo( b'\n' + str_to_bytes(curses.ascii.unctrl(ord(c))) + b' QUIT\n', echo) return b'QUIT' elif c == chr_py3(10): self._readline_echo(c, echo) result = b''.join([chr_py3(i) for i in line]) if use_history: self.history.append(result) if echo is False: if prompt: self.write(chr_py3(10)) log.debug('readline: %s(hidden text)', bytes_to_str(prompt)) else: log.debug('readline: %s%r', bytes_to_str(prompt), bytes_to_str(result)) return result elif c == curses.KEY_BACKSPACE or c == chr_py3( 127) or c == chr_py3(8): if insptr > 0: self._readline_echo( self.CODES['CSRLEFT'] + self.CODES['DEL'], echo) insptr = insptr - 1 del line[insptr] else: self._readline_echo(BELL, echo) continue elif c == curses.KEY_DC: if insptr < len(line): self._readline_echo(self.CODES['DEL'], echo) del line[insptr] else: self._readline_echo(BELL, echo) continue else: if ord(c) < 32: c = curses.ascii.unctrl(ord(c)) if len(line) > insptr: self._readline_insert(c, echo, insptr, line) else: self._readline_echo(c, echo) line[insptr:insptr] = c insptr = insptr + len(c) if self._readline_do_echo(echo): self._current_line = line