File: //opt/Kaseya/TESTJS72184235756529/libexec/GConfRegister
#! /usr/bin/perl
use Cwd;
use File::Basename;
use Sys::Syslog qw(:standard :macros);
sub ExitAndCleanup;
sub Usage;
sub GetAgentRoot;
sub getAgentCount;
$GConfInstall = 1;
$AgentRoot = GetAgentRoot($0);
$AgentInstanceGuid = basename($AgentRoot);
$KPanelXml = "$AgentRoot/share/KcsRegister.xml";
@unlinkList = ();
$Debug = 0;
$logMask = 15; # LOG_UPTO(LOG_WARNING)
$logOpts = 'pid';
$SIG{INT} = "ExitAndCleanup";
$SIG{QUIT} = "ExitAndCleanup";
while (@ARGV) {
if ($ARGV[0] =~ /^-I/ || $ARGV[0] =~ /--install/) {
$GConfInstall = 1;
} elsif ($ARGV[0] =~ /^-U/ || $ARGV[0] =~ /--uninstall/) {
$GConfInstall = 0;
} elsif ($ARGV[0] =~ /^-V/ || $ARGV[0] =~ /--verbose/) {
$logOpts = 'pid,cons';
} elsif ($ARGV[0] =~ /^-D/ || $ARGV[0] =~ /--debug/) {
$logMask = 255; # LOG_UPTO(LOG_DEBUG)
$Debug = 1;
} elsif ($ARGV[0] =~ /^-/) {
Usage("Unexpected Option: $ARGV[0]");
ExitAndCleanup(1);
} else {
Usage("Unexpected argument: $ARGV[0]");
ExitAndCleanup(1);
}
shift @ARGV;
}
openlog('[GConfRegister]', $logOpts, 'user');
setlogmask $logMask;
syslog("debug", "");
syslog("debug", "Starting...");
if ( -d "/etc/gconf" ) {
$etc_gconf = "/etc/gconf";
} elsif ( -d "/etc/opt/gnome/gconf" ) {
$etc_gconf = "/etc/opt/gnome/gconf";
} else {
syslog("err", "Cannot file /etc/gconf equivalent directory.\n");
ExitAndCleanup(1);
}
if ($GConfInstall) {
syslog("debug", "Processing GConf/Kaseya default setup file.");
$KaseyaPanelDefaults = "$etc_gconf/schemas/kaseya-panel-default-setup$$.entries";
if (-r "$etc_gconf/schemas/kaseya-panel-default-setup.entries") {
syslog("debug", "Processing Kaseya default setup file.");
$PanelDefaults = "$etc_gconf/schemas/kaseya-panel-default-setup.entries";
} elsif (-r "$etc_gconf/schemas/panel-default-setup.entries") {
syslog("debug", "Processing GConf default setup file.");
$PanelDefaults = "$etc_gconf/schemas/panel-default-setup.entries";
mkdir("$etc_gconf/kaseya.xml.defaults", 0755);
} else {
syslog("err", "Missing panel-default-setup.entries file.\n");
ExitAndCleanup(1);
}
open(IN, "< $PanelDefaults") || die "Can't open GConf default setup file.\n";
open(OUT, "> $KaseyaPanelDefaults") || die "Can't open output file $KaseyaPanelDefaults.\n";
$lnum = 0;
$inComment = 0;
while (<IN>) {
$lnum++;
if ($inComment) {
print OUT;
if (m'-->') {
$inComment = 0;
}
} elsif (m'<!--' && m'-->') {
# Single line comment
print OUT;
} elsif (m'<!--') {
$inComment = 1;
print OUT;
} elsif (m'<schema_key>/schemas/apps/panel/general/applet_id_list</schema_key>') {
print OUT;
$_ = <IN>;
$lnum++;
if (m'<value>') {
print OUT;
$_ = <IN>;
$lnum++;
if (m'<list type="string">') {
print OUT;
print OUT " <value>\n";
printf OUT " <string>KaUsrTskApplet_%s</string>\n", $AgentInstanceGuid;
print OUT " </value>\n";
}
} else {
syslog("debug", "Unexpected line [$_] (line $lnum)");
print OUT;
}
} elsif (m'</entrylist>') {
$save = $_;
open(KIN, "< $KPanelXml") || die "Can't open KcsRegister.xml file";
while (<KIN>) {
s/%AGENT_INSTANCE_GUID%/$AgentInstanceGuid/g;
print OUT;
}
print OUT $save;
} else {
print OUT;
}
}
close(IN);
close(OUT);
if (-r "$etc_gconf/schemas/kaseya-panel-default-setup.entries") {
rename("$etc_gconf/schemas/kaseya-panel-default-setup.entries", "$etc_gconf/schemas/kaseya-panel-default-setup.entries.old");
}
rename($KaseyaPanelDefaults, "$etc_gconf/schemas/kaseya-panel-default-setup.entries");
syslog("debug", "Done processing schema file.");
syslog("debug", "Process the path file (add kaseya.xml.defaults).");
$gotit = 0;
open(IN, "< $etc_gconf/2/path") || die "Can't open GConf 2.0 path file.\n";
while (<IN>) {
if (m'kaseya.xml.defaults') {
$gotit = 1;
break;
}
}
if ($gotit == 0) {
syslog("debug", "Process the path file (add kaseya.xml.defaults).");
$lnum = 0;
seek(IN, 0, 0);
open(OUT, "> $etc_gconf/2/path$$") || die "Can't create new GConf 2.0 path file.\n";
while (<IN>) {
$lnum++;
print OUT;
if (m'kaseya.xml.defaults') {
} elsif (m"^include\s+$etc_gconf/2/local-defaults.path") {
print OUT "\nxml:readonly:$etc_gconf/kaseya.xml.defaults\n";
} elsif (m"$etc_gconf/2/local-defaults.path") {
print yes;
} elsif (m"local-defaults.path") {
print yes;
}
}
close(OUT);
rename("$etc_gconf/2/path", "$etc_gconf/2/path.orig");
rename("$etc_gconf/2/path$$", "$etc_gconf/2/path");
syslog("debug", "Done processing path file.");
}
close(IN);
`gconftool-2 --config-source=xml:readwrite:$etc_gconf/kaseya.xml.defaults --direct --load $etc_gconf/schemas/kaseya-panel-default-setup.entries`;
} else {
# Uninstall
$agentCount = getAgentCount();
if ($agentCount == 1) {
# Simple case:
syslog("debug", "Removing GConf setup for the last Linux Agent.");
`rm -rf $etc_gconf/kaseya.xml.defaults $etc_gconf/schemas/kaseya-panel-default-setup.entries`;
syslog("debug", "Removing kaseya.xml.defaults from path file.");
$gotit = 0;
open(IN, "< $etc_gconf/2/path") || die "Can't open GConf 2.0 path file.\n";
open(OUT, "> $etc_gconf/2/path$$") || die "Can't create new GConf 2.0 path file.\n";
while (<IN>) {
if (m'kaseya.xml.defaults') {
$gotit = 1;
} elsif ($gotit && m/^$/) {
$gotit = 0; # skip the 1st line after kaseya.xml.defaults if its empty
} else {
print OUT;
}
}
close(OUT);
rename("$etc_gconf/2/path", "$etc_gconf/2/path.orig");
rename("$etc_gconf/2/path$$", "$etc_gconf/2/path");
if ( -e "$etc_gconf/schemas/panel-default-setup.entries" ) {
# Sometimes the schemas directory doesn't exist and this gets an error. I'm not sure why it doesn't exist, but don't load if it doesn't!
`gconftool-2 --config-source=xml:readwrite:$etc_gconf/gconf.xml.defaults --direct --load $etc_gconf/schemas/panel-default-setup.entries`;
}
syslog("debug", "Done processing path file.");
} elsif ($agentCount > 1) {
# Complex case: We have multiple agents and must edit the Kaseya files/directies rather
# than removing them.
syslog("debug", "Removing GConf setup for the one of $agentCount Linux Agents.");
$KaseyaPanelDefaults = "$etc_gconf/schemas/kaseya-panel-default-setup.entries";
if (-r "$KaseyaPanelDefaults") {
$NewKPD = "$etc_gconf/schemas/kaseya-panel-default-setup$$.entries";
open(IN, "< $KaseyaPanelDefaults") || die "Can't open Kaseya panel defaults file.\n";
open(OUT, "> $NewKPD") || die "Can't open output file for edited Kaseya panel default information.\n";
$skipping = 0;
while (<IN>) {
if (/<!-- Start: KaUsrTskApplet_$AgentInstanceGuid/) {
$skipping = 1;
} elsif (/<!-- End: KaUsrTskApplet_$AgentInstanceGuid/) {
$skipping = 0;
} elsif (!$skipping) {
print OUT;
}
}
if ($skipping) { # Never found the end tag so abort without updating
syslog("debug", "Error: Failure parsing Kaseya defaults file (never found End tag).");
exitAndCleanup(1);
} else {
close(IN);
close(OUT);
rename("$KaseyaPanelDefaults", "$KaseyaPanelDefaults.old");
rename("$NewkPD", "$KaseyaPanelDefaults");
`gconftool-2 --config-source=xml:readwrite:$etc_gconf/kaseya.xml.defaults --direct --load $KaseyaPanelDefaults`;
syslog("debug", "Done.");
}
}
} else {
syslog("debug", "Failed uninstall. Agent list has no entries.");
}
}
ExitAndCleanup(0);
sub GetAgentRoot {
my $pgm = $_[0];
my $cwd = getcwd();
my $path;
if ($pgm =~ m'^/') {
$path = dirname("$pgm");
} else {
$path = dirname("$cwd/$pgm");
}
chdir("$path/..");
$path = getcwd();
chdir($cwd);
return $path;
}
# Important Note: This script is expected to be run by KcsSetup.sh or KcsUninstall.sh. Both
# scripts call GConfRegiser --uninstall before updating the AgentList. This means a list
# length of 1 means we're in the process of removing the last Agent.
sub getAgentCount {
my $count = 0;
open(IN, "< /var/Kaseya/Agent/AgentList") || return 0;
while (<IN>) {
next if m/^#/ || m/^$/; # it really shouldn't have comments or blank lines...
$count++;
}
close(IN);
return $count;
}
sub Usage {
syslog('info', "%s\n", "$_[0]");
print("Warning: $_[0]\n");
print("Usage: GConfRegister [ -s ]\n");
}
sub ExitAndCleanup {
if ($_[0] != '0') {
syslog('info', "Exiting with %s return code.\n", "$_[0]");
}
if (!$Debug) {
unlink "$KaseyaPanelDefaults";
}
exit $_[0];
}