aboutsummaryrefslogtreecommitdiff
path: root/3.2/get_mail.rb
diff options
context:
space:
mode:
Diffstat (limited to '3.2/get_mail.rb')
-rwxr-xr-x3.2/get_mail.rb85
1 files changed, 85 insertions, 0 deletions
diff --git a/3.2/get_mail.rb b/3.2/get_mail.rb
new file mode 100755
index 0000000..8cbbb1f
--- /dev/null
+++ b/3.2/get_mail.rb
@@ -0,0 +1,85 @@
+#!/usr/bin/env ruby
+
+require 'net/http'
+require 'uri'
+require 'json'
+
+def get_ticket(un, tgt, sv)
+ uri = URI.parse("http://localhost:4567/ticket")
+ header = {'Content-Type': 'text/json'}
+
+ login = {
+ "username": un,
+ "ticket": tgt,
+ "service": sv
+ }
+
+ http = Net::HTTP.new(uri.host, uri.port)
+ request = Net::HTTP::Post.new(uri.request_uri, header)
+ request.body = login.to_json
+
+ response = http.request(request)
+ response.body
+end
+
+def ticket_valid?(ticket)
+ now = Time.now.to_i
+ timeStart = ticket["timestamp"]
+ timeEnd = ticket["timestamp"] + ticket["lifespan"]
+ now >= timeStart && now < timeEnd
+end
+
+def update_keytab!
+ File.open(".keytab", "w") do |f|
+ f.puts Tickets.map { |s, p| [s, *p.values].join(?:) }
+ end
+end
+
+print "Username: "; un = gets.strip
+print "Mailserver: "; ms = gets.strip
+
+unless File.exist?(".keytab")
+ puts "No keytab, please kinit"
+ exit 1
+end
+
+Tickets = File.readlines(".keytab").map { |l|
+ a = l.strip.split(?:)
+ [a[0], {
+ "lifespan" => a[1].to_i,
+ "timestamp" => a[2].to_i,
+ "ticket" => a[3]
+ }]
+}.to_h
+
+unless Tickets.keys.include?(ms) && ticket_valid?(Tickets[ms])
+ if Tickets.keys.include?("_TGS") && ticket_valid?(Tickets["_TGS"])
+ packet = get_ticket(un, Tickets["_TGS"]["ticket"], ms).split(?:)
+ Tickets[ms] = {
+ "lifespan" => packet[0].to_i,
+ "timestamp" => packet[1].to_i,
+ "ticket" => packet[2]
+ }
+ update_keytab!
+ else
+ puts "No Ticket Granting Ticket, please kinit"
+ exit 1
+ end
+end
+
+ticket = Tickets[ms]["ticket"]
+
+uri = URI.parse("http://localhost:4568/login")
+header = {'Content-Type': 'text/json'}
+
+login = {
+ "username": un,
+ "ticket": ticket
+}
+
+http = Net::HTTP.new(uri.host, uri.port)
+request = Net::HTTP::Post.new(uri.request_uri, header)
+request.body = login.to_json
+
+response = http.request(request)
+puts response.body