#!/usr/local/bin/perl

# sample_server
# receives authentication info from a client using recvauth

use blib; # remove if not in module build directory
use IO::Socket;
use Sys::Hostname;
use Authen::Krb5 (KRB5_NT_SRV_HST);

# replace with your own stuff
$SERVICE = "sample";
$KEYTAB_FILE = "/etc/krb5.keytab";

chomp($SERVER = hostname());

Authen::Krb5::init_context();

$ac = new Authen::Krb5::AuthContext;

$s = new IO::Socket::INET(
	LocalAddr => $SERVER,
	LocalPort => 12345,
	Proto => 'tcp',
	Reuse => 1,
	Listen => 5
);
defined $s or die $!;

$ns = $s->accept();

$sprinc = Authen::Krb5::sname_to_principal($SERVER,$SERVICE,KRB5_NT_SRV_HST);
$kt = Authen::Krb5::kt_resolve("FILE:$KEYTAB_FILE");
$t = Authen::Krb5::recvauth($ac,$ns,"V1",$sprinc,$kt);
if ($t) {
	print "Received authentication info.\n";
	$client = $t->enc_part2->client;
	print "Hello, ",$client->data,".\n";
}
else {
	print "recvauth error: ",Authen::Krb5::error(),"\n";
}

close($ns);
close($s);

Authen::Krb5::free_context();
