authsock.pl revision 1.1.1.3
1#!/usr/bin/env perl
2#
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0. If a copy of the MPL was not distributed with this
7# file, you can obtain one at https://mozilla.org/MPL/2.0/.
8#
9# See the COPYRIGHT file distributed with this work for additional
10# information regarding copyright ownership.
11
12# test the update-policy external protocol
13
14require 5.6.0;
15
16use IO::Socket::UNIX;
17use Getopt::Long;
18
19my $path;
20my $typeallowed = "A";
21my $pidfile = "authsock.pid";
22my $timeout = 0;
23
24GetOptions("path=s" => \$path,
25	   "type=s" => \$typeallowed,
26	   "pidfile=s" => \$pidfile,
27	   "timeout=i" => \$timeout);
28
29if (!defined($path)) {
30	print("Usage: authsock.pl --path=<sockpath> --type=type --pidfile=pidfile\n");
31	exit(1);
32}
33
34unlink($path);
35my $server = IO::Socket::UNIX->new(Local => $path, Type => SOCK_STREAM, Listen => 8) or
36    die "unable to create socket $path";
37chmod 0777, $path;
38
39# setup our pidfile
40open(my $pid,">",$pidfile)
41    or die "unable to open pidfile $pidfile";
42print $pid "$$\n";
43close($pid);
44
45# close gracefully
46sub rmpid { unlink "$pidfile"; exit 1; };
47$SIG{INT} = \&rmpid;
48$SIG{TERM} = \&rmpid;
49
50if ($timeout != 0) {
51    # die after the given timeout
52    alarm($timeout);
53}
54
55while (my $client = $server->accept()) {
56	$client->recv(my $buf, 8, 0);
57	my ($version, $req_len) = unpack('N N', $buf);
58
59	if ($version != 1 || $req_len < 17) {
60		printf("Badly formatted request\n");
61		$client->send(pack('N', 2));
62		next;
63	}
64
65	$client->recv(my $buf, $req_len - 8, 0);
66
67	my ($signer,
68	    $name,
69	    $addr,
70	    $type,
71	    $key,
72	    $key_data) = unpack('Z* Z* Z* Z* Z* N/a', $buf);
73
74	if ($req_len != length($buf)+8) {
75		printf("Length mismatch %u %u\n", $req_len, length($buf)+8);
76		$client->send(pack('N', 2));
77		next;
78	}
79
80	printf("version=%u signer=%s name=%s addr=%s type=%s key=%s key_data_len=%u\n",
81	       $version, $signer, $name, $addr, $type, $key, length($key_data));
82
83	my $result;
84	if ($typeallowed eq $type) {
85		$result = 1;
86		printf("allowed type %s == %s\n", $type, $typeallowed);
87	} else {
88		printf("disallowed type %s != %s\n", $type, $typeallowed);
89		$result = 0;
90	}
91
92	$reply = pack('N', $result);
93	$client->send($reply);
94}
95