authsock.pl revision 1.1.1.4
1#!/usr/bin/env perl
2
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0.  If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14# test the update-policy external protocol
15
16require 5.6.0;
17
18use IO::Socket::UNIX;
19use Getopt::Long;
20
21my $path;
22my $typeallowed = "A";
23my $pidfile = "authsock.pid";
24my $timeout = 0;
25
26GetOptions("path=s" => \$path,
27	   "type=s" => \$typeallowed,
28	   "pidfile=s" => \$pidfile,
29	   "timeout=i" => \$timeout);
30
31if (!defined($path)) {
32	print("Usage: authsock.pl --path=<sockpath> --type=type --pidfile=pidfile\n");
33	exit(1);
34}
35
36unlink($path);
37my $server = IO::Socket::UNIX->new(Local => $path, Type => SOCK_STREAM, Listen => 8) or
38    die "unable to create socket $path";
39chmod 0777, $path;
40
41# setup our pidfile
42open(my $pid,">",$pidfile)
43    or die "unable to open pidfile $pidfile";
44print $pid "$$\n";
45close($pid);
46
47# close gracefully
48sub rmpid { unlink "$pidfile"; exit 1; };
49$SIG{INT} = \&rmpid;
50$SIG{TERM} = \&rmpid;
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