MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/setupnameserver
#!/usr/bin/perl
# cpanel - setupnameserver                        Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN { unshift @INC, '/usr/local/cpanel'; }

use strict;
use Cpanel::Config    ();
use Cpanel::Chkservd  ();
use Cpanel::Sys::OS   ();
use Cpanel::SysPkgs   ();
use Cpanel::Services  ();
use Cpanel::Init      ();
use Cpanel::Usage     ();
use Cpanel::PID       ();
use Cpanel::Update    ();
use Cpanel::Resolvers ();
use Cpanel::DNSONLY   ();

my $force   = 0;
my $current = 0;

delete $ENV{'cp_security_token'};
delete $ENV{'HTTP_REFERER'};

# Argument processing
my %opts = (
    'force'   => \$force,
    'current' => \$current,
);

Cpanel::Usage::wrap_options( \@ARGV, \&usage, \%opts );

@ARGV = ( grep( !/^--/, @ARGV ) );

my $dnstype = shift;
usage() unless ( $dnstype || $current );

my $cpconf_ref = Cpanel::Config::loadcpconf();

$cpconf_ref->{'local_nameserver_type'} ||= 'bind';
my $current_nameserver = Cpanel::Services::is_enabled('dns') ? $cpconf_ref->{'local_nameserver_type'} : 'disabled';

if ($current) {
    print "Current nameserver type: $current_nameserver\n";
    exit 0;
}

if ( !$force && $current_nameserver eq $dnstype ) {
    print "Already configured.\n";
    exit 0;
}

unless ( $dnstype eq 'bind' || $dnstype eq 'nsd' || $dnstype eq 'disabled' ) {
    print "Unknown nameserver type specified.\nTry $0 --help\n";
    exit 1;
}

my ( $valid, $reason ) = valid_nameserver_type($dnstype);
unless ($valid) {
    print "The specified nameserver type is not available for your system:\n";
    print $reason;
    exit 1;
}

if ( $> != 0 ) {
    die "Conversion process must be performed as root";
}

my $pid_obj = Cpanel::PID->new( { 'pid_file' => '/var/run/setupnameserver.pid' } );
unless ( $pid_obj->create_pid_file() > 0 ) {
    print "Setupnameserver appears to be running already.\n";
    print "Please wait for the conversion to finish before attempting another.\n";
    exit 1;
}

$cpconf_ref->{'local_nameserver_type'} = $dnstype if $dnstype ne 'disabled';
Cpanel::Config::savecpconf($cpconf_ref);
my $init = Cpanel::Init->new();

install_initscripts();
disable_chksrvd_monitoring();

#branch to uninsall/install functions
if ( $dnstype eq 'nsd' ) {
    disable_none();
    disable_bind();
    enable_nsd();
    enable_chksrvd_monitoring();
}
elsif ( $dnstype eq 'bind' ) {
    disable_none();
    disable_nsd();
    enable_bind();
    enable_chksrvd_monitoring();
}
else {
    enable_none();
    disable_bind();
    disable_nsd();
}

$pid_obj->remove_pid_file();
exit 0;

sub valid_nameserver_type {
    my $dnstype = shift;

    # The only invalid type currently is NSD on FreeBSD, Gentoo or Debian
    if ( $dnstype eq 'nsd' ) {
        if ( Cpanel::DNSONLY::enabled() ) {
            return ( 0, "NSD is not supported on DNS ONLY systems\n" );
        }
        my $distro = Cpanel::Sys::OS::getos();
        if ( $distro eq 'gentoo' || $distro eq 'debian' ) {
            return ( 0, "NSD is not supported on this distro.\n" );
        }
        if ( Cpanel::Resolvers::requires_caching_nameserver() ) {
            return ( 0, "NSD does not act as a caching nameserver and /etc/resolv.conf includes a local IP address.\n" );
        }
    }

    return ( 1, 'Ok' );
}

sub disable_bind {
    print "\nHalting BIND\n";
    my $output = $init->run_command( 'named', 'stop' );
    print "\nDisabling BIND in init system\n";
    $output = $init->run_command_for_one( 'disable', 'named' );
}

sub disable_nsd {
    return unless valid_nameserver_type('nsd');
    print "\nHalting NSD\n";
    my $output = $init->run_command( 'nsd', 'stop' );
    print "\nDisabling NSD in init system\n";
    $output = $init->run_command_for_one( 'disable', 'nsd' );
    unlink('/var/cpanel/usensd') if ( -e '/var/cpanel/usensd' );
    print "\nDisabling NSD updates\n";
    my $updateconf = Cpanel::Update::loadupdateconfig();
    $updateconf->{'NSDUP'} = 'never';
    Cpanel::Update::saveupdateconfig($updateconf);

}

