|
require 'cyborghood/base'
|
|
require 'delegate'
|
|
require 'tmail'
|
|
require 'tmail_extra'
|
|
require 'gpgme' # >= 1.0.2 needed for :always_trust sign option
|
|
require 'net/smtp'
|
|
|
|
# This class handles RFC3156 signed messages, validates them, and extract orders properly.
|
|
# Encrypted content are not implemented yet.
|
|
module CyborgHood
|
|
class Mail < Delegator
|
|
def initialize(msg)
|
|
@config = Config.instance
|
|
|
|
# unquote headers and transform into TMail object
|
|
@mail = TMail::Mail.parse(TMail::Unquoter.unquote_and_convert_to(msg, "UTF-8"))
|
|
end
|
|
|
|
def __getobj__
|
|
@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
|
|
logger.info "Mail content was properly signed by key #{sig_check.fingerprint}"
|
|
user = Person.find_by_fingerprint(sig_check.fingerprint)
|
|
if user.nil?
|
|
logger.info "Mail is from an unknown person"
|
|
else
|
|
logger.info "Mail is from user #{user.uid} (#{user.cn})"
|
|
|
|
signed_content = pgp_signed_part()
|
|
if signed_content.multipart?
|
|
if signed_content.parts[0].content_type == "text/plain"
|
|
command_txt = signed_content.parts[0].body
|
|
refs = signed_content.parts.collect{|p| p.dup }
|
|
end
|
|
else
|
|
command_txt = signed_content.body if signed_content.content_type == "text/plain"
|
|
refs = []
|
|
end
|
|
|
|
if command_txt
|
|
commands = []
|
|
command_txt.each_line do |line|
|
|
line.chomp!
|
|
sline = line.strip
|
|
# skip empty lines and comments
|
|
next if sline == "" or sline[0, 1] == "#"
|
|
# stop processing when detecting message signature
|
|
break if line == "-- "
|
|
|
|
commands << sline
|
|
end
|
|
|
|
return {:user => user, :commands => commands, :refs => refs}.to_ostruct
|
|
else
|
|
logger.info "Mail does not contain a proper MIME part for commands"
|
|
end
|
|
end
|
|
else
|
|
logger.info "Mail content tampered or badly signed: " + sig_check.to_s
|
|
end
|
|
|
|
nil
|
|
end
|
|
|
|
def create_reply
|
|
mail_reply = @mail.create_reply
|
|
mail_reply.from_addrs = TMail::Address.parse(@config.mail.from_address || self.to.first)
|
|
self.class.new(mail_reply.to_s)
|
|
end
|
|
|
|
def deliver
|
|
smtp_server = @config.mail.smtp_server || "localhost"
|
|
smtp_port = @config.mail.smtp_port || 25
|
|
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|
|
|
#p @mail.to_s
|
|
smtp.send_message(@mail.to_s, smtp_from, smtp_to)
|
|
end
|
|
end
|
|
|
|
def to_s
|
|
@mail.to_s
|
|
end
|
|
|
|
def crypt(fingerprint)
|
|
# build a fake mail to get the generated content to be crypted
|
|
if @mail.multipart?
|
|
fake_mail = TMail::Mail.new
|
|
fake_mail.set_content_type("multipart", "mixed", {'boundary' => TMail.new_boundary})
|
|
@mail.each_part {|p| fake_mail.parts << p }
|
|
clear_data = fake_mail.to_s
|
|
else
|
|
fake_mail = TMail::Mail.new
|
|
fake_mail.content_type = @mail.content_type
|
|
fake_mail.body = @mail.body
|
|
fake_mail.transfer_encoding = @mail.transfer_encoding if @mail.transfer_encoding
|
|
clear_data = fake_mail.to_s
|
|
end
|
|
|
|
# retrieve key and encrypt
|
|
gpg = GPGME::Ctx.new
|
|
key = gpg.get_key(fingerprint)
|
|
encrypted_data = GPGME.encrypt([key], clear_data, {:armor => true, :always_trust => true})
|
|
|
|
# build properly encrypted mail
|
|
# (modify original mail parts)
|
|
@mail.set_content_type("multipart", "encrypted", {'boundary' => TMail.new_boundary, "protocol" => "application/pgp-encrypted"})
|
|
@mail.transfer_encoding = nil
|
|
@mail.body = "This mail is a RFC3156 crypted message."
|
|
@mail.parts.clear
|
|
p_pgp = TMail::Mail.new
|
|
p_pgp.set_content_type("application", "pgp-encrypted")
|
|
p_pgp.body = "Version: 1"
|
|
@mail.parts << p_pgp
|
|
p_encrypted = TMail::Mail.new
|
|
p_encrypted.set_content_type("application", "octet-stream")
|
|
p_encrypted.body = encrypted_data
|
|
@mail.parts << p_encrypted
|
|
end
|
|
end
|
|
end
|