root/postman @ bc4894ce
55a68712 | Marc Dequenes | #!/usr/bin/ruby -Ku
|
|
# http://www.ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html
|
|||
$: << "./lib"
|
|||
#require 'socket'
|
|||
#require 'fileutils'
|
|||
#require 'tempfile'
|
|||
require 'shellwords'
|
|||
bc4894ce | Marc Dequenes | require 'cyborghood/imap'
|
|
c427bfc7 | Marc Dequenes | require 'cyborghood/mail'
|
|
bc4894ce | Marc Dequenes | require 'cyborghood/objects'
|
|
3f7a1eee | Marc Dequenes | ||
55a68712 | Marc Dequenes | #Socket.gethostname
|
|
#
|
|||
# TODO:
|
|||
# - should be able to handle encrypted messages for user to send sensitive data (postman would need a GPG key too)
|
|||
#
|
|||
class CommandParser
|
|||
c427bfc7 | Marc Dequenes | def self.run(order)
|
|
order.commands.each do |cmd|
|
|||
logger.info "Executing command: #{cmd}"
|
|||
3f7a1eee | Marc Dequenes | begin
|
|
c427bfc7 | Marc Dequenes | execute_cmd(order.user, cmd, order.refs)
|
|
3f7a1eee | Marc Dequenes | rescue
|
|
logger.info "Command failed: " + $!
|
|||
end
|
|||
55a68712 | Marc Dequenes | end
|
|
end
|
|||
private
|
|||
c427bfc7 | Marc Dequenes | def self.execute_cmd(user, cmdstr, refs)
|
|
55a68712 | Marc Dequenes | cmdline = Shellwords.shellwords(cmdstr)
|
|
subsys = cmdline.shift
|
|||
ok = true
|
|||
case subsys.upcase
|
|||
when "DNS"
|
|||
case cmdline.shift.upcase
|
|||
when "INFO"
|
|||
591ec1a2 | Marc Dequenes | if cmdline.empty?
|
|
c427bfc7 | Marc Dequenes | list = CyborgHood::DnsDomain.find_by_manager(user)
|
|
591ec1a2 | Marc Dequenes | logger.info "User is manager of the following zones: " + list.collect{|z| z.cn }.sort.join(", ")
|
|
else
|
|||
ok = false
|
|||
end
|
|||
55a68712 | Marc Dequenes | when "GET"
|
|
case cmdline.shift.upcase
|
|||
when "ZONE"
|
|||
zone = cmdline.shift.downcase
|
|||
c427bfc7 | Marc Dequenes | dom = CyborgHood::DnsDomain.new(zone)
|
|
591ec1a2 | Marc Dequenes | logger.info "User requesting zone content for '#{zone}'"
|
|
if dom.hosted?
|
|||
if dom.managed_by? user
|
|||
logger.info "User is manager of the zone"
|
|||
else
|
|||
logger.info "User is not allowed to manage the zone"
|
|||
end
|
|||
else
|
|||
logger.info "Zone not hosted"
|
|||
end
|
|||
else
|
|||
ok = false
|
|||
end
|
|||
55a68712 | Marc Dequenes | when "SET"
|
|
else
|
|||
ok = false
|
|||
end
|
|||
else
|
|||
ok = false
|
|||
end
|
|||
if not ok
|
|||
3f7a1eee | Marc Dequenes | logger.info "Command not recognized: #{cmdstr}"
|
|
55a68712 | Marc Dequenes | end
|
|
end
|
|||
end
|
|||
module CyborgHood
|
|||
# not yet ready to be a real Cyborg
|
|||
class Postman #< Cyborg
|
|||
def initialize
|
|||
# load config
|
|||
Config.load(self.human_name.downcase)
|
|||
@config = Config.instance
|
|||
0af9cada | Marc Dequenes | ldap_config = @config.ldap
|
|
ldap_config.logger = logger
|
|||
ActiveLdap::Base.establish_connection(ldap_config.marshal_dump)
|
|||
55a68712 | Marc Dequenes | # setup logs
|
|
unless @config.log.nil?
|
|||
logger.output_level(@config.log.console_level) unless @config.log.console_level.nil?
|
|||
logger.log_to_file(@config.log.file) unless @config.log.file.nil?
|
|||
end
|
|||
logger.info "Bot '#{self.human_name}' loaded"
|
|||
end
|
|||
def run
|
|||
945de171 | Marc Dequenes | imap = IMAP.new(@config.imap)
|
|
imap.check_mail do |msg|
|
|||
c427bfc7 | Marc Dequenes | mail = Mail.new(msg)
|
|
945de171 | Marc Dequenes | logger.info "Mail #{mail.message_id}: #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"
|
|
c427bfc7 | Marc Dequenes | ||
55a68712 | Marc Dequenes | # ignore mails not signed
|
|
591ec1a2 | Marc Dequenes | unless mail.is_pgp_signed?
|
|
c427bfc7 | Marc Dequenes | logger.info "Mail not signed or not RFC3156 compliant, ignoring..."
|
|
55a68712 | Marc Dequenes | next
|
|
591ec1a2 | Marc Dequenes | end
|
|
55a68712 | Marc Dequenes | ||
logger.debug "Proper signed content detected"
|
|||
c427bfc7 | Marc Dequenes | begin
|
|
order = mail.parse
|
|||
rescue CyberError => e
|
|||
945de171 | Marc Dequenes | case e.severity
|
|
when :dangerous
|
|||
logger.fatal " (#{e.message})"
|
|||
exit 2
|
|||
when :unrecoverable
|
|||
logger.error "Internal processing error, skipping mail (#{e.message})"
|
|||
next
|
|||
when :ignorable
|
|||
end
|
|||
55a68712 | Marc Dequenes | end
|
|
c427bfc7 | Marc Dequenes | if order.nil?
|
|
logger.info "Mail is invalid, ignoring..."
|
|||
next
|
|||
end
|
|||
CommandParser.run(order)
|
|||
55a68712 | Marc Dequenes | end
|
|
end
|
|||
def ask_to_stop
|
|||
end
|
|||
end
|
|||
end
|
|||
bot = CyborgHood::Postman.new
|
|||
trap('INT') do
|
|||
bot.ask_to_stop
|
|||
end
|
|||
trap('TERM') do
|
|||
bot.ask_to_stop
|
|||
end
|
|||
bot.run
|