File: gss_iov_server.rb

package info (click to toggle)
ruby-gssapi 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 216 kB
  • sloc: ruby: 770; makefile: 2
file content (67 lines) | stat: -rw-r--r-- 1,429 bytes parent folder | download | duplicates (3)
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