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: //scripts.20110531.215904.25158/nsdup
#!/usr/bin/perl
# cpanel - nsdup                                  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', '/scripts'; }

use strict;
use warnings;
use Cpanel::Config      ();
use Cpanel::RpmStor     ();
use Cpanel::SafeRun     ();
use Cpanel::RpmUtils    ();
use Cpanel::HttpRequest ();
use Cpanel::Update      ();
use Cpanel::Sys::OS     ();
use cPpkgversions       ();
use IPC::Open3          ();
use Cpanel::OSSys       ();
use Fcntl               ();
use Cpanel::SysPkgs     ();
use Cpanel::Usage       ();

Cpanel::Update::safe_update_environment('nsd');

my $force = 0;
my $src   = 0;

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

my ( $system, $machine ) = ( Cpanel::OSSys::uname() )[ 0, 4 ];

### Check for systems where NSD isn't supported
if ( -e '/etc/gentoo-release' || -e '/etc/debian-release' ) {
    die "NSD is not currently supported on this operating system";
}

# Check that NSD is the configured nameserver
my $cpconf_ref = Cpanel::Config::loadcpconf();

if ( !defined $cpconf_ref->{'local_nameserver_type'} || $cpconf_ref->{'local_nameserver_type'} ne 'nsd' ) {
    print "NSD is not the configured local nameserver.\n";
    print "Use /scripts/setupnameserver to change this setting.\n";
    exit 0;
}

if ( $> != 0 ) {
    die "Insufficient permissions to update NSD";
}

### Setup worker object and initilize variables
Cpanel::Update::init_UP_update('nsd');

my $httpClient = Cpanel::HttpRequest->new( 'hideOutput' => 0 );
my $updated = 0;
$force ||= check_current_install();

print "NSD Setup Script Version 1.0\n";

if ( -x "/scripts/prensdup" ) {
    system("/scripts/prensdup");
}

$updated = ( $system =~ /freebsd/i ) ? freebsd_install( 'nsd', $force, $src ) : linux_install( 'nsd', $force, $src );

chdir('/');
if ( -e '/home/cpanel-nsd-install' ) {
    system 'rm', '-rf', '/home/cpanel-nsd-install';
}

system( '/scripts/cpservice', 'nsd', 'install' ) if ($updated);

system('/scripts/checknsddirs');

# Setup conf file from named.conf
system('/scripts/buildnsdconf');

# Start NSD
system('/scripts/restartsrv_nsd') if $updated;

if ( -x "/scripts/postnsdup" ) {
    system("/scripts/postnsdup");
}

print "Install Complete\n";

# Checks to see if the current installation is corrupted and needs to
# be reinstalled.  The return value from this will be the default for $force
sub check_current_install {
    return 0;
}

# Determine the distro name, version and package arch.
# This logic is reused in most of the up scripts and should
# be merged together at some point.  See FogBugz Case 4789.
sub get_distro_id {
    my $machine = shift;
    my $distro;
    my $arch = 'i386';
    my $version;
    my $ises = 0;

    if ( -e "/etc/trustix-release" ) {
        $distro = 'trustix';
        ($version) = Cpanel::Sys::OS::getversionfromfile('/etc/trustix-release');
        $arch = 'i586';
    }
    elsif ( -e "/etc/caos-release" ) {
        $distro = 'caos';
        ($version) = Cpanel::Sys::OS::getversionfromfile('/etc/caos-release');
    }
    elsif ( -e "/etc/SuSE-release" ) {
        $distro = 'suse';
        ($version) = Cpanel::Sys::OS::getversionfromfile('/etc/SuSE-release');
        if ( $version >= 9 ) { $arch = 'i586'; }
    }
    elsif ( -e "/etc/fedora-release" ) {
        $distro = 'fedora';
        ($version) = Cpanel::Sys::OS::getversionfromfile('/etc/fedora-release');
    }
    elsif ( -e "/etc/mandrake-release" ) {
        $distro = 'mandrake';
        ($version) = Cpanel::Sys::OS::getversionfromfile('/etc/mandrake-release');
        $arch = 'i586';
    }
    elsif ( -e "/etc/whitebox-release" ) {
        $distro = 'whitebox';
        ($version) = Cpanel::Sys::OS::getversionfromfile('/etc/whitebox-release');
    }
    elsif ( -e "/etc/redhat-release" ) {
        $distro = 'redhat';
        ( $version, $ises ) = Cpanel::Sys::OS::getversionfromfile('/etc/redhat-release');
    }

    if ( $ises == 1 ) {
        $version = "AS-${version}";
    }
    if ( $ises == 2 ) {
        $distro = "centos";
    }
    if ( $ises == 3 ) {
        $distro = "whitebox";
    }

    if ( $machine =~ /64/ ) {
        $arch = 'x86_64';
    }
    return ( $distro, $version, $arch );
}

