Project

General

Profile

Download (5.25 KB) Statistics
| Branch: | Tag: | Revision:
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++

require 'eventmachine'
require 'cyborghood/cyborg/interface'
require 'shellwords'


module CyborgHood
class BotServer
private_class_method :new

def self.build(interface)
case Config.instance.botnet.connection_type
when 'unix_socket'
return BotServerUNIXSocket.new(interface)
else
raise CyberError.new(:unrecoverable, "config", "Unknown botnet connection type")
end
end

def initialize(interface)
@interface = interface

@config = Config.instance
end

def run
EventMachine.run do
yield
end
end

def stop
EventMachine.stop
end
end

class BotServerUNIXSocket < BotServer
public_class_method :new

def initialize(interface)
super(interface)

@socket = File.join(Config::RUN_DIR, @config.bot_name.downcase + ".sock")
at_exit { remove_socket_file }
end

def run
super do
EventMachine.start_unix_domain_server(@socket, ConversationUNIXSocket, @interface)
end
end

private

def remove_socket_file
File.delete(@socket) if @socket && File.exist?(@socket)
end
end

class Conversation < EventMachine::Protocols::LineAndTextProtocol
private_class_method :new

# don't rely on EventMachine's default, it may change one day
MaxLineLength = 16*1024

EOD = "\033[0J"
COMMAND_PATTERN = "^#{CyborgServerInterfaceBase::NODE_PATTERN}(?: ([+?]+))?$"

def initialize(interface)
@interface = interface

super

@config = Config.instance
@split_data_mode = false
@split_data_cmd = nil
@split_data = []
end

def send_line(msg)
send_data "#{msg}\n"
logger.debug "Sent data [#{identifier}]: #{msg}"
end

def post_init
logger.debug "New conversation with #{identifier}"
send_line "220 #{PRODUCT} #{VERSION} - #{@config.bot_name}"
end

def receive_line(data)

return if data.empty?

if data == EOD
logger.debug "Received EOD [#{identifier}]"
exit_split_mode
else
if @split_data_mode
logger.debug "Received data (split mode) [#{identifier}]: #{data}"

@split_data << data
else
logger.debug "Received data [#{identifier}]: #{data}"

unless data =~ Regexp.new(COMMAND_PATTERN)
logger.error "Error [#{identifier}]: syntax error"
send_line "552 syntax error in command"
return
end
cmd = $1
flags = $2 || ""
pp "GROK"
pp cmd
pp flags

if flags.index '?'
send_line "250+ ok"
send_line({'exists?' => @interface.has_node?(cmd)}.to_yaml)
return
end

if flags.index '+'
enter_split_mode(cmd)
else
receive_command(cmd)
end
end
end
end

def receive_command(cmd, data = nil)
logger.debug "Executing command '#{cmd}' [#{identifier}]"
send_line @interface.call(cmd, data)
end

def receive_error(msg)
logger.error "Error [#{identifier}]: #{msg}"
end

def unbind
logger.debug "Conversation finished with #{identifier}"
end

protected

def enter_split_mode(cmd)
if @split_data_mode
logger.error "Error [#{identifier}]: already in split mode"
send_line "551 protocol error"
@split_data_mode = false
@split_data_cmd = nil
else
logger.debug "Entered split mode for command '#{cmd}' [#{identifier}]"
@split_data_mode = true
@split_data_cmd = cmd
end
@split_data = []
end

def exit_split_mode
if @split_data_mode
logger.debug "Quit split mode for command '#{@split_data_cmd}' [#{identifier}]"
receive_command(@split_data_cmd, @split_data.join("\n"))
else
logger.error "Error [#{identifier}]: not in split mode"
send_line "551 protocol error"
end
@split_data_mode = false
@split_data_cmd = nil
@split_data = []
end
end

class ConversationUNIXSocket < Conversation
public_class_method :new

def identifier
"unix_socket/#{@signature}"
end
end

class EmptyInterface
include CyborgServerInterface
include CyborgServerDefaultInterface
end

module SimpleServer
def setup
super
@server = BotServer.build(self.interface)
end

def run
super
@server.run
end

def ask_to_stop
@server.stop
super
end

def interface
EmptyInterface.instance
end
end
end
(2-2/2)