#!/usr/bin/perl

# sudoTs
# This script reports on the 'sudo' timestamp for the username given
# as a command-line argument. If no command-line argument is given, it will
# report on the current user.
# 
# This script must be run with 'root' privileges
# so it can read the /var/run/sudo directory
# Note that using 'sudo' to run the script is useless if reporting on the
# current user since each use of 'sudo' will reset the timestamp.
# Thus I suggest starting a sub-shell via 'sudo -s'
# and running the script from this sub-shell. Of course you should be careful
# to exit from this sub-shell when finished using the script.
#
# Cameron Hayne (macdev@hayne.net)  January 2005

use strict;
use warnings;
use File::stat;

my $username; 
if (scalar(@ARGV) >= 1)
{
    $username = $ARGV[0];
}
else
{
    $username = $ENV{'SUDO_USER'};
    unless ($username)
    {
        print "This script must be run from a sub-shell started by 'sudo -s'\n";
        exit;
    }
}

my $timestamp = "/var/run/sudo/$username";

my $info = stat($timestamp) or die "Can't stat $timestamp: $!\n";
my $timestampCTime = $info->ctime;
my $timestampMTime = $info->mtime;

my $nowTime = time();
my $nowDate = localtime($nowTime);
my $timestampCDate = localtime($timestampCTime);
my $timestampMDate = localtime($timestampMTime);
my $elapsed = $nowTime - $timestampMTime;

print "sudo timestamp: $timestampMDate ($elapsed seconds ago)\n";