# This could also be generalized so that all of the up scripts use the same function
# See FogBugz case 4790
sub buildrpm {
    my $srpm_url = shift;
    my $rpm_file;

    # Only need flex and gcc
    my $syspkgobj = Cpanel::SysPkgs->new();
    if ( !$syspkgobj ) { die print "Could not create SysPkgs object\n"; }
    my @pkgs = ( "rpm", "rpm-build", "flex", "gcc", "glibc-devel" );
    $syspkgobj->install( 'pkglist' => \@pkgs );

    my $rpm = 'rpm';
    if ( -e "/usr/bin/rpmbuild" ) { $rpm = '/usr/bin/rpmbuild'; }

    $httpClient->download( $srpm_url, "/home/cpanel-nsd-install/nsd.src.rpm" );

    open( RNULL, "<", "/dev/null" );
    print "Building RPM...";
    open( RPM, ">", "rpmbuildlog" );
    my $pid = IPC::Open3::open3( "<&RNULL", ">&RPM", ">&RPM", "$rpm", "--rebuild", "/home/cpanel-nsd-install/nsd.src.rpm" );
    while ( waitpid( $pid, 1 ) <= 0 ) {
        print ".";
        sleep(1);
    }
    close(RPM);
    close(RNULL);
    print "Done\n";

    open( RPM, "<", "rpmbuildlog" );
    while (<RPM>) {
        if (/^Wrote: (\S+)/) {
            $rpm_file = $1;
            if ( $rpm_file =~ /.(64|86)\.rpm/ ) {
                Cpanel::RpmStor::rpmstor($rpm_file);
                return ($rpm_file);
            }
        }
    }
    close(RPM);

    open( RPM, "<", "rpmbuildlog" );
    seek( RPM, -2048, &Fcntl::SEEK_END );
    while (<RPM>) {
        print "\t" . $_;
    }
    close(RPM);

    die "Failed to build rpm";
}

sub freebsd_install {
    my $pkg   = shift;
    my $force = shift;
    my $src   = shift;    # ignored on FreeBSD
    system("/scripts/checkmakeconf");
    if ($force) {
        unlink("/var/cpanel/lastportsup");
    }
    system("/scripts/portsup");
    $pkg = 'nsd';

    check_port_options();

    my $result;
    my (@command) = ( '/scripts/ensurepkg', '--useport', $pkg );
    if ($force) {
        @command = ( '/scripts/ensurepkg', '--useport', '--force', $pkg );
    }
    open( RNULL, "<", "/dev/null" );
    IPC::Open3::open3( "<&RNULL", \*INS, \*INS, @command );
    while (<INS>) {
        print;
        $result .= $_;
    }
    close(INS);
    close(RNULL);

    my $updated = 0;
    if (   $result !~ /(^|(?<!Checking if)\s)\s*\S*${pkg}.*\salready\s+installed/m
        && $result !~ /${pkg}.*\sis\s+installed/m
        && $result !~ /${pkg}.*\sis\s+newer/m ) {
        $updated = 1;
    }
    return $updated;
}

