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