HEX
Server: Apache
System: Linux 26.184.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64
User: hyderabadrubberp (1048)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //scripts/cpdig
#!/usr/local/cpanel/3rdparty/bin/perl

#                                      Copyright 2026 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package scripts::cpdig;

use cPstrict;

=encoding utf-8

=head1 NAME

cpdig

=head1 USAGE

    cpdig <name> <type> [--verbose]

=head1 DESCRIPTION

This script performs a DNS query using cPanel’s DNS resolver.
Its output should yield the same end results as C<dig +trace $name $type>.

cPanel provides this script solely for diagnostic purposes; no cPanel
& WHM feature requires its use.

=cut

use parent qw( Cpanel::HelpfulScript );

use Cpanel::DNS::Rcodes  ();
use Cpanel::DNS::Unbound ();
use Cpanel::NameServers  ();

use constant _OPTIONS        => ('verbose');
use constant _ACCEPT_UNNAMED => 1;

exit( __PACKAGE__->new(@ARGV)->run() // 0 ) unless caller;

sub run {
    my ($self) = @_;

    my ( $name, $type ) = $self->getopt_unnamed();
    die $self->help() if grep { !$_ } $name, $type;

    $type = uc($type);

    if ( $type eq 'NS' ) {

        # requesting DNS::Unbound is not complete when the NS are not setup correctly

        my $list = Cpanel::NameServers::get_nameservers_for_domain($name) // [];

        if (@$list) {
            say $_ for @$list;

            return;
        }
    }

    my $dns = Cpanel::DNS::Unbound->new();

    my $ret = $dns->recursive_queries( [ [ $name, $type ] ] )->[0];

    if ( $self->getopt('verbose') ) {
        print STDERR $ret->{debug};
    }

    # Report query execution errors (timeouts, resolve failures).
    die $ret->{'error'} if $ret->{'error'};

    # Report DNS-level errors (SERVFAIL, REFUSED, etc.).
    my $rcode = $ret->{'result'}{'rcode'};
    if ( $rcode > 0 ) {
        my $rcode_name = Cpanel::DNS::Rcodes::RCODES()->[$rcode] // "RCODE_$rcode";
        say STDERR "DNS error: $rcode_name ($name $type)";

        my $has_data = $ret->{'decoded_data'} && @{ $ret->{'decoded_data'} }
          || $ret->{'result'}{'data'} && @{ $ret->{'result'}{'data'} };

        # Fatal when no data and not NXDOMAIN — nothing useful to show.
        return 1 if !$has_data && !$ret->{'result'}{'nxdomain'};
    }

    my $data_ar = $ret->{'decoded_data'} || $ret->{result}{data};

    my %FORMATTERS = (
        'ARRAY' => sub { "@{$_[0]}" },
        'HASH'  => sub {
            join ' ', map { "$_=$_[0]->{$_}" } sort keys %{ $_[0] };
        }
    );

    foreach my $item ( @{$data_ar} ) {
        my $type = ref $item;

        if ( $type ne '' && defined $FORMATTERS{$type} ) {
            $self->_print( $FORMATTERS{$type}->($item) . "\n" );
        }
        else {
            $self->_print("$item\n");
        }
    }

    return;
}

1;