#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw/fatalsToBrowser/;

my %credentials;
&getCreds();
my $command = "/home/websites/splitreflection.com".$ENV{'SCRIPT_NAME'}.$ENV{'PATH_INFO'};
$ENV{'SCRIPT_NAME'} .= $ENV{'PATH_INFO'};
&run($command);

exit;

sub run
{
  my $script = shift;
  open my $filehandle, "<".$script or die "$! opening $script";
  my $data = join "", <$filehandle>;
  close $filehandle;

  if($data =~ /(.*)/s){$data = $1;}
  eval $data;
  if($@){die "Error in eval:$@"};
}

sub getCreds
{
  my $user;
  if($ENV{'REMOTE_USER'} =~ /(.*)/)
  { $user = $1; }
 
  open my $fh, "</etc/passwd";
  my @passwd = <$fh>;
  close $fh;
  @passwd = grep($_ =~ /^$ENV{'REMOTE_USER'}:/, @passwd);
  (my $tmp, $tmp, $credentials{'uid'}, $credentials{'group'}, $credentials{'description'}, $credentials{'home'}, $credentials{'shell'}) = split ":", $passwd[0];
  $credentials{'username'} = $ENV{'REMOTE_USER'};
  open my $fh, "</etc/group";
  my @groups;
  while(my $line = <$fh>)
  {
    my ($grp, $tmp, $gid, $users) = split ":", $line;
    my @users = split ",", $users;
    grep
    {
      chomp $_;
      if($_ eq $credentials{'username'}){ push @groups, $grp; }
    } @users;
  }
  close $fh;
  grep
  {
    my ($key, $value) = split "=", $_;
    chomp $value;
    $credentials{$key} = "yes";
  } @groups;
}

