#!/usr/bin/perl
# ptr v4.2.0  (c) 20.2.2013 by Andreas Ley  (u) 27.10.2025
# Handle PTR records through NET-WebAPI

use warnings;
use strict;
no warnings 'uninitialized';
use sigtrap;
#use diagnostics;

# The script itself may use utf-8 encoded identifiers and literals
use utf8;
# Latin-1 codepoints are considered characters
use feature 'unicode_strings';
use locale;
# Enable UTF-8 encoding for all files (but not already open handles)
use open ':encoding(utf8)';

use Getopt::Long;
use File::Basename;
use File::Spec::Functions;
use NetAddr::IP ':lower';

use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
$Data::Dumper::Sortkeys = 1;
#warn Data::Dumper->Dump([\%hash],['*']) if ($opt_debug>0);
#warn Data::Dumper->new([\%hash],['*'])->Indent(0)->Dump if ($opt_debug>0);

my $config = catfile($ENV{'HOME'}||(getpwuid($<))[7],'.config','netdb_client.ini');

sub usage
{
	my $image = $0;
	$image =~ s!.*/!!;
	print  STDERR  "Usage: $image [-b ipv6-base] ip fqdn\n";
	print  STDERR  "Or:    $image  -b ipv6-base  ipv4\n";
	# "Die Behandlung der genannten Attribute bzw. Felder ist durch diese Funktion nicht erlaubt und daher durch die Anwendungslogik gesperrt."
	#print  STDERR  "Or:    $image                -c old-ip new-ip\n";
	print  STDERR  "Or:    $image [-b ipv6-base] -c ip new-fqdn\n";
	print  STDERR  "Or:    $image [-b ipv6-base] -d ip [...]\n";
	print  STDERR  "Or:    $image [-b ipv6-base] -l [-v] [ip|fqdn] [...]\n";
	print  STDERR  "-b, --base	Specify IPv6 base address; IPv4 addresses will be mapped\n";
	print  STDERR  "-c, --change	Change PTR record\n";
	print  STDERR  "-d, --delete	Delete PTR record\n";
	print  STDERR  "-l, --list	List PTR record(s)\n";
	print  STDERR  "-v, --verbose	Verbose mode\n";
	print  STDERR  "Your NET-WebAPI-Key and -Certificate locations must be specified in environment variables NET_WEBAPI_KEY and NET_WEBAPI_CERT or your NetVS-Token must be readable from $config\n" unless (-r $config || defined($ENV{'NET_WEBAPI_KEY'}) && -r $ENV{'NET_WEBAPI_KEY'} && defined($ENV{'NET_WEBAPI_CERT'}) && -r $ENV{'NET_WEBAPI_CERT'});
	exit(1);
}

my %opt;
@_ = @ARGV;
$Getopt::Long::ignorecase = 0;
GetOptions (\%opt,'debug|D:s','help|h','trace|x','verbose|v','dry-run|n','devel|N',
	'change|c','delete|d','list|l','base|b=s');
exec($^X,'-d:Trace',$0,@_) if (defined($opt{'trace'}) && !defined($Devel::Trace::TRACE));

&usage() if (defined($opt{'help'}) ||
	(defined($opt{'change'}) && (defined($opt{'delete'}) || defined($opt{'list'})) || defined($opt{'delete'}) && defined($opt{'list'})) ||
	(defined($opt{'dry-run'}) && defined($opt{'devel'})) ||
	@ARGV < (defined($opt{'change'}) ? 1 : 0));
&usage() unless (-r $config || defined($ENV{'NET_WEBAPI_KEY'}) && -r $ENV{'NET_WEBAPI_KEY'} && defined($ENV{'NET_WEBAPI_CERT'}) && -r $ENV{'NET_WEBAPI_CERT'});

if (defined($opt{'verbose'}) || defined($opt{'debug'})) {
	$|=1;
	select((select(STDERR),$|=1)[0]);
}

my (%debug,$on,$off);

sub debug
{
	my $file = shift;
	my $line = shift;
	my $prefix = defined($debug{'path'})?$file:defined($debug{'file'})?basename($file):'';
	if (defined($debug{'line'})) {
		$prefix .= ':' if (length($prefix));
		$prefix .= $line;
	}
	$prefix .= '  ' if (length($prefix));
	warn $prefix.(scalar(@_)>1 ? join(', ',map("\"$_\"",@_))."\n" : "@_\n");
}
$debug{undef} = \&debug;

