#!/usr/bin/perl
#WHMADDON:addonupdates:ConfigServer Explorer
#ACLS:configserver
###############################################################################
# Copyright (C) 2006-2025 Jonathan Michaelson
#
# https://github.com/waytotheweb/scripts
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see .
###############################################################################
## no critic (RequireUseWarnings, ProhibitExplicitReturnUndef, ProhibitMixedBooleanOperators, RequireBriefOpen)
use strict;
use IPC::Open3;
use Fcntl qw(:DEFAULT :flock);
use File::Find;
use File::Copy;
use CGI::Carp qw(fatalsToBrowser);
use lib '/usr/local/cpanel';
require Cpanel::Form;
require Whostmgr::ACLS;
require Cpanel::Template;
require Cpanel::Rlimit;
require Cpanel::Version::Tiny;
###############################################################################
# start main
our ($myv, $images, $versionfile, $script, %form, $webpath, $demo, $message,
$thisdir, @months, @thisdirs, @thisfiles, $tgid, $tuid, $storepath,
@passrecs, $act, $origpath, $destpath, $dir, @dirs, @files, @splitme,
$extra, $extramessage, $fileno, $wwwpath, @userfiles, $downloadserver);
%form = Cpanel::Form::parseform();
Whostmgr::ACLS::init_acls();
if (!Whostmgr::ACLS::hasroot()) {
print "Content-type: text/html\r\n\r\n";
print "You do not have access to access the ConfigServer Explorer.\n";
exit();
}
Cpanel::Rlimit::set_rlimit_to_infinity();
$script = "cse.cgi";
$images = "cse";
$versionfile = "/usr/local/cpanel/whostmgr/docroot/cgi/configserver/cse/cseversion.txt";
$webpath = '/';
$demo = 0;
if ($form{do} eq "view") {
&view;
exit;
}
my $thisapp = "cse";
my $reregister;
my $modalstyle;
if ($Cpanel::Version::Tiny::major_version >= 65) {
if (-e "/usr/local/cpanel/whostmgr/docroot/cgi/configserver/${thisapp}/${thisapp}.conf") {
sysopen (my $CONF, "/usr/local/cpanel/whostmgr/docroot/cgi/configserver/${thisapp}/${thisapp}.conf", O_RDWR | O_CREAT);
flock ($CONF, LOCK_EX);
my @confdata = <$CONF>;
chomp @confdata;
for (0..scalar(@confdata)) {
if ($confdata[$_] =~ /^target=mainFrame/) {
$confdata[$_] = "target=_self";
$reregister = 1;
}
}
if ($reregister) {
seek ($CONF, 0, 0);
truncate ($CONF, 0);
foreach (@confdata) {
print $CONF "$_\n";
}
&printcmd("/usr/local/cpanel/bin/register_appconfig","/usr/local/cpanel/whostmgr/docroot/cgi/configserver/${thisapp}/${thisapp}.conf");
$reregister = "
Updated application. The next time you login to WHM this will open within the native WHM main window instead of launching a separate window
\n";
}
close ($CONF);
}
}
print "Content-type: text/html\r\n\r\n";
#if ($Cpanel::Version::Tiny::major_version < 65) {$modalstyle = "style='top:120px'"}
open (IN, "<", $versionfile) or die $!;
$myv = ;
close (IN);
chomp $myv;
$downloadserver = &getdownloadserver;
my $bootstrapcss = "";
my $jqueryjs = "";
my $bootstrapjs = "";
my $templatehtml;
unless ($form{do} eq "console") {
open SCRIPTOUT, '>', \$templatehtml;
select SCRIPTOUT;
print <
$jqueryjs
$bootstrapjs
EOF
} else {
print <
$bootstrapcss
$jqueryjs
$bootstrapjs
\n";
}
print <
\$("#loader").hide();
\$("#docs-link").hide();
\$(document).ready(function(){
\$('[data-tooltip="tooltip"]').tooltip();
});
EOF
unless ($form{do} eq "console") {
close SCRIPTOUT;
select STDOUT;
Cpanel::Template::process_template(
'whostmgr',
{
"template_file" => "${thisapp}.tmpl",
"${thisapp}_output" => $templatehtml,
"print" => 1,
}
);
} else {
print "\n";
print "\n";
print "\n";
}
exit;
# end main
###############################################################################
# start browse
sub browse {
my %htmlext;
#foreach my $img (split(/\,/,$wwwext)) {$htmlext{$img} = 1}
my $extra;
if ($form{c}) {
if (-e "$webpath$form{c}") {
$extra = "&c=$form{c}";
} else {
$form{c} = "";
}
}
if ($form{m}) {
if (-e "$webpath$form{m}") {
$extra = "&m=$form{m}"
} else {
$form{m} = "";
}
}
print "\n";
$thisdir = $webpath;
if ($thisdir !~ /\/$/) {$thisdir .= "/"}
$thisdir .= $form{p};
$thisdir =~ s/\/+/\//g;
@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
my $errordir = 0;
opendir (DIR, "$thisdir") or $errordir = 1;
while (my $file = readdir(DIR)) {
if (-d "$thisdir/$file") {
if ($file !~ /^\.$|^\.\.$/) {push (@thisdirs, $file)}
} else {
push (@thisfiles, $file);
}
}
closedir (DIR);
@thisdirs = sort @thisdirs;
@thisfiles = sort @thisfiles;
print "
\n";
print "WARNING! While this utility can be very useful it is also very dangerous indeed. You can easily render your server inoperable and unrecoverable by performing ill advised actions. No warranty or guarantee is provided with the product that protects against system damage.\n\n";
print "
\n";
if ($message) {print "
$message
\n";}
print "
\n";
print "
";
print "[Home]";
my $path = "";
my $cnt = 2;
my @path = split(/\//,$form{p});
foreach my $dir (@path) {
if ($dir ne "" and ($dir ne "/")) {
if ($cnt == @path) {
print "/$dir";
} else {
print "/$dir";
}
$path .= "/$dir";
$cnt++;
}
}
if ($form{c}) {print " Copy buffer:$form{c}\n"}
if ($form{m}) {print " Move buffer:$form{m}\n"}
print "
Unable to connect to http://www.configserver.com, retry in $retrytime seconds. An Upgrade button will appear here if new version is detected
\n";
}
} else {
print "
Unable to connect to http://www.configserver.com, retry in ".($retrytime - $retry)." seconds. An Upgrade button will appear here if new version is detected
\n";
}
print "
\n";
return;
}
# end browse
###############################################################################
# start setp
sub setp {
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
my $status = 0;
chmod (oct("0$form{newp}"),"$webpath$form{p}/$form{f}") or $status = $!;
if ($status) {$message = "Operation Failed - $status"} else {$message = ""}
}
&browse;
return;
}
# end setp
###############################################################################
# start seto
sub seto {
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
my $status = "";
my ($uid,$gid) = split (/\:/,$form{newo});
if ($uid !~ /^\d/) {$uid = (getpwnam($uid))[2]}
if ($gid !~ /^\d/) {$gid = (getgrnam($gid))[2]}
if ($uid eq "") {$message .= "No such user \n"}
if ($gid eq "") {$message .= "No such group \n"}
if ($message eq "") {
chown ($uid,$gid,"$webpath$form{p}/$form{f}") or $status = $!;
if ($status) {$message = "Operation Failed - $status"} else {$message = ""}
}
}
&browse;
return;
}
# end seto
###############################################################################
# start ren
sub ren {
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
my $status = 0;
rename ("$webpath$form{p}/$form{f}","$webpath$form{p}/$form{newf}") or $status = $!;
if ($status) {$message = "Operation Failed - $status"} else {$message = ""}
}
&browse;
return;
}
# end ren
###############################################################################
# start moveit
sub moveit {
if ($demo) {
$form{m} = "";
$message = "This action is disabled in the demonstration";
} else {
if ("$webpath$form{m}" eq "$webpath$form{p}/$form{newf}") {
$message = "Move Failed - Cannot overwrite original";
}
elsif ((-d "$webpath$form{m}") and ("$webpath$form{p}/$form{newf}" =~ /^$webpath$form{m}\//)) {
$message = "Move Failed - Cannot move inside original";
}
else {
my $status = 0;
rename ("$webpath$form{m}","$webpath$form{p}/$form{newf}") or $status = $!;
if ($status) {$message = "Operation Failed - $status"} else {$message = ""}
}
if ($message eq "") {$form{m} = ""}
}
&browse;
return;
}
# end moveit
###############################################################################
# start copyit
sub copyit {
if ($demo) {
$form{c} = "";
$message = "This action is disabled in the demonstration";
} else {
if ("$webpath$form{c}" eq "$webpath$form{p}/$form{newf}") {
$message = "Copy Failed - Cannot overwrite original";
}
elsif ((-d "$webpath$form{c}") and ("$webpath$form{p}/$form{newf}" =~ /^$webpath$form{c}\//)) {
$message = "Copy Failed - Cannot copy inside original";
}
else {
if (-d "$webpath$form{c}") {
use File::Copy;
$origpath = "$webpath$form{c}";
$destpath = "$webpath$form{p}/$form{newf}";
find(\&mycopy, $origpath);
} else {
use File::Copy;
copy ("$webpath$form{c}","$webpath$form{p}/$form{newf}") or $message = "Copy Failed - $!";
if ($message eq "") {
my $mode = sprintf "%04o", (stat("$webpath$form{c}"))[2] & 00777;
chmod (oct($mode),"$webpath$form{p}/$form{newf}") or $message = "Permission Change Failed - $!";
}
}
}
if ($message eq "") {$form{c} = ""}
}
&browse;
return;
}
# end copyit
###############################################################################
# start mycopy
sub mycopy {
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
my $file = $File::Find::name;
(my $dest = $file) =~ s/^\Q$origpath/$destpath/;
my $status = "";
if (-d $file) {
my $err = (split(/\//,$dest))[-1];
mkpath ($dest) or $status = "Copy Failed Making New Dir [$err] - $! \n";
} elsif (-f $file) {
my $err = (split(/\//,$file))[-1];
copy ($file,$dest) or $status = "Copy Failed [$err] - $! \n";
}
if ($status eq "") {
my $err = (split(/\//,$file))[-1];
my $mode = sprintf "%04o", (stat("$file"))[2] & 00777;
chmod (oct($mode),"$dest") or $message .= "Copy Failed Setting Perms [$err] - $! \n";
} else {
$message .= $status;
}
}
return;
}
# end mycopy
###############################################################################
# start cnewd
sub cnewd {
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
my $status = 0;
if ($form{newf} ne "") {
mkdir ("$webpath$form{p}/$form{newf}",0777) or $status = $!;
}
if ($status) {$message = "Operation Failed - $status"} else {$message = ""}
}
&browse;
return;
}
# end cnewd
###############################################################################
# start cnewf
sub cnewf {
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
my $status = 0;
if ($form{newf} ne "") {
if (-f ">$webpath$form{p}/$form{newf}") {
$status = "File exists";
} else {
open (OUT, ">", "$webpath$form{p}/$form{newf}") or $status = $!;
close (OUT);
}
}
if ($status) {$message = "Operation Failed - $status"} else {$message = ""}
}
&browse;
return;
}
# end cnewf
###############################################################################
# start del
sub del {
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
my $status = 0;
if (-d "$webpath$form{p}/$form{f}") {
use File::Path;
rmtree("$webpath$form{p}/$form{f}", 0, 0) or $status = $!;
} else {
unlink ("$webpath$form{p}/$form{f}") or $status = $!;
}
if ($status) {$message = "Operation Failed - $status"} else {$message = ""}
}
&browse;
return;
}
# end del
###############################################################################
# start view
sub view {
if (-e "$webpath$form{p}/$form{f}" ) {
if (-T "$webpath$form{p}/$form{f}") {
print "content-type: text/plain\r\n";
} else {
print "content-type: application/octet-stream\r\n";
}
print "content-disposition: attachment; filename=$form{f}\r\n\r\n";
open(IN,"<","$webpath$form{p}/$form{f}") or die $!;
while () {print}
close(IN);
}else{
print "content-type: text/html\r\n\r\n";
print "File [$webpath$form{p}/$form{f}] not found!";
}
return;
}
# end view
###############################################################################
# start console
sub console {
my $thisdir = "$webpath$form{p}";
$thisdir =~ s/\/+/\//g;
print "
\n";
print "";
return;
}
# end console
###############################################################################
# start cd
sub cd {
if (-d $form{directory}) {
$form{p} = $form{directory};
} else {
$message = "No such directory [$form{directory}]";
}
&browse;
return;
}
# end cd
###############################################################################
# start edit
sub edit {
open (IN, "<", "$webpath$form{p}/$form{f}") or die $!;
my @data = ;
close (IN);
my $filedata;
foreach my $line (@data) {
$line =~ s/\</g;
$line =~ s/\>/>/g;
$filedata .= $line;
}
my $lf = 0;
if ($filedata =~ /\r/) {$lf = 1}
print "\n";
print "
\n";
return;
}
# end search
###############################################################################
# start process_form
sub process_form {
my $buffer = $ENV{'QUERY_STRING'};
if ($buffer eq "") {
binmode (STDIN);
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (($buffer =~ /Content-Disposition: form-data/) and ($buffer =~ /^--/)) {
&upload($buffer);
exit;
}
}
my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) {
my ($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\|//g;
$value =~ s/\`//g;
$value =~ s/\0//g;
$value =~ s/\.\.//g;
$value =~ s/~!/ ~!/g;
$form{$name} = $value;
}
return;
}
# end process_form
###############################################################################
# start uploadfile
sub uploadfile {
$form{p} =~ s/\r//g;
$form{p} =~ s/\n//g;
$form{p} = &validate_upload_vars($form{p});
$form{type} =~ s/\r//g;
$form{type} =~ s/\n//g;
$form{type} = &validate_upload_vars($form{type});
$form{c} =~ s/\r//g;
$form{c} =~ s/\n//g;
$form{c} = &validate_upload_vars($form{c});
$form{m} =~ s/\r//g;
$form{m} =~ s/\n//g;
$form{m} = &validate_upload_vars($form{m});
$form{caller} =~ s/\r//g;
$form{caller} =~ s/\n//g;
my @filenames;
my @filebodies;
foreach my $key (keys %form) {
if (($form{$key} =~ /\/tmp\/cpanel3upload\./) or ($form{$key} =~ /\/tmp\/cpanel\.TMP/) or ($form{$key} =~ /\/tmp\/Cpanel_Form_file\.upload\./)) {
push (@filenames, $key);
push (@filebodies, $form{$key});
}
}
if ($demo) {
$message = "This action is disabled in the demonstration";
} else {
for (my $x = 0;$x < @filenames ;$x++) {
$filenames[$x] = &validate_upload_vars($filenames[$x]);
$filenames[$x] =~ s/\r//g;
$filenames[$x] =~ s/\n//g;
$filenames[$x] =~ s/^file-//g;
$filenames[$x] = (split (/\\/,$filenames[$x]))[-1];
$filenames[$x] = (split (/\//,$filenames[$x]))[-1];
if ($form{type} eq "ascii") {$filebodies[$x] =~ s/\r//g}
if (-e "$webpath$form{p}/$filenames[$x]") {
$extramessage .= " $filenames[$x] - Already exists, delete the original first";
$fileno--;
next;
}
my $openok = 1;
&printcmd("mv -f $filebodies[$x] $webpath$form{p}/$filenames[$x]");
$extramessage .= " $filenames[$x] - Uploaded";
}
$message = "$fileno File(s) Uploaded".$extramessage;
}
&browse;
return;
}
# end upload
###############################################################################
# start validate_upload_vars
sub validate_upload_vars {
my $value = shift;
return $value;
}
# end validate_upload_vars
###############################################################################
# start countfiles
sub countfiles {
if (-d $File::Find::name) {push (@dirs, $File::Find::name)} else {push (@files, $File::Find::name)}
return;
}
# end countfiles
###############################################################################
# start wantedfiles
sub wantedfiles {
unless (-f $File::Find::name) {return}
if ($File::Find::name =~ /\_vti\_/) {return}
if ($File::Find::dir =~ /^$wwwpath/) {$dir = $';} else {return}
if ($dir =~ /^\//) {$dir = $';}
if ($dir ne "") {$dir = $dir."/";}
push (@userfiles,$dir.$_);
return;
}
# end wantedfiles
###############################################################################
# start upgrade
sub upgrade {
$| = 1;
print "
";
if (-e "/usr/src/cse.tgz") {unlink ("/usr/src/cse.tgz") or die $!}
print "Retrieving new cse package...\n";
my ($status, $text) = &urlget("https://$downloadserver/cse.tgz","/usr/src/cse.tgz");
if ($status) {print "Oops: $text\n"}
if (! -z "/usr/src/cse.tgz") {
print "Unpacking new cse package...\n";
print "
";
&printcmd("cd /usr/src ; tar -xzf cse.tgz ; cd cse ; sh install.sh 2>&1");
print "
";
print "Tidying up...\n";
print "
";
&printcmd("rm -Rfv /usr/src/cse*");
print "
";
print "...All done.\n";
}
print "
";
open (IN, "<", $versionfile) or die $!;
$myv = ;
close (IN);
chomp $myv;
print "
\n";
return;
}
# end upgrade
###############################################################################
###############################################################################
# start urlget (v1.3)
#
# Examples:
#my ($status, $text) = &urlget("http://prdownloads.sourceforge.net/clamav/clamav-0.92.tar.gz","/tmp/clam.tgz");
#if ($status) {print "Oops: $text\n"}
#
#my ($status, $text) = &urlget("http://www.configserver.com/free/msfeversion.txt");
#if ($status) {print "Oops: $text\n"} else {print "Version: $text\n"}
#
sub urlget {
my $url = shift;
my $file = shift;
my $status = 0;
my $timeout = 1200;
local $SIG{PIPE} = 'IGNORE';
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(30);
my $req = HTTP::Request->new(GET => $url);
my $res;
my $text;
($status, $text) = eval {
local $SIG{__DIE__} = undef;
local $SIG{'ALRM'} = sub {die "Download timeout after $timeout seconds"};
alarm($timeout);
if ($file) {
$|=1;
my $expected_length;
my $bytes_received = 0;
my $per = 0;
my $oldper = 0;
open (OUT, ">", "$file\.tmp") or return (1, "Unable to open $file\.tmp: $!");
binmode (OUT);
print "...0\%\n";
$res = $ua->request($req,
sub {
my($chunk, $res) = @_;
$bytes_received += length($chunk);
unless (defined $expected_length) {$expected_length = $res->content_length || 0}
if ($expected_length) {
my $per = int(100 * $bytes_received / $expected_length);
if ((int($per / 5) == $per / 5) and ($per != $oldper)) {
print "...$per\%\n";
$oldper = $per;
}
} else {
print ".";
}
print OUT $chunk;
});
close (OUT);
print "\n";
} else {
$res = $ua->request($req);
}
alarm(0);
if ($res->is_success) {
if ($file) {
rename ("$file\.tmp","$file") or return (1, "Unable to rename $file\.tmp to $file: $!");
return (0, $file);
} else {
return (0, $res->content);
}
} else {
return (1, "Unable to download: ".$res->message);
}
};
alarm(0);
if ($@) {
return (1, $@);
}
if ($text) {
return ($status,$text);
} else {
return (1, "Download timeout after $timeout seconds");
}
}
# end urlget
###############################################################################
## start printcmd
sub printcmd {
my @command = @_;
my ($childin, $childout);
my $pid = open3($childin, $childout, $childout, @command);
while (<$childout>) {print $_}
waitpid ($pid, 0);
return;
}
## end printcmd
###############################################################################
## start getdownloadserver
sub getdownloadserver {
my @servers;
my $downloadservers = "/usr/local/cpanel/whostmgr/docroot/cgi/configserver/cse/downloadservers";
my $chosen;
if (-e $downloadservers) {
open (my $DOWNLOAD, "<", $downloadservers);
flock ($DOWNLOAD, LOCK_SH);
my @data = <$DOWNLOAD>;
close ($DOWNLOAD);
chomp @data;
foreach my $line (@data) {
if ($line =~ /^download/) {push @servers, $line}
}
## foreach my $line (slurp($downloadservers)) {
## $line =~ s/$cleanreg//g;
## if ($line =~ /^download/) {push @servers, $line}
## }
$chosen = $servers[rand @servers];
}
if ($chosen eq "") {$chosen = "download.configserver.com"}
return $chosen;
}
## end getdownloadserver
###############################################################################
1;