Project

General

Profile

Download (5.28 KB) Statistics
| Branch: | Tag: | Revision:

# add Rails load path for Debian, until rails framework is split properly
DEB_RAILS_PATH = "/usr/share/rails"
Dir.new(DEB_RAILS_PATH).each do |file|
next if file =~ /^\./
path = File.join(DEB_RAILS_PATH, file, "lib")
$: << path if File.directory?(path)
end

require 'cyborghood/base'
require 'delegate'
require 'tmail'
require 'tmail_extra'
require 'gpgme' # >= 1.0.2 needed for :always_trust sign option
require 'action_mailer/quoting'
require 'action_mailer/utils'
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
include ActionMailer::Quoting
include ActionMailer::Utils

def initialize(msg = nil)
@config = Config.instance

if msg.nil?
@mail = TMail::Mail.new
set_custom_headers
else
# unquote headers and transform into TMail object
@mail = TMail::Mail.parse(TMail::Unquoter.unquote_and_convert_to(msg, "UTF-8"))
end
end

def __getobj__
@mail
end

def parse
order = {:ok => false, :msg => "mail not formatted correctly"}

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"
order[:msg] = "unknown user"
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

order = {:ok => true, :user => user, :commands => commands, :refs => refs}
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
return nil
end

order.to_ostruct
end

def create_reply
tmail_reply = @mail.create_reply
tmail_reply.from_addrs = TMail::Address.parse(@config.mail.from_address || self.to.first)
reply = self.class.new(tmail_reply.to_s)
reply.set_custom_headers
reply
end

def set_custom_headers
@mail['Organization'] = @config.mail.organization
end

def check_headers
@mail.header.keys.each do |h|
@mail[h] = quote_address_if_necessary(@mail[h].to_s, "utf-8")
end
end

def deliver
check_headers

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 encrypted 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

def quoted_printable_body=(txt)
@mail.transfer_encoding = "quoted-printable"
@mail.body = [normalize_new_lines(txt)].pack("M*")
end
end
end
(5-5/6)