enhanced lamdaemon test

This commit is contained in:
Roland Gruber 2006-11-05 12:16:07 +00:00
parent c87dfec035
commit c71cd3049d
2 changed files with 197 additions and 174 deletions

View File

@ -89,150 +89,156 @@ sub get_fs { # Load mountpoints from mtab if enabled quotas
if ($< == 0 ) { # we are root if ($< == 0 ) { # we are root
# Drop root Previleges # Drop root Previleges
($<, $>) = ($>, $<); ($<, $>) = ($>, $<);
if ($ARGV[0] eq "*test") { # loop for every transmitted user
use Quota; # Needed to get and set quotas while (1) {
print "Perl quota module successfully installed.\n"; my $input = <STDIN>;
print "If you haven't seen any errors lamdaemon.pl was set up successfully.\n"; chop($input);
} $return = "";
else { @vals = split (' ', $input);
# loop for every transmitted user switch: {
while (1) { # test if lamdaemon can be run
my $input = <STDIN>; if (($vals[1] eq 'test')) {
chop($input); # basic test
$return = ""; if ($vals[2] eq 'basic') {
@vals = split (' ', $input); $return = "Ok";
switch: { }
# Get user information # quota test
if (($vals[3] eq 'user') || ($vals[1] eq 'home')) { @user = getpwnam($vals[0]); } elsif ($vals[2] eq 'quota') {
else { @user = getgrnam($vals[0]); } require Quota;
$vals[1] eq 'home' && do { $return = "Ok";
switch2: { }
$vals[2] eq 'add' && do { last switch;
# split homedir to set all directories below the last dir. to 0755 }
my $path = $user[7]; # Get user information
$path =~ s,/(?:[^/]*)$,,; if (($vals[3] eq 'user') || ($vals[1] eq 'home')) { @user = getpwnam($vals[0]); }
($<, $>) = ($>, $<); # Get root privileges else { @user = getgrnam($vals[0]); }
if (! -e $path) { $vals[1] eq 'home' && do {
system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir switch2: {
$vals[2] eq 'add' && do {
# split homedir to set all directories below the last dir. to 0755
my $path = $user[7];
$path =~ s,/(?:[^/]*)$,,;
($<, $>) = ($>, $<); # Get root privileges
if (! -e $path) {
system 'mkdir', '-m', '0755', '-p', $path; # Create paths to homedir
}
if (! -e $user[7]) {
system 'mkdir', '-m', '0755', $user[7]; # Create homedir itself
system ("(cd /etc/skel && tar cf - .) | (cd $user[7] && tar xmf -)"); # Copy /etc/sekl into homedir
system 'chown', '-hR', "$user[2]:$user[3]" , $user[7]; # Change owner to new user
if (-e '/usr/sbin/useradd.local') {
system '/usr/sbin/useradd.local', $user[0]; # run useradd-script
} }
if (! -e $user[7]) { $return = "Ok";
system 'mkdir', '-m', '0755', $user[7]; # Create homedir itself }
system ("(cd /etc/skel && tar cf - .) | (cd $user[7] && tar xmf -)"); # Copy /etc/sekl into homedir else {
system 'chown', '-hR', "$user[2]:$user[3]" , $user[7]; # Change owner to new user $return = "ERROR,Lamdaemon,Home directory already exists.";
if (-e '/usr/sbin/useradd.local') { }
system '/usr/sbin/useradd.local', $user[0]; # run useradd-script ($<, $>) = ($>, $<); # Give up root previleges
last switch2;
};
$vals[2] eq 'rem' && do {
($<, $>) = ($>, $<); # Get root previliges
if (-d $user[7] && $user[7] ne '/') {
if ((stat($user[7]))[4] eq $user[2]) {
system 'rm', '-R', $user[7]; # Delete Homedirectory
if (-e '/usr/sbin/userdel.local') {
system '/usr/sbin/userdel.local', $user[0];
} }
$return = "Ok"; $return = "Ok";
} }
else { else {
$return = "ERROR,Lamdaemon,Home directory already exists."; $return = "ERROR,Lamdaemon,Home directory not owned by $user[2].";
} }
($<, $>) = ($>, $<); # Give up root previleges
last switch2;
};
$vals[2] eq 'rem' && do {
($<, $>) = ($>, $<); # Get root previliges
if (-d $user[7] && $user[7] ne '/') {
if ((stat($user[7]))[4] eq $user[2]) {
system 'rm', '-R', $user[7]; # Delete Homedirectory
if (-e '/usr/sbin/userdel.local') {
system '/usr/sbin/userdel.local', $user[0];
}
$return = "Ok";
}
else {
$return = "ERROR,Lamdaemon,Home directory not owned by $user[2].";
}
}
else {
$return = "ERROR,Lamdaemon,Home directory does not exist.";
}
($<, $>) = ($>, $<); # Give up root previleges
last switch2;
};
# Show error if undfined command is used
$return = "ERROR,Lamdaemon,Unknown command $vals[2].";
} }
last switch; else {
}; $return = "ERROR,Lamdaemon,Home directory does not exist.";
$vals[1] eq 'quota' && do { }
use Quota; # Needed to get and set quotas ($<, $>) = ($>, $<); # Give up root previleges
get_fs(); # Load list of devices with enabled quotas last switch2;
# Store quota information in array };
@quota_temp1 = split (':', $vals[4]); # Show error if undfined command is used
$group=0; $return = "ERROR,Lamdaemon,Unknown command $vals[2].";
$i=0; }
while ($quota_temp1[$i]) { last switch;
$j=0; };
@temp = split (',', $quota_temp1[$i]); $vals[1] eq 'quota' && do {
while ($temp[$j]) { require Quota; # Needed to get and set quotas
$quota[$i][$j] = $temp[$j]; get_fs(); # Load list of devices with enabled quotas
$j++; # Store quota information in array
@quota_temp1 = split (':', $vals[4]);
$group=0;
$i=0;
while ($quota_temp1[$i]) {
$j=0;
@temp = split (',', $quota_temp1[$i]);
while ($temp[$j]) {
$quota[$i][$j] = $temp[$j];
$j++;
}
$i++;
}
if ($vals[3] eq 'user') { $group=false; }
else {
$group=1;
@quota_usr = @quota_grp;
}
switch2: {
$vals[2] eq 'rem' && do {
$i=0;
($<, $>) = ($>, $<); # Get root privileges
while ($quota_usr[$i][0]) {
$dev = Quota::getqcarg($quota_usr[$i][1]);
$return = Quota::setqlim($dev,$user[2],0,0,0,0,1,$group);
$i++;
}
($<, $>) = ($>, $<); # Give up root previleges
last switch2;
};
$vals[2] eq 'set' && do {
$i=0;
($<, $>) = ($>, $<); # Get root privileges
while ($quota_usr[$i][0]) {
$dev = Quota::getqcarg($quota[$i][0]);
$return = Quota::setqlim($dev,$user[2],$quota[$i][1],$quota[$i][2],$quota[$i][3],$quota[$i][4],1,$group);
if ($return == -1) {
$return = "ERROR,Lamdaemon,Unable to set quota!";
} }
$i++; $i++;
} }
if ($vals[3] eq 'user') { $group=false; } ($<, $>) = ($>, $<); # Give up root previleges
else { last switch2;
$group=1; };
@quota_usr = @quota_grp; $vals[2] eq 'get' && do {
} $i=0;
switch2: { ($<, $>) = ($>, $<); # Get root privileges
$vals[2] eq 'rem' && do { while ($quota_usr[$i][0]) {
$i=0; if ($vals[0]ne'+') {
($<, $>) = ($>, $<); # Get root privileges
while ($quota_usr[$i][0]) {
$dev = Quota::getqcarg($quota_usr[$i][1]); $dev = Quota::getqcarg($quota_usr[$i][1]);
$return = Quota::setqlim($dev,$user[2],0,0,0,0,1,$group); @temp = Quota::query($dev,$user[2],$group);
$i++; if ($temp[0]ne'') {
} if ($temp == -1) {
($<, $>) = ($>, $<); # Give up root previleges $return = "ERROR,Lamdaemon,Unable to read quota!";
last switch2; }
}; else {
$vals[2] eq 'set' && do { $return = "$quota_usr[$i][1],$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$temp[7]:$return";
$i=0;
($<, $>) = ($>, $<); # Get root privileges
while ($quota_usr[$i][0]) {
$dev = Quota::getqcarg($quota[$i][0]);
$return = Quota::setqlim($dev,$user[2],$quota[$i][1],$quota[$i][2],$quota[$i][3],$quota[$i][4],1,$group);
if ($return == -1) {
$return = "ERROR,Lamdaemon,Unable to set quota!";
}
$i++;
}
($<, $>) = ($>, $<); # Give up root previleges
last switch2;
};
$vals[2] eq 'get' && do {
$i=0;
($<, $>) = ($>, $<); # Get root privileges
while ($quota_usr[$i][0]) {
if ($vals[0]ne'+') {
$dev = Quota::getqcarg($quota_usr[$i][1]);
@temp = Quota::query($dev,$user[2],$group);
if ($temp[0]ne'') {
if ($temp == -1) {
$return = "ERROR,Lamdaemon,Unable to read quota!";
}
else {
$return = "$quota_usr[$i][1],$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$temp[7]:$return";
}
} }
else { $return = "$quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
} }
else { $return = "$quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; } else { $return = "$quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
$i++;
} }
($<, $>) = ($>, $<); # Give up root previleges else { $return = "$quota_usr[$i][1],0,0,0,0,0,0,0,0:$return"; }
last switch2; $i++;
}; }
$return = "ERROR,Lamdaemon,Unknown command $vals[2]."; ($<, $>) = ($>, $<); # Give up root previleges
} last switch2;
}; };
last switch; $return = "ERROR,Lamdaemon,Unknown command $vals[2].";
$return = "ERROR,Lamdaemon,Unknown command $vals[1]."; }
}; };
print "$return\n"; last switch;
} $return = "ERROR,Lamdaemon,Unknown command $vals[1].";
};
print "$return\n";
} }
} }
else { else {

View File

@ -93,7 +93,7 @@ if (!$stopTest) {
} }
else { else {
echo "<td bgcolor=\"red\">" . _("Error") . "&nbsp;&nbsp;</td>\n"; echo "<td bgcolor=\"red\">" . _("Error") . "&nbsp;&nbsp;</td>\n";
echo "<td bgcolor=\"red\">" . _("Your LAM admin user must be a valid Unix account to work with lamdaemon!") . "</td>"; echo "<td bgcolor=\"red\">" . sprintf(_("Your LAM admin user (%s) must be a valid Unix account to work with lamdaemon!"), $credentials[0]) . "</td>";
$stopTest = true; $stopTest = true;
} }
echo "</tr>\n"; echo "</tr>\n";
@ -143,51 +143,68 @@ if (!$stopTest) {
flush(); flush();
// run lamdaemon and get user quotas $stopTest = lamTestLamdaemon("+ test basic\n", $stopTest, $handle, _("Execute lamdaemon"));
if (!$stopTest) { $stopTest = lamTestLamdaemon("+ test quota\n", $stopTest, $handle, _("Lamdaemon: Quota module installed"));
echo "<tr class=\"userlist\">\n<td>" . _("Execute lamdaemon") . "&nbsp;&nbsp;</td>\n"; $stopTest = lamTestLamdaemon("+ quota get user\n", $stopTest, $handle, _("Lamdaemon: read quotas"));
/**
* Runs a test case of lamdaemon.
*
* @param string $command test command
* @param boolean $stopTest specifies if test should be run
* @param connection $handle SSH connection
* @param string $testText describing text
* @return boolean true, if errors occured
*/
function lamTestLamdaemon($command, $stopTest, $handle, $testText) {
// run lamdaemon and get user quotas
if (!$stopTest) {
echo "<tr class=\"userlist\">\n<td>" . $testText . "&nbsp;&nbsp;</td>\n";
flush();
$lamdaemonOk = false;
$errorMessage = "";
$shell = ssh2_exec($handle, "sudo " . $_SESSION['config']->scriptPath);
$stderr = ssh2_fetch_stream($shell, SSH2_STREAM_STDERR);
fwrite($shell, $command);
$return = array();
$time = time() + 20;
while (sizeof($return) < 1) {
if ($time < time()) {
$lamdaemonOk = false;
$return[] = "ERROR," . _("Timeout while executing lamdaemon commands!");
break;
}
usleep(100);
$read = split("\n", trim(fread($shell, 100000)));
if ((sizeof($read) == 1) && (!isset($read[0]) || ($read[0] == ""))) continue;
for ($i = 0; $i < sizeof($read); $i++) {
$return[] = $read[$i];
}
}
$errOut = @fread($stderr, 100000);
if ((stripos($errOut, "sudoers") !== false) || (stripos($errOut, "sorry") !== false)) {
$return[] = "ERROR," . _("Sudo is not setup correctly!") . "," . str_replace(",", " ", $errOut);
}
if ((sizeof($return) == 1) && (stripos($return[0], "error") === false)) {
$lamdaemonOk = true;
}
if ($lamdaemonOk) {
echo "<td bgcolor=\"green\">" . _("Ok") . "</td>";
echo "<td bgcolor=\"green\">" . _("Lamdaemon successfully run.") . "</td>";
}
else {
echo "<td bgcolor=\"red\">" . _("Error") . "&nbsp;&nbsp;</td>\n";
echo "<td bgcolor=\"red\">\n";
for ($i = 0; $i < sizeof($return); $i++) {
call_user_func_array('StatusMessage', split(",", $return[$i]));
}
echo "</td>\n";
$stopTest = true;
}
echo "</tr>\n";
}
flush(); flush();
$lamdaemonOk = false; return $stopTest;
$errorMessage = "";
$shell = ssh2_exec($handle, "sudo " . $_SESSION['config']->scriptPath);
$stderr = ssh2_fetch_stream($shell, SSH2_STREAM_STDERR);
fwrite($shell, "+ quota get user\n");
$return = array();
$time = time() + 20;
while (sizeof($return) < 1) {
if ($time < time()) {
$lamdaemonOk = false;
$errorMessage = _("Timeout while executing lamdaemon commands!");
break;
}
usleep(100);
$read = split("\n", trim(fread($shell, 100000)));
if ((sizeof($read) == 1) && (!isset($read[0]) || ($read[0] == ""))) continue;
for ($i = 0; $i < sizeof($read); $i++) {
$return[] = $read[$i];
}
}
$errOut = @fread($stderr, 100000);
if ((stripos($errOut, "sudoers") !== false) || (stripos($errOut, "sorry") !== false)) {
$return[] = "ERROR," . _("Sudo is not setup correctly!") . "," . str_replace(",", " ", $errOut);
}
if ((sizeof($return) == 1) && (stripos($return[0], "error") === false)) {
$lamdaemonOk = true;
}
if ($lamdaemonOk) {
echo "<td bgcolor=\"green\">" . _("Ok") . "</td>";
echo "<td bgcolor=\"green\">" . _("Lamdaemon successfully run.") . "</td>";
}
else {
echo "<td bgcolor=\"red\">" . _("Error") . "&nbsp;&nbsp;</td>\n";
echo "<td bgcolor=\"red\">\n";
for ($i = 0; $i < sizeof($return); $i++) {
call_user_func_array('StatusMessage', split(",", $return[$i]));
}
echo "</td>\n";
$stopTest = true;
}
echo "</tr>\n";
} }
echo "</table>\n"; echo "</table>\n";