1
=begin
2
  GPG decryption script
3
  
4
  Requires a running GPGServer and uses another (proprietary protocol 
5
  addon to ESMPT for declaring source and target file for decryption. 
6
  This allows to reuse passphrase and GPG/MIME implementation which is 
7
  already implemented for the GPGServer
8
  
9
  (C) 2009, GNU General Public Licence, Author: Otto Linnemann
10
=end
11
12
# ------------- Main script  ------------- 
13
# Invocation: ruby gpg_smtp_decrypt encrypted_source_file decrypted_target_file [configfile]
14
15
16
# Read configuration file 
17
18
require 'socket'
19
require 'plist_parser'
20
21
LINESEP = "\r\n"
22
result = 0
23
24
if ARGV[3] == nil
25
  # in case no config file is given use default (here for MacOS X)
26
  configfilename = File.join( ENV["HOME"], "/Library/Preferences/GpgServer.plist" )
27
else
28
  configfilename = ARGV[3]
29
end
30
31
32
xml = ""
33
File.open( configfilename, "r") { |stream| xml = stream.read}
34
config  = PropertyParser.new.parse( xml )
35
36
extSmtp=config[0]["External SMTP"]
37
local=config[0]["Local SMTP"]
38
server_port = local["Port"]
39
40
41
begin
42
  # connect to server
43
  s = TCPSocket.open( "localhost", server_port )
44
rescue 
45
  STDERR.puts "GPGServer not running or couldn't connect!"
46
  exit -1
47
end
48
49
50
begin
51
  # reads server greeting message
52
  msg = s.gets 
53
  puts msg
54
  raise "Connection error" if !msg.match("GPG SMTP")
55
  
56
  # writes decode command to server 
57
  s.write "gpgdecode" + LINESEP
58
59
  # read ack from server
60
  msg = s.gets 
61
  puts msg
62
63
  # write source and target file to server
64
  s.write '"'+ARGV[0] +'" "' + ARGV[1] + '"' + LINESEP
65
66
  # read status from server
67
  msg = s.gets
68
  puts msg
69
  raise "GPG error: " + msg if !msg.match("250 Decryption: OK")
70
71
rescue => exception
72
  STDERR.puts "Decode error occured: "+exception.to_s
73
  result = -1
74
  
75
ensure
76
  # write quit to server
77
  s.write "QUIT" + LINESEP
78
  s.close
79
  exit result
80
  
81
end
82
__END__