c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt#!/usr/bin/env perl
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# Copyright (C) 2011, 2012, 2016 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# file, You can obtain one at http://mozilla.org/MPL/2.0/.
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
93235c1cba839a88013063ff8c440849d95dd222Automatic Updater# $Id: authsock.pl,v 1.3 2011/01/07 23:47:07 tbox Exp $
93235c1cba839a88013063ff8c440849d95dd222Automatic Updater
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt# test the update-policy external protocol
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntrequire 5.6.0;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntuse IO::Socket::UNIX;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntuse Getopt::Long;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntmy $path;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntmy $typeallowed = "A";
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntmy $pidfile = "authsock.pid";
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntmy $timeout = 0;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan HuntGetOptions("path=s" => \$path,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt "type=s" => \$typeallowed,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt "pidfile=s" => \$pidfile,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt "timeout=i" => \$timeout);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntif (!defined($path)) {
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt print("Usage: authsock.pl --path=<sockpath> --type=type --pidfile=pidfile\n");
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt exit(1);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt}
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntunlink($path);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntmy $server = IO::Socket::UNIX->new(Local => $path, Type => SOCK_STREAM, Listen => 8) or
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt die "unable to create socket $path";
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntchmod 0777, $path;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt# setup our pidfile
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntopen(my $pid,">",$pidfile)
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt or die "unable to open pidfile $pidfile";
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntprint $pid "$$\n";
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntclose($pid);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntif ($timeout != 0) {
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt # die after the given timeout
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt alarm($timeout);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt}
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Huntwhile (my $client = $server->accept()) {
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $client->recv(my $buf, 8, 0);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt my ($version, $req_len) = unpack('N N', $buf);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt if ($version != 1 || $req_len < 17) {
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt printf("Badly formatted request\n");
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $client->send(pack('N', 2));
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt next;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt }
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $client->recv(my $buf, $req_len - 8, 0);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt my ($signer,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $name,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $addr,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $type,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $key,
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $key_data) = unpack('Z* Z* Z* Z* Z* N/a', $buf);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt if ($req_len != length($buf)+8) {
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt printf("Length mismatch %u %u\n", $req_len, length($buf)+8);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $client->send(pack('N', 2));
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt next;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt }
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt printf("version=%u signer=%s name=%s addr=%s type=%s key=%s key_data_len=%u\n",
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $version, $signer, $name, $addr, $type, $key, length($key_data));
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt my $result;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt if ($typeallowed eq $type) {
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $result = 1;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt printf("allowed type %s == %s\n", $type, $typeallowed);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt } else {
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt printf("disallowed type %s != %s\n", $type, $typeallowed);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $result = 0;
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt }
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $reply = pack('N', $result);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt $client->send($reply);
c23a9eed3ea86d92f42ef7296b9eeee1b54bb851Evan Hunt}