File: //scripts.20110531.215904.25158/dovecotup
#!/usr/bin/perl
# cpanel - dovecotup 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::RpmStor ();
use Cpanel::SafeRun ();
use Cpanel::RpmUtils ();
use Cpanel::HttpRequest ();
use Cpanel::Update ();
use Cpanel::Sys::OS ();
use Cpanel::Proc ();
use Cpanel::Init ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::AdvConfig::dovecot::utils ();
use Cpanel::FileUtils::TouchFile ();
use cPpkgversions ();
use IPC::Open3 ();
use Cpanel::OSSys ();
use Fcntl ();
use Cpanel::SysPkgs ();
use Cpanel::Usage ();
Cpanel::Update::safe_update_environment('dovecot');
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 Dovecot isn't supported
if ( -e '/etc/gentoo-release' || -e '/etc/debian-release' ) {
die "Dovecot is not currently supported on this operating system";
}
# Check that Dovecot is the configured IMAP server and that system is Maildir format
my $cpconf_ref = Cpanel::Config::LoadCpConf::loadcpconf();
if ( !defined $cpconf_ref->{'mailserver'} || $cpconf_ref->{'mailserver'} ne 'dovecot' ) {
print "Dovecot is not the configured IMAP server.\n";
print "Use /scripts/setupmailserver to change this setting.\n";
exit 0;
}
if ( !$cpconf_ref->{'maildir'} ) {
print "This system is currently configured to use mbox formatted\n";
print "mailboxes. Dovecot support is only available on systems\n";
print "using the maildir format.\n";
print "Use /scripts/convert2maildir to change this setting.\n";
exit 0;
}
if ( $> != 0 ) {
die "Insufficient permissions to update Dovecot";
}
### Setup worker object and initilize variables
Cpanel::Update::init_UP_update('dovecot');
my $httpClient = Cpanel::HttpRequest->new( 'hideOutput' => 0 );
my $updated = 0;
$force ||= check_current_install();
print "Dovecot Setup Script Version 1.0\n";
if ( -x "/scripts/predovecotup" ) {
system("/scripts/predovecotup");
}
$updated = ( $system =~ /freebsd/i ) ? freebsd_install( 'dovecot', $force, $src ) : linux_install( 'dovecot', $force, $src );
if ( !$updated ) {
my $dovecot_conf = Cpanel::AdvConfig::dovecot::utils::find_dovecot_conf();
if ( !-e $dovecot_conf || Cpanel::AdvConfig::dovecot::utils::templates_updated()) {
$updated = 1;
}
else {
Cpanel::AdvConfig::dovecot::utils::checksrvd_tcp_check($dovecot_conf);
}
}
if ($updated) {
system('/scripts/builddovecotconf');
my $init = Cpanel::Init->new(); # Have the module figure out if the operating system is supported.
$init->generate_scripts(
{
'service' => 'dovecot',
'user' => 'root',
'pidfile' => '/var/run/dovecot/master.pid',
'regex' => '(?:^|\\s+|\\/)dovecot$',
'commands' => {
'start' => {
'exec' => [ [ ( -x '/usr/local/sbin/dovecot' ? '/usr/local/sbin/dovecot' : '/usr/sbin/dovecot' ) ] ],
'showoutput' => 0,
'chained' => 1,
},
'stop' => {
'exec' => [ [ '/scripts/ckillall', '-TERM', 'dovecot' ] ],
'showoutput' => 0,
},
'status' => {
'exec' => ['echo'],
'showoutput' => 1,
},
},
}
);
# Kill old style dovecot-auth
Cpanel::Proc::doom('dovecot-auth');
# Turn on in init system
my $output = $init->run_command_for_one( 'enable', 'dovecot' );
# Start Dovecot
system('/scripts/restartsrv_dovecot');
}
if ( -x "/scripts/postdovecotup" ) {
system("/scripts/postdovecotup");
}
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 $pkg_name = shift;
my $srpm_url = shift;
my $syspkgobj = shift;
my $rpm_file;
my @pkgs = ( "rpm", "rpm-build", "openssl-devel", "pam-devel", "zlib-devel" );
$syspkgobj->install( 'pkglist' => \@pkgs );
my $rpm = 'rpm';
if ( -e "/usr/bin/rpmbuild" ) { $rpm = '/usr/bin/rpmbuild'; }
$httpClient->download( $srpm_url, "/home/cpanel-${pkg_name}-install/cpanel-update.src.rpm" );
open( RNULL, "<", "/dev/null" );
print "Building RPM...";
open( RPM, ">", "rpmbuildlog" );
my $pid = IPC::Open3::open3( "<&RNULL", ">&RPM", ">&RPM", "$rpm", "--rebuild", "/home/cpanel-${pkg_name}-install/cpanel-update.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");
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 linux_install {
my $pkg_name = shift;
my $force = shift;
my $src = shift;
my $updated = 0;
my %VINFO = cPpkgversions::getpkgversioninfo($pkg_name);
my ( $distro, $version, $arch ) = get_distro_id($machine);
my $pkg_version = $VINFO{$pkg_name}{'version'};
my $pkg_release = $VINFO{$pkg_name}{'release'};
my $current_package_string = Cpanel::SafeRun::saferunnoerror( 'rpm', '-q', $pkg_name );
chomp($current_package_string);
print "This is the ${pkg_name} ${pkg_version} installer release number ${pkg_release} for platform ${distro} ${version}\n";
if ( $current_package_string eq "${pkg_name}-${pkg_version}-${pkg_release}" && !$force ) {
print "${pkg_name} is up to date. Use ${pkg_name}up --force to force a reinstall\n";
}
else {
my $syspkgobj = Cpanel::SysPkgs->new();
if ( !$syspkgobj ) { die print "Could not create SysPkgs object\n"; }
my @dependencies = ('libcap');
$syspkgobj->install( 'pkglist' => \@dependencies );
print "Resetting ${pkg_name} to cPanel Defaults and Installing the Latest Version\n";
### Fetch or build RPM
my $rpm_file = "/${pkg_name}-${pkg_version}-${pkg_release}.${arch}.rpm";
my $srpm_file = "/${pkg_name}-${pkg_version}-${pkg_release}.src.rpm";
my $rpm_url = "http://httpupdate.cpanel.net/${pkg_name}install/${pkg_version}-${pkg_release}" . ( $machine =~ /64/ ? '/64/' : '/' ) . "${distro}/${version}/${rpm_file}";
my $srpm_url = "http://httpupdate.cpanel.net/${pkg_name}install/${pkg_version}-${pkg_release}/src/${srpm_file}";
mkdir "/home/cpanel-${pkg_name}-install", oct(700);
chdir "/home/cpanel-${pkg_name}-install";
if ( !$src ) {
$httpClient->download( $rpm_url, "/home/cpanel-${pkg_name}-install/${rpm_file}" );
my $filesize = ( stat("/home/cpanel-${pkg_name}-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-${pkg_name}-install/${rpm_file}";
}
}
if ($src) {
$rpm_file = buildrpm( $pkg_name, $srpm_url, $syspkgobj );
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;
chdir('/');
if ( -e "/home/cpanel-${pkg_name}-install" ) {
system 'rm', '-rf', "/home/cpanel-${pkg_name}-install";
}
}
return $updated;
}
sub usage {
print <<EO_USAGE;
Usage: dovecotup [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;
}