File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/restartsrv_named
#!/usr/bin/perl
# cpanel - restartsrv_named 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 RestartSrv;
use Cpanel::OSSys ();
use Cpanel::CleanupStub ();
if ( -e '/var/cpanel/usensd' ) {
exec '/scripts/restartsrv_nsd', @ARGV;
}
eval {
local $SIG{'__DIE__'};
require Cpanel::Rlimit;
Cpanel::Rlimit::set_rlimit_to_infinity();
};
setuppath();
my ( $system, $nodename, $release, $version, $machine ) = Cpanel::OSSys::uname();
my ( $restart, $check, $status ) = parseargv();
my $iscript = '';
my $initdir = '';
my $service = 'named';
my $processowner = 'root';
my $overridercconf = '';
my $manualstart = 0;
my $servicebin = '';
my $serviceflags = '';
my $disabled = 0;
( $processowner, $service, $iscript, $manualstart, $servicebin, $serviceflags, $disabled ) = servicefixup( $system, $service );
if ( $restart && !$disabled ) {
my $lock_file = '/var/run/restartsrv_named.lock';
lock_file($lock_file);
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
}
else {
Cpanel::OSSys::setsid();
Cpanel::CleanupStub::daemonclosefds();
if ( $manualstart != 1 ) {
check_init_script($iscript);
nooutputsystem( $iscript, 'stop' );
doomedprocess($service);
if ( $restart != -1 ) {
system( $iscript, 'start' );
}
}
if ( $manualstart == 1 ) {
if ( $system =~ m/freebsd/i ) {
nooutputsystem( 'ndc', 'halt' );
doomedprocess($service);
nooutputsystem( $servicebin, $serviceflags );
}
else {
nooutputsystem( 'ndc', 'halt' );
doomedprocess($service);
if ( $restart != -1 ) {
system( '/usr/sbin/named', '-u', 'named' );
}
}
}
exit;
}
unlink $lock_file if -e $lock_file;
}
elsif ($status) {
#--status (show ps)
if ($disabled) {
print "$service is disabled\n";
}
else {
print check_service( 'service' => $service, 'user' => $processowner );
}
}
elsif ($check) {
exit if ($disabled);
require Cpanel::PwCache;
require Cpanel::NameServer::Utils::BIND;
my $namedconf = Cpanel::NameServer::Utils::BIND::find_namedconf();
my @PATH = split( /\//, $namedconf );
pop @PATH;
my $conf_dir = join( '/', @PATH );
my ( $chrootdir, $binduser, $bindgroup ) = Cpanel::NameServer::Utils::BIND::find_chrootbinddir();
my $binduid = ( Cpanel::PwCache::getpwnam($binduser) )[2];
my $bindgid = ( getgrnam($bindgroup) )[2];
if ( $binduid && $bindgid ) {
my @confs;
foreach my $file ( $namedconf, $conf_dir . '/rndc.key', $conf_dir . '/rndc.conf' ) {
if ( -e $file && !-l $file ) {
push @confs, $file;
}
if ($chrootdir) {
if ( -e $chrootdir . '/' . $file && !-l $chrootdir . '/' . $file ) {
push @confs, $chrootdir . '/' . $file;
}
}
}
foreach my $conf (@confs) {
$conf =~ s/\/+/\//g;
my ( $confuid, $confgid ) = ( stat($conf) )[ 4, 5 ];
if ( $confuid != $binduid || $confgid != $bindgid ) {
chown $binduid, $bindgid, $conf;
print "Fixed ownership on $conf\n"; #print something so named gets restarted
}
}
}
if ( check_service( 'service' => $service, 'user' => $processowner ) eq '' ) {
print "$service is not running\n";
}
}
sub check_init_script {
my ($bind_init_file) = @_;
my $flag_file = '/var/cpanel/version/named_init_update';
return unless ( -e $bind_init_file );
my $init_mtime = ( stat(_) )[9];
if ( -e $flag_file ) {
my $flag_mtime = ( stat(_) )[9];
return if ( $flag_mtime == $init_mtime );
}
if ( open my $init_fh, '+<', $bind_init_file ) {
my @initd_named;
# Flag changes for write
my $removed_checkzone_option = 0;
LINE:
while ( my $line = readline $init_fh ) {
# Check for checkzone option during restarts
if ( !$removed_checkzone_option && $line =~ m/^\s*ckcf_options=/ && ( $line =~ m/^(\s*)ckcf_options=["']([^'"]+)["'](.*)/ || $line =~ m/^(\s*)ckcf_options=([^"'\s;]+)(.*)/ ) ) {
my ( $line_beginning, $options, $line_ending ) = ( $1, $2, $3 );
# Only concerned if '-z' option is present
if ( $options =~ m/\-z/ ) {
$options =~ s/^["']//;
$options =~ s/["']$//;
my @options_array = split( /\s+/, $options );
my $options_count = scalar @options_array;
@options_array = grep { $_ ne '-z' } @options_array;
# Check to see if '-z' was removed explicitly as we may end up here when the option was '-zwhatever'
if ( $options_count != scalar @options_array ) {
$removed_checkzone_option = 1;
chomp $line_ending;
push @initd_named, $line_beginning . q{ckcf_options='} . join( ' ', @options_array ) . q{'} . $line_ending . "\n";
# next LINE; # Explicitly state we are done with the line
}
}
else {
push @initd_named, $line;
# next LINE; # Explicitly state we are done with the line
}
}
else {
push @initd_named, $line;
# next LINE; # Explicitly state we are done with the line
}
}
# Only write the file if we've changed something
if ($removed_checkzone_option) {
seek( $init_fh, 0, 0 );
print {$init_fh} join( '', @initd_named );
truncate( $init_fh, tell($init_fh) );
}
close $init_fh;
# Update the flag file and init file for the same time
system( 'touch', '-r', $bind_init_file, $flag_file );
}
else {
warn "Failed to open $bind_init_file for read write: $!";
}
return;
}