1#!/usr/bin/env perl
2#
3# Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15# PERFORMANCE OF THIS SOFTWARE.
16
17# Id: authsock.pl,v 1.3 2011/01/07 23:47:07 tbox Exp
18
19# test the update-policy external protocol
20
21require 5.6.0;
22
23use IO::Socket::UNIX;
24use Getopt::Long;
25
26my $path;
27my $typeallowed = "A";
28my $pidfile = "authsock.pid";
29my $timeout = 0;
30
31GetOptions("path=s" => \$path,
32	   "type=s" => \$typeallowed,
33	   "pidfile=s" => \$pidfile,
34	   "timeout=i" => \$timeout);
35
36if (!defined($path)) {
37	print("Usage: authsock.pl --path=<sockpath> --type=type --pidfile=pidfile\n");
38	exit(1);
39}
40
41unlink($path);
42my $server = IO::Socket::UNIX->new(Local => $path, Type => SOCK_STREAM, Listen => 8) or
43    die "unable to create socket $path";
44chmod 0777, $path;
45
46# setup our pidfile
47open(my $pid,">",$pidfile)
48    or die "unable to open pidfile $pidfile";
49print $pid "$$\n";
50close($pid);
51
52if ($timeout != 0) {
53    # die after the given timeout
54    alarm($timeout);
55}
56
57while (my $client = $server->accept()) {
58	$client->recv(my $buf, 8, 0);
59	my ($version, $req_len) = unpack('N N', $buf);
60
61	if ($version != 1 || $req_len < 17) {
62		printf("Badly formatted request\n");
63		$client->send(pack('N', 2));
64		next;
65	}
66
67	$client->recv(my $buf, $req_len - 8, 0);
68
69	my ($signer,
70	    $name,
71	    $addr,
72	    $type,
73	    $key,
74	    $key_data) = unpack('Z* Z* Z* Z* Z* N/a', $buf);
75
76	if ($req_len != length($buf)+8) {
77		printf("Length mismatch %u %u\n", $req_len, length($buf)+8);
78		$client->send(pack('N', 2));
79		next;
80	}
81
82	printf("version=%u signer=%s name=%s addr=%s type=%s key=%s key_data_len=%u\n",
83	       $version, $signer, $name, $addr, $type, $key, length($key_data));
84
85	my $result;
86	if ($typeallowed eq $type) {
87		$result = 1;
88		printf("allowed type %s == %s\n", $type, $typeallowed);
89	} else {
90		printf("disallowed type %s != %s\n", $type, $typeallowed);
91		$result = 0;
92	}
93
94	$reply = pack('N', $result);
95	$client->send($reply);
96}
97