diff --git a/lib/msf/Shared.rb b/lib/msf/Shared.rb deleted file mode 100644 index 5739ed414f..0000000000 --- a/lib/msf/Shared.rb +++ /dev/null @@ -1,17 +0,0 @@ -### -# -# framework-shared -# ---------------- -# -# The shared library in the framework contains classes that are -# used by various framework subsystems. -# -### - -# Shared single purpose classes -require 'Msf/Shared/ReadWriteLock' -require 'Msf/Shared/Transformer' - -# Logging facilities -require 'Msf/Shared/Logging/LogSink' -require 'Msf/Shared/Logging/LogDispatcher' diff --git a/lib/msf/core.rb b/lib/msf/core.rb index 0be715dba6..1adff86768 100644 --- a/lib/msf/core.rb +++ b/lib/msf/core.rb @@ -9,8 +9,8 @@ # ### -# framework-core depends on framework-shared -require 'Msf/Shared' +# framework-core depends on Rex +require 'Rex' # General require 'Msf/Core/Constants' diff --git a/lib/msf/core/event_dispatcher.rb b/lib/msf/core/event_dispatcher.rb index e23b6be621..0c9bf18cba 100644 --- a/lib/msf/core/event_dispatcher.rb +++ b/lib/msf/core/event_dispatcher.rb @@ -21,7 +21,7 @@ class EventDispatcher self.exploit_event_subscribers = [] self.session_event_subscribers = [] self.recon_event_subscribers = [] - self.subscribers_rwlock = ReadWriteLock.new + self.subscribers_rwlock = Rex::ReadWriteLock.new end # diff --git a/lib/msf/shared/Constants.rb b/lib/msf/shared/Constants.rb deleted file mode 100644 index 3278b50dcb..0000000000 --- a/lib/msf/shared/Constants.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Msf - -# -# Log severities -# -LOG_ERROR = 'error' -LOG_DEBUG = 'debug' -LOG_INFO = 'info' -LOG_WARN = 'warn' -LOG_RAW = 'raw' - -# -# Log levels -# -# LEV_0 errors and warnings may be displayed to the user by default. -# -LEV_0 = 0 -LEV_1 = 1 -LEV_2 = 2 -LEV_3 = 3 - -end diff --git a/lib/msf/shared/ReadWriteLock.rb b/lib/msf/shared/ReadWriteLock.rb deleted file mode 100644 index 8c540d2886..0000000000 --- a/lib/msf/shared/ReadWriteLock.rb +++ /dev/null @@ -1,163 +0,0 @@ -require 'thread' - -module Msf - -### -# -# ReadWriteLock -# ------------- -# -# This class implements a read/write lock synchronization -# primitive. It is meant to allow for more efficient access to -# resources that are more often read from than written to and many -# times can have concurrent reader threads. By allowing the reader -# threads to lock the resource concurrently rather than serially, -# a large performance boost can be seen. Acquiring a write lock -# results in exclusive access to the resource and thereby prevents -# any read operations during the time that a write lock is acquired. -# Only one write lock may be acquired at a time. -# -### -class ReadWriteLock - - def initialize - @read_sync_mutex = Mutex.new - @write_sync_mutex = Mutex.new - @exclusive_mutex = Mutex.new - @readers = 0 - @writer = false - end - - # Acquires the read lock for the calling thread - def lock_read - read_sync_mutex.lock - - begin - # If there are a non-zero number of readers and a - # writer is waiting to acquire the exclusive lock, - # free up the sync mutex temporarily and lock/unlock - # the exclusive lock. This is to give the writer - # thread a chance to acquire the lock and prevents - # it from being constantly starved. - if ((@readers > 0) and - (@writer)) - read_sync_mutex.unlock - exclusive_mutex.lock - exclusive_mutex.unlock - read_sync_mutex.lock - end - - # Increment the active reader count - @readers += 1 - - # If we now have just one reader, acquire the exclusive - # lock. Track the thread owner so that we release the - # lock from within the same thread context later on. - if (@readers == 1) - exclusive_mutex.lock - - @owner = Thread.current - end - ensure - read_sync_mutex.unlock - end - end - - # Releases the read lock for the calling thread - def unlock_read - read_sync_mutex.lock - - begin - unlocked = false - - # Keep looping until we've lost this thread's reader - # lock - while (!unlocked) - # If there are no more readers left after this one - if (@readers - 1 == 0) - # If the calling thread is the owner of the exclusive - # reader lock, then let's release that shit! - if (Thread.current == @owner) - @owner = nil - - exclusive_mutex.unlock - end - # If there is more than one reader left and this thread is - # the owner of the exclusive lock, then keep looping so that - # we can eventually unlock the exclusive mutex in this thread's - # context - elsif (Thread.current == @owner) - read_sync_mutex.unlock - - next - end - - # Unlocked! - unlocked = true - - # Decrement the active reader count - @readers -= 1 - end - ensure - read_sync_mutex.unlock - end - end - - # Acquire the exclusive write lock - def lock_write - write_sync_mutex.lock - - begin - @writer = true - - exclusive_mutex.lock - - @owner = Thread.current - ensure - write_sync_mutex.unlock - end - end - - # Release the exclusive write lock - def unlock_write - # If the caller is not the owner of the write lock, then someone is - # doing something broken, let's let them know. - if (Thread.current != @owner) - raise RuntimeError, "Non-owner calling thread attempted to release write lock", caller - end - - # Otherwise, release the exclusive write lock - @writer = false - - exclusive_mutex.unlock - end - - # Synchronize a block for read access - def synchronize_read - lock_read - begin - yield - ensure - unlock_read - end - end - - # Synchronize a block for write access - def synchronize_write - lock_write - begin - yield - ensure - unlock_write - end - end - -protected - - attr_accessor :read_sync_mutex - attr_accessor :write_sync_mutex - attr_accessor :exclusive_mutex - -end - -end diff --git a/lib/msf/shared/Transformer.rb b/lib/msf/shared/Transformer.rb deleted file mode 100644 index 2c28d58ae1..0000000000 --- a/lib/msf/shared/Transformer.rb +++ /dev/null @@ -1,87 +0,0 @@ -module Msf - -### -# -# Transformer - more than meets the eye! -# ----------- -# -# This class, aside from having a kickass name, is responsible for translating -# object instances of one or more types into a single list instance of one or -# more types. This is useful for translating object instances that be can -# either strings or an array of strings into an array of strings, for -# instance. It lets you make things take a uniform structure in an abstract -# manner. -# -### -class Transformer - - # Translates the object instance supplied in src_instance to an instance of - # dst_class. The dst_class parameter's instance must support the << - # operator. An example call to this method looks something like: - # - # Transformer.transform(string, Array, [ String ], target) - def Transformer.transform(src_instance, dst_class, supported_classes, - target = nil) - dst_instance = dst_class.new - - if (src_instance.kind_of?(Array)) - src_instance.each { |src_inst| - Transformer.transform_single(src_inst, dst_instance, - supported_classes, target) - } - elsif (!src_instance.kind_of?(NilClass)) - Transformer.transform_single(src_instance, dst_instance, - supported_classes, target) - end - - return dst_instance - end - -protected - - # Transform a single source instance. - def Transformer.transform_single(src_instance, dst_instance, - supported_classes, target) - # If the src instance's class is supported, just add it to the dst - # instance - if (supported_classes.include?(src_instance.class)) - dst_instance << src_instance - # If the source instance is a string, query each of the supported - # classes to see if they can serialize it to their particular data - # type. - elsif (src_instance.kind_of?(String)) - new_src_instance = nil - - # Walk each supported class calling from_s if exported - supported_classes.each { |sup_class| - new_src_instance = sup_class.from_s(src_instance) - - if (new_src_instance != nil) - dst_instance << new_src_instance - break - end - } - - # If we don't have a valid new src instance, then we suck - if (new_src_instance == nil) - bomb_translation(src_instance, target) - end - # Otherwise, bomb translation - else - bomb_translation(src_instance, target) - end - end - - def Transformer.bomb_translation(src_instance, target) - error = "Invalid source class (#{src_instance.class})" - - if (target != nil) - error += " for #{target}" - end - - raise ArgumentError, error, caller - end - -end - -end diff --git a/lib/msf/shared/logging/LogDispatcher.rb b/lib/msf/shared/logging/LogDispatcher.rb deleted file mode 100644 index d285f936ed..0000000000 --- a/lib/msf/shared/logging/LogDispatcher.rb +++ /dev/null @@ -1,126 +0,0 @@ -require 'Msf/Shared' - -module Msf -module Logging - -### -# -# LogDispatcher -# ------------- -# -# The log dispatcher associates log sources with log sinks. A log source -# is a unique identity that is associated with one and only one log sink. -# For instance, the framework-core registers the 'core' -# -### -class LogDispatcher - - def initialize() - self.log_sinks = {} - self.log_sinks_rwlock = ReadWriteLock.new - end - - # Returns the sink that is associated with the supplied source - def [](src) - sink = nil - - log_sinks_rwlock.synchronize_read { - sink = log_sinks[src] - } - - return sink - end - - # Calls the source association routnie - def []=(src, sink) - store(src, sink) - end - - # Associates the supplied source with the supplied sink - def store(src, sink) - log_sinks_rwlock.synchronize_write { - if (log_sinks[src] == nil) - log_sinks[src] = sink - else - raise( - RuntimeError, - "The supplied log source #{src} is already registered.", - caller) - end - } - end - - # Removes a source association if one exists - def delete(src) - sink = nil - - log_sinks_rwlock.synchronize_write { - sink = log_sinks[src] - - log_sinks.delete(src) - } - - if (sink) - sink.cleanup - - return true - end - - return false - end - - # Performs the actual log operation against the supplied source - def log(sev, src, level, msg, from) - log_sinks_rwlock.synchronize_read { - if ((sink = log_sinks[src])) - sink.log(sev, src, level, msg, from) - end - } - end - - attr_accessor :log_sinks, :log_sinks_rwlock -end - -end -end - -### -# -# An instance of the log dispatcher exists in the global namespace, along -# with stubs for many of the common logging methods. Various sources can -# register themselves as a log sink such that logs can be directed at -# various targets depending on where they're sourced from. By doing it -# this way, things like sessions can use the global logging stubs and -# still be directed at the correct log file. -# -### -def dlog(msg, src = 'core', level = 0, from = caller) - $dispatcher.log(Msf::LOG_DEBUG, src, level, msg, from) -end - -def elog(msg, src = 'core', level = 0, from = caller) - $dispatcher.log(Msf::LOG_ERROR, src, level, msg, from) -end - -def wlog(msg, src = 'core', level = 0, from = caller) - $dispatcher.log(Msf::LOG_WARN, src, level, msg, from) -end - -def ilog(msg, src = 'core', level = 0, from = caller) - $dispatcher.log(Msf::LOG_INFO, src, level, msg, from) -end - -def rlog(msg, src = 'core', level = 0, from = caller) - $dispatcher.log(Msf::LOG_RAW, src, level, msg, from) -end - -def register_log_source(src, sink) - $dispatcher[src] = sink -end - -def deregister_log_source(src, sink) - $dispatcher.delete(src) -end - -# Creates the global log dispatcher -$dispatcher = Msf::Logging::LogDispatcher.new diff --git a/lib/msf/shared/logging/LogSink.rb b/lib/msf/shared/logging/LogSink.rb deleted file mode 100644 index ecfe2f9d2d..0000000000 --- a/lib/msf/shared/logging/LogSink.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'Msf/Shared/Constants' - -module Msf -module Logging - -### -# -# LogSink -# ------- -# -# This abstract interface is what must be implemented by any class -# that would like to register as a log sink on a given LogDispatcher -# instance, such as the Framework object. -# -### -module LogSink - - def cleanup - end - - def log(sev, src, level, msg, from) - raise NotImplementedError - end - -protected - - def get_current_timestamp - return Time.now.strftime("%m/%d/%Y %H:%M:%S") - end - -end - -end; end - -require 'Msf/Shared/Logging/Sinks/Flatfile' diff --git a/lib/msf/shared/logging/sinks/Flatfile.rb b/lib/msf/shared/logging/sinks/Flatfile.rb deleted file mode 100644 index 8f10c36c6c..0000000000 --- a/lib/msf/shared/logging/sinks/Flatfile.rb +++ /dev/null @@ -1,50 +0,0 @@ -module Msf -module Logging -module Sinks - -### -# -# Flatfile -# -------- -# -# This class implements the LogSink interface and backs it against a -# file on disk. -# -### -class Flatfile - - include Msf::Logging::LogSink - - def initialize(file) - self.fd = File.new(file, "a") - end - - def cleanup - fd.close - end - - def log(sev, src, level, msg, from) - if (sev == LOG_RAW) - fd.write(msg) - else - code = 'i' - - case sev - when LOG_DEBUG - code = 'd' - when LOG_ERROR - code = 'e' - when LOG_INFO - code = 'i' - end - fd.write("[#{get_current_timestamp}] [#{code}(#{level})] #{src}: #{msg}\n") - end - end - -protected - - attr_accessor :fd - -end - -end; end; end