2005-07-14 22:45:10 +00:00
|
|
|
require 'rex/ui'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Ui
|
|
|
|
module Text
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'readline'
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class implements standard input using readline against
|
|
|
|
# standard input. It supports tab completion.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Input::Readline < Rex::Ui::Text::Input
|
|
|
|
include ::Readline
|
2008-06-27 19:57:28 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Initializes the readline-aware Input instance for text.
|
|
|
|
#
|
2005-07-14 22:45:10 +00:00
|
|
|
def initialize(tab_complete_proc = nil)
|
|
|
|
if (tab_complete_proc)
|
2005-11-23 22:12:49 +00:00
|
|
|
::Readline.basic_word_break_characters = "\x00"
|
2005-07-14 22:45:10 +00:00
|
|
|
::Readline.completion_proc = tab_complete_proc
|
2007-09-22 19:53:39 +00:00
|
|
|
@rl_saved_proc = tab_complete_proc
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
2006-07-31 20:00:20 +00:00
|
|
|
|
2007-09-22 19:53:39 +00:00
|
|
|
#
|
|
|
|
# Reattach the original completion proc
|
|
|
|
#
|
|
|
|
def reset_tab_completion(tab_complete_proc = nil)
|
|
|
|
::Readline.basic_word_break_characters = "\x00"
|
|
|
|
::Readline.completion_proc = tab_complete_proc || @rl_saved_proc
|
|
|
|
end
|
|
|
|
|
2008-06-04 22:56:25 +00:00
|
|
|
def child_readline(wtr, prompt, history)
|
|
|
|
$0 = "<readline>"
|
2008-06-27 20:51:36 +00:00
|
|
|
|
2008-06-04 22:56:25 +00:00
|
|
|
line = ::Readline.readline(prompt, history)
|
|
|
|
line = "\n" if (line and line.strip.length == 0)
|
2008-06-27 20:51:36 +00:00
|
|
|
|
2008-06-04 22:56:25 +00:00
|
|
|
wtr.write(line || "exit\n")
|
|
|
|
wtr.flush
|
|
|
|
wtr.close
|
2008-06-27 20:51:36 +00:00
|
|
|
|
|
|
|
# Self-destruct mechanism activated
|
|
|
|
Process.kill(9, $$)
|
2008-06-04 22:56:25 +00:00
|
|
|
end
|
|
|
|
|
2006-07-31 20:00:20 +00:00
|
|
|
#
|
|
|
|
# Whether or not the input medium supports readline.
|
|
|
|
#
|
|
|
|
def supports_readline
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2005-12-07 03:40:09 +00:00
|
|
|
#
|
|
|
|
# Calls sysread on the standard input handle.
|
|
|
|
#
|
|
|
|
def sysread(len = 1)
|
2008-10-01 17:32:55 +00:00
|
|
|
begin
|
|
|
|
self.fd.sysread(len)
|
|
|
|
rescue ::Errno::EINTR
|
|
|
|
retry
|
|
|
|
end
|
2005-12-07 03:40:09 +00:00
|
|
|
end
|
2005-11-23 22:12:49 +00:00
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2008-06-04 22:56:25 +00:00
|
|
|
# Read a line from stdin
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2006-07-27 22:28:19 +00:00
|
|
|
def gets()
|
2008-10-01 17:32:55 +00:00
|
|
|
begin
|
|
|
|
self.fd.gets()
|
|
|
|
rescue ::Errno::EINTR
|
|
|
|
retry
|
|
|
|
end
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2008-06-04 22:56:25 +00:00
|
|
|
# Prompt-based getline using readline. We run the actual Readline routine inside of
|
|
|
|
# a forked child process. This solves a ton of problems introduced by the Readline
|
|
|
|
# extension. Specifically, readline will use 100ms for each time slice that its thread
|
|
|
|
# receives, massively slowing down the entire framework.
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def pgets
|
2008-06-27 19:57:28 +00:00
|
|
|
|
2008-06-27 20:51:36 +00:00
|
|
|
if(Rex::Compat.is_windows())
|
2008-06-05 00:13:33 +00:00
|
|
|
output.prompting
|
2008-06-05 00:15:40 +00:00
|
|
|
line = ::Readline.readline(prompt, true)
|
2008-06-24 20:55:42 +00:00
|
|
|
HISTORY.pop if (line and line.empty?)
|
2008-06-05 00:13:33 +00:00
|
|
|
return line
|
|
|
|
end
|
2008-06-27 19:57:28 +00:00
|
|
|
|
2008-06-27 20:51:36 +00:00
|
|
|
|
|
|
|
line = ""
|
2008-06-05 00:13:33 +00:00
|
|
|
|
2008-06-04 22:56:25 +00:00
|
|
|
output.prompting
|
2007-04-04 02:38:15 +00:00
|
|
|
|
2008-06-04 22:56:25 +00:00
|
|
|
rdr,wtr = ::IO.pipe
|
|
|
|
pid = fork()
|
|
|
|
|
|
|
|
if(not pid)
|
|
|
|
child_readline(wtr, prompt, true)
|
2006-07-27 22:28:19 +00:00
|
|
|
end
|
2008-06-04 22:56:25 +00:00
|
|
|
|
|
|
|
line = nil
|
|
|
|
while(not line)
|
|
|
|
r = select([rdr], nil, nil, 0.01)
|
|
|
|
if(r)
|
|
|
|
line = rdr.sysread(16384)
|
|
|
|
break if not line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-04-04 02:38:15 +00:00
|
|
|
output.prompting(false)
|
|
|
|
|
2008-06-04 22:56:25 +00:00
|
|
|
::Process.waitpid(pid, 0)
|
|
|
|
|
2008-06-27 20:51:36 +00:00
|
|
|
# Parse the results
|
2007-04-04 02:38:15 +00:00
|
|
|
if line
|
2008-06-05 00:01:38 +00:00
|
|
|
HISTORY.push(line) if (not line.empty?)
|
2005-07-14 22:45:10 +00:00
|
|
|
return line + "\n"
|
|
|
|
else
|
|
|
|
eof = true
|
|
|
|
return line
|
2008-06-27 20:51:36 +00:00
|
|
|
end
|
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2006-07-27 22:28:19 +00:00
|
|
|
# Returns the output pipe handle
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def fd
|
2008-06-04 22:56:25 +00:00
|
|
|
$stdin
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
# Indicates that this input medium as a shell builtin, no need
|
|
|
|
# to extend.
|
|
|
|
#
|
|
|
|
def intrinsic_shell?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The prompt that is to be displayed.
|
|
|
|
#
|
|
|
|
attr_accessor :prompt
|
|
|
|
#
|
|
|
|
# The output handle to use when displaying the prompt.
|
|
|
|
#
|
|
|
|
attr_accessor :output
|
2005-07-14 22:45:10 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2008-10-19 21:03:39 +00:00
|
|
|
end
|