sub check_port_options {
    my $options_dir = '/var/db/ports/nsd';
    unless ( -d $options_dir ) {
        system( 'mkdir', '-p', $options_dir );
    }
    if ( -e $options_dir . '/options' && open( my $options_fh, '+<', $options_dir . '/options' ) ) {
        my @options = readline($options_fh);
        unless ( grep( /^\s*WITH_MAXINT=true/, @options ) ) {
            @options = grep( !/^\s*WITH(OUT)?_MAXINT/, @options );
            push @options, "WITH_MAXINT=true\n";
            seek $options_fh, 0, 0;
            print $options_fh @options;
            truncate $options_fh, tell($options_fh);
        }
        close $options_fh;
    }
    else {
        if ( open( my $options_fh, '>', $options_dir . '/options' ) ) {
            print $options_fh "WITH_MAXINT=true\n";
            close $options_fh;
        }
    }
}

sub linux_install {
    my $pkgname = shift;
    my $force   = shift;
    my $src     = shift;
    my $updated = 0;

    my %VINFO = cPpkgversions::getpkgversioninfo('nsd');

    my ( $distro, $version, $arch ) = get_distro_id($machine);
    my $nsd_version = $VINFO{$pkgname}{'version'};
    my $nsd_release = $VINFO{$pkgname}{'release'};

    my $current_package_string = Cpanel::SafeRun::saferunnoerror(qw{rpm -q nsd});
    chomp($current_package_string);

    print "This is the NSD ${nsd_version} installer release number ${nsd_release} for platform ${distro} ${version}\n";

    if ( $current_package_string eq "nsd-${nsd_version}-${nsd_release}" && !$force ) {
        print "NSD is up to date.  Use nsdup --force to force a reinstall\n";
    }
    else {
        print "Resetting NSD to cPanel Defaults and Installing the Latest Version\n";

        ### Fetch or build RPM
        my $rpm_file  = "/nsd-${nsd_version}-${nsd_release}.${arch}.rpm";
        my $srpm_file = "/nsd-${nsd_version}-${nsd_release}.src.rpm";
        my $rpm_url   = "http://httpupdate.cpanel.net/nsdinstall/${nsd_version}-${nsd_release}" . ( $machine =~ /64/ ? '/64/' : '/' ) . "${distro}/${version}/${rpm_file}";
        my $srpm_url  = "http://httpupdate.cpanel.net/nsdinstall/${nsd_version}-${nsd_release}/src/${srpm_file}";

        mkdir '/home/cpanel-nsd-install', oct(700);
        chdir '/home/cpanel-nsd-install';

        if ( !$src ) {
            $httpClient->download( $rpm_url, "/home/cpanel-nsd-install/${rpm_file}" );
            my $filesize = ( stat("/home/cpanel-nsd-install/${rpm_file}") )[7] || 0;
            if ( $filesize <= 0 ) {
                print "Failed to download ${rpm_url} .. using source instead\n";
                $src = 1;
            }
            else {
                $rpm_file = "/home/cpanel-nsd-install/${rpm_file}";
            }
        }

        if ($src) {
            $rpm_file = buildrpm($srpm_url);
            if ( $rpm_file eq '' ) {
                die "Rpm failed to build!";
            }
        }

        if ( ( stat($rpm_file) )[7] <= 0 ) {
            die "Failed to download or build rpm from ${rpm_url} (${rpm_file})";
        }

        if ( !Cpanel::RpmUtils::checkrpm($rpm_file) ) {
            die "$rpm_file is not a valid rpm file";
        }

        ### Install RPM
        $ENV{'RPMINSTALL'} = 1;
        Cpanel::RpmUtils::rpmpreinstall($rpm_file);

        # Install rpm
        system( 'rpm', '-Uvh', '--nodeps', '--force', $rpm_file );

        $ENV{'RPMINSTALL'} = 0;
        $updated = 1;
    }
    return $updated;
}

sub usage {
    print <<EO_USAGE;
Usage: nsdup [options]

    Options:
      --help       Brief help message
      --force      Perform update even if current installed version is up to date
      --source     On RPM systems, force a rebuild of the SRPM rather than installing
                   precompiled binary RPMs
EO_USAGE
    exit 0;
}