1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/usr/bin/env ruby
$: << '../lib'
$: << '.'
#require 'gssapi/heimdal'
require 'gssapi'
require 'gss_iov_helpers'
require 'base64'
require 'socket'
class GssIovServer
include GssIOVHelpers
def initialize
@host = 'example.org'
@service = "host"
@keytab = "#{ENV['HOME']}/.gssapi/krb5.keytab" # this is optional, but probably required if not running as root
@port = 8082
@tcpsrv = TCPServer.new(@host, @port)
end
def runner
loop do
puts "Listening on port #{@port}"
Thread.start(@tcpsrv.accept) do |s|
init_krb
handshake(s)
begin
emsg = (s.gets.chomp)
puts "---> Received: #{emsg}"
msg = iov_decrypt(emsg)
puts "===> Received: #{msg}"
end while msg != 'exit'
print(s, "Closing Socket\n")
s.close
puts "Closed...."
end
end
end
private
def init_krb
@gss = GSSAPI::Simple.new(@host, @service, @keytab)
@gss.acquire_credentials
puts "HELLO"
end
def handshake(sock)
print(sock, "Accepted Connection\n")
stok = sock.gets.chomp
print(sock, "Received string#{stok}\n")
otok = @gss.accept_context(Base64.strict_decode64(stok.chomp))
sock.write("#{Base64.strict_encode64(otok)}\n")
end
# Decrypt message
def msg_dec(msg)
@gss.unwrap_message(Base64.strict_decode64(msg.chomp))
end
end
gserv = GssIovServer.new
gserv.runner
|