if (defined($opt{'debug'})) {
	for (split(',',$opt{'debug'})) {
		if (/=/) {
			$debug{$`} = $';
		}
		else {
			$debug{$_} = 1;
		}
	}
	&debug(__FILE__,__LINE__,'%debug = '.Data::Dumper->Dump([\%debug],['*debug'])) if ($debug{'debug'}>0);
	if (defined($ENV{'TERM'})) {
		use Term::Cap;
		my $term = Tgetent Term::Cap { 'OSPEED'=>9600 };
		$on = $term->Tputs('md');
		$off = $term->Tputs('me');
	}
}

#$IO::Socket::SSL::DEBUG = $debug{'ssl'} if (defined($debug{'ssl'}));

# Enable UTF-8 encoding for already open handles
# ":utf8" would enable perl native coding which happens to be UTF-8 only on ASCII platforms
binmode(STDIN,':encoding(utf8)');
binmode(STDOUT,':encoding(utf8)');
binmode(STDERR,':encoding(utf8)') unless (defined($Devel::Trace::TRACE));

####################################################################################################################################

if (defined($opt{'devel'})) {
	use lib '.';
}
use NET::WebAPI ('ip','is_ip');

my $base = &ip($opt{'base'},'IPv6 base') if (defined($opt{'base'}));

my $opt = NET::WebAPI->read_config($config) if (-s $config);
$opt->{'key'} = $ENV{'NET_WEBAPI_KEY'} if (defined($ENV{'NET_WEBAPI_KEY'}));
$opt->{'cert'} = $ENV{'NET_WEBAPI_CERT'} if (defined($ENV{'NET_WEBAPI_CERT'}));
$opt->{'debug'} = \%debug;
$opt->{'url'} = 'test' if (defined($opt{'dry-run'})); 
$opt->{'url'} = 'devel' if (defined($opt{'devel'}));

my $webapi = NET::WebAPI->new($opt) or die "Can't connect to WebAPI\n";

# Change
if (defined($opt{'change'})) {
	&usage() if (@ARGV != 2);
	$webapi->update_ptr(&four2six(&ip(shift)),shift);
}

# Delete
elsif (defined($opt{'delete'})) {
	for my $ip (@ARGV) {
		$webapi->delete_ptr(&four2six(&ip($ip)));
	}
}

# List
elsif (defined($opt{'list'})) {
	for my $arg (@ARGV) {
		if (&is_ip($arg)) {
			$arg = &four2six(&ip($arg));
			for my $fqdn ($webapi->list_ptr($arg)) {
				print $arg->short()," => " if (defined($opt{'verbose'}));
				print $fqdn,"\n";
			}
		}
		else {
			#&usage() if (defined($base));
			for my $ip ($webapi->list_ptr($arg)) {
				print $ip->short();
				print " => $arg" if (defined($opt{'verbose'}));
				print "\n";
			}
		}
	}
}

# Create
else {
	my $ip = &ip(shift);
	if (@ARGV) {
		&usage() if (@ARGV > 1);
		$webapi->create_ptr(&four2six($ip),@ARGV);
	}
	else {
		&usage() unless (defined($base));
		&usage() if ($ip->version() == 6);
		$webapi->create_ptr(&four2six($ip),$webapi->list_ptr($ip));
	}
}

sub four2six
{
	&debug(__FILE__,__LINE__,'+ &four2six'.Data::Dumper->new([\@_],['*'])->Indent(0)->Dump) if ($debug{'call'}>1 || $debug{'four2six'}>0);
	my ($ip) = @_;

	if (defined($base)) {
		my $numeric = scalar($ip->numeric());
		# We only have 32 bit signed(!) arithmetics
		$ip = $base+int($numeric/2)+($numeric-int($numeric/2));
	}

	&debug(__FILE__,__LINE__,'+ &four2six = '.Data::Dumper->new([\$ip],['*'])->Indent(0)->Dump) if ($debug{'return'}>1 || $debug{'four2six'}>1);
	return ($ip);
}
