Project

General

Profile

« Previous | Next » 

Revision 4153cce4

Added by Marc Dequènes almost 16 years ago

  • ID 4153cce452781b6afb801451276ed6151581532a

[evol] new DNS class to manage zone files (only read zone file at the moment) + generate proper mail reply

View differences:

lib/cyborghood/mail.rb
@mail
end
def self.normalize_new_lines(text)
text.to_s.gsub(/\r\n?/, "\n")
end
def parse
sig_check = verify_pgp_signature()
if sig_check.status == 0
......
smtp_from = @mail.from_addrs.collect{|a| a.address}.join(", ")
smtp_to = @mail.to_addrs.collect{|a| a.address}
Net::SMTP.start(smtp_server, smtp_port) do |smtp|
smtp.send_message(@mail.to_s, smtp_from, smtp_to)
#smtp.send_message(@mail.to_s, smtp_from, smtp_to)
p @mail.to_s
end
end
lib/cyborghood/services/dns.rb
module CyborgHood
module Services
class DNS
def initialize(zone)
@zone = zone
@config = Config.instance
end
def read_zone
file = @config.dns.master_zone_pattern.gsub("#ZONE#", @zone)
# TODO: should be checked at startup time
raise CyberError.new(:unrecoverable, "services/dns", "erroneous configuration: pattern is constant") if file == @config.dns.master_zone_pattern
begin
File.read(file)
rescue
raise CyberError.new(:unrecoverable, "services/dns", "zone '#{@zone}' cannot be read (nonexistent or lack of permission)")
end
end
end
end
end
postman
require 'cyborghood/imap'
require 'cyborghood/mail'
require 'cyborghood/objects'
require 'cyborghood/services/dns'
#Socket.gethostname
......
class CommandParser
def self.run(order)
result_list = []
order.commands.each do |cmd|
logger.info "Executing command: #{cmd}"
begin
execute_cmd(order.user, cmd, order.refs)
result_list << execute_cmd(order.user, cmd, order.refs)
rescue
logger.info "Command failed: " + $!
end
end
result_list
end
private
......
cmdline = Shellwords.shellwords(cmdstr)
subsys = cmdline.shift
result = OpenStruct.new
result.cmd = cmdstr
ok = true
case subsys.upcase
when "DNS"
......
when "INFO"
if cmdline.empty?
list = CyborgHood::DnsDomain.find_by_manager(user)
logger.info "User is manager of the following zones: " + list.collect{|z| z.cn }.sort.join(", ")
txt_list = list.collect{|z| z.cn }.sort.join(", ")
logger.info "User is manager of the following zones: " + txt_list
result.message = "You are manager of the following zones: " + txt_list
else
ok = false
end
......
if dom.hosted?
if dom.managed_by? user
logger.info "User is manager of the zone"
srv_dns = CyborgHood::Services::DNS.new(zone)
result.message = "Requested zone content attached."
result.refs = [srv_dns.read_zone]
else
logger.info "User is not allowed to manage the zone"
result.message = "You are not allowed to manage this zone."
end
else
logger.info "Zone not hosted"
result.message "This zone is not hosted here."
end
else
ok = false
end
when "SET"
result.message = "Command not yet implemented."
else
ok = false
end
......
end
if not ok
result.message = "Command not recognized"
result.refs = nil
logger.info "Command not recognized: #{cmdstr}"
end
result
end
end
# imap.store(message_id, "+FLAGS", [:Deleted])
# imap.expunge()
module CyborgHood
# not yet ready to be a real Cyborg
class Postman #< Cyborg
......
next
end
CommandParser.run(order)
result_list = CommandParser.run(order)
mail_reply = mail.create_reply
mail_reply.body = "COUCOU"
reply_txt = "Hello #{order.user.cn},\n\nFollows the transcript of your commands:\n"
reply_attachments = []
result_list.each do |result|
reply_txt << "> #{result.cmd}\n"
reply_txt << "#{result.message}\n"
reply_attachments += result.refs unless result.refs.nil?
end
if reply_attachments.empty?
mail_reply.set_content_type("text", "plain")
mail_reply.set_disposition("inline")
mail_reply.transfer_encoding = "quoted-printable"
mail_reply.body = reply_txt
else
boundary = TMail.new_boundary
mail_reply.set_content_type("multipart", "mixed", {'boundary' => boundary})
parts = []
p = TMail::Mail.new()
p.set_content_type("text", "plain", {'charset' => "utf-8"})
p.set_disposition("inline")
p.transfer_encoding = "quoted-printable"
p.body = [Mail.normalize_new_lines(reply_txt)].pack("M*")
mail_reply.parts << p
reply_attachments.each do |attachment|
p = TMail::Mail.new()
p.set_content_type("text", "plain", {'charset' => "utf-8"})
p.set_disposition("attachment", {'filename' => "test.rb"})
p.transfer_encoding = "quoted-printable"
p.body = [Mail.normalize_new_lines(attachment)].pack("M*")
mail_reply.parts << p
end
end
mail_reply.deliver
end
end

Also available in: Unified diff