#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - bin/check_nsd_badzones                  Copyright 2022 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use Cpanel::NameServer::Conf::NSD::BadZones ();
use Cpanel::Sys::Hostname                   ();
use Cpanel::Config::LoadCpConf              ();
use Cpanel::Notify                          ();
use Cpanel::Usage                           ();
use Cpanel::Services::Enabled               ();
use Cpanel::SafeRun::Errors                 ();
use Cpanel::Redirect                        ();
use Cpanel::IP::Remote                      ();

my $verbose = 0;

Cpanel::Usage::wrap_options( \@ARGV, \&usage, { 'verbose' => \$verbose } );

my $hostname   = Cpanel::Sys::Hostname::gethostname();
my $cpconf_ref = Cpanel::Config::LoadCpConf::loadcpconf_not_copy();

unless ( Cpanel::Services::Enabled::is_enabled('dns') ) {
    print "Local DNS services are currently disabled.\n" if $verbose;
    exit 0;
}

unless ( defined $cpconf_ref->{'local_nameserver_type'} && $cpconf_ref->{'local_nameserver_type'} eq 'nsd' ) {
    print "BIND is being used for the local DNS server.\n" if $verbose;

    exit 0;    # -- comment this line to test
}

my $badzones_obj = Cpanel::NameServer::Conf::NSD::BadZones->new();
my $badzones_ref = $badzones_obj->get_badzones();

# $badzones_ref = {'dog.org'=>1}; # -- uncomment to test

if ( scalar keys %{$badzones_ref} == 0 ) {
    print "No zones are marked as corrupt on this system.\n" if $verbose;
    exit 0;
}

my ( $changed, $current_badzones_ref ) = $badzones_obj->expire_badzones();
if ($changed) {
    if ($verbose) {
        print "Some of the zones that were previously marked as invalid appear to have changed.\n";
        print "NSD will be restarted to retest these zones.\n";
    }
    $badzones_obj->save_badzones($current_badzones_ref);
    my $output = Cpanel::SafeRun::Errors::saferunallerrors('/usr/local/cpanel/scripts/restartsrv_named');
    print $output if $verbose;

    # Refresh the badzones list to see if any remain
    $badzones_ref = $badzones_obj->get_badzones();
    if ( scalar keys %{$badzones_ref} == 0 ) {
        print "All zones pass syntax check.\n" if $verbose;
        exit 0;
    }
}

my $url_host = Cpanel::Redirect::getserviceSSLdomain('cpanel') || $hostname;
require Cpanel::Notify;
my $ic_obj = Cpanel::Notify::notification_class(
    'class'            => 'NSD::BadZones',
    'application'      => 'NSD::BadZones',
    'constructor_args' => [
        'origin'            => 'check_nsd_zones',
        'source_ip_address' => Cpanel::IP::Remote::get_current_remote_ip(),
        'url_host'          => $url_host,
        'hostname'          => $hostname,
        'corrupt_zones'     => [ sort keys %{$badzones_ref} ],
        'skip_send'         => 1,
    ]
);

if ($verbose) {
    print $ic_obj->render_template_include_as_text( 'template' => 'body', 'type' => 'html' );
}

$ic_obj->send();

sub usage {
    print <<EO_USAGE;
check_nsd_badzones [options]

    Options:
      --help       Brief help message
      --verbose    Display interactive output
EO_USAGE
    exit 0;
}