sub disable_none {
    unlink('/etc/nameddisable') if ( -e '/etc/nameddisable' );
    unlink('/etc/binddisable')  if ( -e '/etc/binddisable' );
    unlink('/etc/nsddisable')   if ( -e '/etc/nsddisable' );

}

# Strictly speaking, touching /etc/nameddisable will cause chksrvd to skip over
# restarting BIND or NSD, but this will stop it from even polling for these processes
sub disable_chksrvd_monitoring {
    my %monitored_services = Cpanel::Chkservd::getmonitored();
    return unless ( $monitored_services{'named'} );
    Cpanel::Chkservd::disable('named');
    system('/scripts/restartsrv_chksrvd');
}

sub enable_bind {
    install_bind();
    print "\nEnabling BIND in init system\n";
    my $output = $init->run_command_for_one( 'enable', 'named' );

    # Setup rndc
    print "\nSetting up rndc configuration\n";
    system( '/scripts/fixrndc', '-f' );

    print "\nStarting BIND\n";
    $output = $init->run_command( 'named', 'start' );
}

sub enable_nsd {
    print "\nChecking that NSD is up to date\n";
    my $updateconf      = Cpanel::Update::loadupdateconfig();
    my %orig_updateconf = %{$updateconf};
    $updateconf->{'NSDUP'} = 'daily';
    $updateconf->{'SYSUP'} = 'daily';
    Cpanel::Update::saveupdateconfig($updateconf);
    system( 'touch', '/var/cpanel/usensd' );
    my @nsdup = ('/scripts/nsdup');
    push @nsdup, '--force' if ($force);
    system(@nsdup);
    print "\nEnabling NSD in init system\n";
    my $output = $init->run_command_for_one( 'enable', 'nsd' );
    print "\nStarting NSD\n";
    $output = $init->run_command( 'nsd', 'start' );
    print "\nEnabling NSD updates\n";
    $orig_updateconf{'NSDUP'} = 'inherit';
    Cpanel::Update::saveupdateconfig( \%orig_updateconf );
}

sub enable_none {
    system( 'touch', '/etc/nameddisable' );
}

# Both BIND and NSD are monitored by chksrvd using restartsrv_named
sub enable_chksrvd_monitoring {
    my %monitored_services = Cpanel::Chkservd::getmonitored();
    return if ( $monitored_services{'named'} );
    Cpanel::Chkservd::enable('named');
    system('/scripts/restartsrv_chksrvd');
}

# This is legacy code from whostmgr.pl newnameserver()
# The installer pulls in the BIND packages on most systems, but additional setup is
# requires on BSD and Gentoo.  It may be preferrable to move all of the BIND
# install logic here or move this logic to the installer.
sub install_bind {
    my $distro = Cpanel::Sys::OS::getos();
    return unless ( $distro eq 'freebsd' || $distro eq 'gentoo' );

    print "\nEnsuring caching-nameserver is installed\n";
    my $syspkgobj = Cpanel::SysPkgs->new();
    if ( !$syspkgobj ) { die print "Could not create SysPkgs object\n"; }
    my @pkgs = ("caching-nameserver");
    $syspkgobj->install( 'pkglist' => \@pkgs );

    if ( $distro eq 'freebsd' ) {
        if ( -e '/etc/namedb/make-localhost' ) {
            chdir '/etc/namedb';
            system( '/bin/sh', 'make-localhost' );
        }
    }
}

sub install_initscripts {
    print "Verifying that required init scripts are installed.\n";
    my $output = $init->run_command_for_one( 'install', 'nsd' );
    $output = $init->run_command_for_one( 'install', 'named' );
}

sub usage {
    print <<EO_USAGE;
setupnameserver [options] [nameserver type]

    Options:
      --help       Brief help message
      --force      Rerun configuration routines even if the selected
                   nameserver type is already configured
      --current    Show the currently selected nameserver type


    Nameserver Types:
      bind         Suggested.  Functions as both authoritative 
                   and caching nameserver.
      nsd          Lower memory.  Functions only as authoritative
                   nameserver.
      disabled     Disable the local nameserver.
EO_USAGE
    exit 0;
}