authsock.pl revision 1.1.1.1
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 http://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
45if ($timeout != 0) {
46    # die after the given timeout
47    alarm($timeout);
48}
49
50while (my $client = $server->accept()) {
51	$client->recv(my $buf, 8, 0);
52	my ($version, $req_len) = unpack('N N', $buf);
53
54	if ($version != 1 || $req_len < 17) {
55		printf("Badly formatted request\n");
56		$client->send(pack('N', 2));
57		next;
58	}
59
60	$client->recv(my $buf, $req_len - 8, 0);
61
62	my ($signer,
63	    $name,
64	    $addr,
65	    $type,
66	    $key,
67	    $key_data) = unpack('Z* Z* Z* Z* Z* N/a', $buf);
68
69	if ($req_len != length($buf)+8) {
70		printf("Length mismatch %u %u\n", $req_len, length($buf)+8);
71		$client->send(pack('N', 2));
72		next;
73	}
74
75	printf("version=%u signer=%s name=%s addr=%s type=%s key=%s key_data_len=%u\n",
76	       $version, $signer, $name, $addr, $type, $key, length($key_data));
77
78	my $result;
79	if ($typeallowed eq $type) {
80		$result = 1;
81		printf("allowed type %s == %s\n", $type, $typeallowed);
82	} else {
83		printf("disallowed type %s != %s\n", $type, $typeallowed);
84		$result = 0;
85	}
86
87	$reply = pack('N', $result);
88	$client->send($reply);
89}
90