File: //opt/Kaseya/TESTJS72184235756529/libexec/KaInstallGdmUser
#! /usr/bin/perl
use Cwd;
use File::Basename;
use Sys::Syslog;
sub ExitAndCleanup;
sub Usage;
sub GetAgentRoot;
$AgentRoot = GetAgentRoot($0);
$AgentInstanceGuid = basename($AgentRoot);
$GdmInstall = 1;
$Debug = 0;
$logMask = 15; # LOG_UPTO(LOG_WARNING)
$logOpts = 'pid';
@SavedArgs = @ARGV;
$SIG{INT} = "ExitAndCleanup";
$SIG{QUIT} = "ExitAndCleanup";
while (@ARGV) {
if ($ARGV[0] =~ /^-I/ || $ARGV[0] =~ /--install/) {
$GdmInstall = 1;
} elsif ($ARGV[0] =~ /^-U/ || $ARGV[0] =~ /--uninstall/) {
$GdmInstall = 0;
} elsif ($ARGV[0] =~ /^-D/ || $ARGV[0] =~ /--debug/) {
$logMask = 255; # LOG_UPTO(LOG_DEBUG)
$Debug = 1;
} elsif ($ARGV[0] =~ /^-V/ || $ARGV[0] =~ /--verbose/) {
$logOpts = 'pid,cons';
} elsif ($ARGV[0] =~ /^-/) {
Usage("Unexpected Option: $ARGV[0]");
ExitAndCleanup(1);
} elsif ("$KPanelXml" eq "" && $ARGV[0] =~ m'^/') {
$KPanelXml = $ARGV[0];
} else {
Usage("Unexpected argument: $ARGV[0]");
ExitAndCleanup(1);
}
shift @ARGV;
}
if ( -d "/etc/gdm" ) {
$etc_gdm = "/etc/gdm";
} elsif ( -d "/etc/opt/gnome/gdm" ) {
$etc_gdm = "/etc/opt/gnome/gdm";
} else {
syslog("err", "Cannot file /etc/gdm equivalent directory.\n");
ExitAndCleanup(1);
}
openlog('[KaInstallGdmUser]', $logOpts, 'user');
setlogmask $logMask;
if ($GdmInstall) {
syslog("info", "Starting KaInstallGdmUser in install mode");
if (! -d "$etc_gdm/PreSession") {
syslog('err', "Missing \"$etc_gdm/PreSession\" directory.");
ExitAndCleanup(1);
}
if (-e "$etc_gdm/PreSession/Default") {
$doit = 1;
# Since many Linux distros have a PreSession/Default file which execs sessreg,
# its better to add our magic at the start. We do read the file through first
# to make sure we're not doing a duplicate install. We also need to make sure
# and remove a "--uninstall" flag if we're installing.
open(IN, "< $etc_gdm/PreSession/Default") || die "Can not open Default file.\n";
while (<IN>) {
next if /^\s*#/;
$uninstDir = "$AgentRoot" . "_Uninstalled";
if (m"$uninstDir/libexec/KaGdmUser") {
syslog('debug', "Switching KaGdmUser from uninstall to install mode in PreSession/Default");
$doit = 2;
last;
} elsif (m"$AgentRoot/libexec/KaGdmUser") {
syslog('debug', "Found reference to KaGdmuser in PreSession/Default");
$doit = 0;
}
}
if ($doit) {
seek(IN, 0, 0); # Go back to the beginning of the file
open(OUT, "> $etc_gdm/PreSession/Default$$") || die "Can not create new Default file.\n";
if ($doit == 1) { # insert mode
syslog('debug', "Adding to existing PreSession/Default file.");
while (<IN>) {
last if ! /^#/;
print OUT;
}
$firstLine = $_;
print OUT "\n";
print OUT "if [ -x \"$AgentRoot/libexec/KaGdmUser\" ]\n";
print OUT "then\n";
print OUT " $AgentRoot/libexec/KaGdmUser\n";
print OUT "fi\n\n";
print OUT "$firstLine" if ! m"^\s*$";
while (<IN>) {
print OUT;
}
} else {
# Change KaGdmUser --uninstall to install mode
syslog('debug', "Switching existing PreSession/Default file from uninstall to install mode.");
while (<IN>) {
if (m"/libexec/KaGdmUser") {
syslog('debug', "Found uninstall code.");
s/_Uninstalled//;
s/ --uninstall//;
}
print OUT;
}
}
close(IN);
close(OUT);
rename("$etc_gdm/PreSession/Default", "$etc_gdm/PreSession/Default.old");
rename("$etc_gdm/PreSession/Default$$", "$etc_gdm/PreSession/Default");
}
} else {
syslog('debug', "Creating new PreSession/Default file.");
open(OUT, "> $etc_gdm/PreSession/Default") || die "Can not Create Default file.\n";
print OUT "#! /bin/sh\n";
print OUT "\n";
print OUT "if [ -x \"$AgentRoot/libexec/KaGdmUser\" ]\n";
print OUT "then\n";
print OUT " $AgentRoot/libexec/KaGdmUser\n";
print OUT "fi\n";
close(OUT);
}
chmod(0755, "$etc_gdm/PreSession/Default");
if (-d "$AgentRoot" . "_Uninstalled/libexec") {
syslog('debug', "Removing $AgentRoot" . "_Uninstalled directory.");
system "rm", "-rf", "$AgentRoot" . "_Uninstalled";
}
open(IN, "< /etc/passwd") || die "Can't open /etc/passwd.\n";
while (<IN>) {
chomp;
($login, $x, $x, $group, $x, $home, $shell) = split /:/;
if ($shell !~ m'/sbin/nologin' && $shell !~ m'/bin/false' && -d "$home/.gconf") {
system "$AgentRoot/libexec/KaGdmUser", "-u", "$login", "-g", "$group", "-h", "$home";
}
}
close(IN);
} else { # Uninstall
syslog('info', "Starting KaInstallGdmUser in uninstall mode");
$uninstDir = "$AgentRoot" . "_Uninstalled";
if (-x "$AgentRoot/libexec/KaGdmUser" && ! -d "$uninstDir") {
mkdir "$uninstDir", 0755;
mkdir "$uninstDir/libexec", 0755;
system "cp", "$AgentRoot/libexec/KaGdmUser", "$uninstDir/libexec";
open(README, "> $uninstDir/README") || die "Cannot create README file explaining uninstall rules.\n";
print README "This directory contains a post-uninstall script which cleans up user\n";
print README "logins after the Agent has been uninstalled. Removing this directory\n";
print README "can cause errors during future logins for some users.\n";
close(README);
}
open(IN, "< $etc_gdm/PreSession/Default") || die "Can not open Default file.\n";
open(OUT, "> $etc_gdm/PreSession/Default$$") || die "Can not create new Default file.\n";
while (<IN>) {
if (m"$AgentRoot/libexec/KaGdmUser") {
syslog('debug', "Found KaGdmUser we want to change.");
s/$AgentRoot/$uninstDir/;
print OUT; # m"\s*if\b";
$_ = <IN>;
print OUT; # m"\s*then\b";
<IN>; # read and replace this line
print OUT " $uninstDir/libexec/KaGdmUser --uninstall\n";
next;
}
print OUT;
}
close(IN);
close(OUT);
rename("$etc_gdm/PreSession/Default", "$etc_gdm/PreSession/Default.old");
rename("$etc_gdm/PreSession/Default$$", "$etc_gdm/PreSession/Default");
chmod(0755, "$etc_gdm/PreSession/Default");
open(IN, "< /etc/passwd") || die "Can't open /etc/passwd.\n";
while (<IN>) {
chomp;
($login, $x, $x, $group, $x, $home, $shell) = split /:/;
if ($shell !~ m'/sbin/nologin' && $shell !~ m'/bin/false' && -d "$home/.gconf") {
system "$AgentRoot/libexec/KaGdmUser", "--uninstall", "-u", "$login", "-g", "$group", "-h", "$home";
}
}
close(IN);
}
ExitAndCleanup(0);
sub GetAgentRoot {
my $pgm = $_[0];"-x",
my $cwd = getcwd();
my $path;
if ($pgm =~ m'^/') {
$path = dirname("$pgm");
} else {
$path = dirname("$cwd/$pgm");
}
chdir("$path/..");
$path = getcwd();
chdir($cwd);
return $path;
}
sub Usage {
syslog('info', "%s\n", "$_[0]");
print("Warning: $_[0]\n");
print("Usage: KaInstallGdmUser.pl [ -s ]\n");
}
sub ExitAndCleanup {
if ($_[0] != '0') {
syslog('info', "Exiting with %s return code.\n", "$_[0]");
}
exit $_[0];
}