#!/usr/bin/perl
#
# Test resolver and authenticator, used for testing nnrpd external auth.
# Saves all of its input into a file named input in the current directory, as
# seen, and then produces output based on its command-line parameter.
#
# Originally written by Russ Allbery in shell in 2005.
# Rewritten in Perl by Julien Élie in 2026 because on OpenBSD, when the script
# kills itself with SIGUP, it doesn't exit like a process killed with SIGHUP
# and instead exits with some sort of weird 129 exit status.
#
# Various bug fixes, code and documentation improvements since then
# in 2008, 2020, 2021, 2026.

use strict;
use warnings;

# Save input.
{
    open(my $fh, '>', 'input') or die "Cannot open 'input' file: $!";
    while (<STDIN>) {
        print $fh $_;
    }
    close($fh);
}

# Choose output.
my $arg = defined($ARGV[0]) ? $ARGV[0] : '';

if ($arg eq 'okay') {
    print STDOUT "User:tester\r\n";

} elsif ($arg eq 'garbage') {
    print STDOUT "blah\n";
    print STDOUT "Use:blah\n";
    print STDOUT "Use:r:blahblahblah\r\n";
    print STDOUT "\n";
    print STDOUT "User:tester\r\n";
    print STDOUT "   User:wrong\r\n";

} elsif ($arg eq 'error') {
    print STDERR "This is an error\n";
    exit 1;

} elsif ($arg eq 'interspersed') {
    print STDERR "This is ";
    print STDOUT "Use";
    print STDERR "an er";
    print STDOUT "r:test";
    print STDERR "ror";
    print STDOUT "er\r";
    print STDERR "\n";
    print STDOUT "\n";

} elsif ($arg eq 'empty') {
    # Nothing to do!

} elsif ($arg eq 'empty-error') {
    exit 1;

} elsif ($arg eq 'okay-error') {
    print STDOUT "User:tester\r\n";
    exit 1;

} elsif ($arg eq 'signal') {
    kill('HUP', $$);

} elsif ($arg eq 'newline') {
    print STDOUT "User:tester\n";

} elsif ($arg eq 'partial') {
    print STDOUT "User:tester";

} elsif ($arg eq 'partial-close') {
    close(STDERR);
    print STDOUT "User:tester";

} elsif ($arg eq 'partial-error') {
    print STDOUT "User:tester";
    print STDERR "This is an error\n";
    exit 1;
}

exit 0;
