#!/usr/bin/perl -w
#
# check_pass_expire.pl - nagios plugin
#
# by eldespistado.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#



use strict;
use Getopt::Long;
use vars qw($PROGNAME);
use lib ".";
#use lib "/usr/lib/nagios/plugins" ;
use utils qw (%ERRORS &print_revision &support);

sub print_help ();
sub print_usage ();

my ($opt_V, $opt_h, $opt_u, $opt_w, $opt_c);
my ($result, $message, $age, $size, $st);


$opt_u="";
$opt_w="10";
$opt_c="5";

# ***** thisplugin ************
sub nagexit($$);
$PROGNAME="check_pass_expire.pl";
my $VERSION="0.1";
my $PROGBY="www.eldespistado.com";

my ($shadow_user, $shadow_passwd, $shadow_last_pass_change, $shadow_minimun, $shadow_maximun, $shadow_warn, $shadow_inactive, $shadow_expire);
my ($CURRENT_EPOCH, $EPOCH, $AGE, $MAX, $EXPIRE);
my $USERINFO = '';
# ***** end thisplugin ************

Getopt::Long::Configure('bundling');
GetOptions(
        "V"   => \$opt_V, "version"     => \$opt_V,
        "h"   => \$opt_h, "help"        => \$opt_h,
        "u=s" => \$opt_u, "user"        => \$opt_u,
        "w=s" => \$opt_w, "warning=s" => \$opt_w,
        "c=s" => \$opt_c, "critical=s" => \$opt_c);

if ($opt_V) {
        print "Nagios plugin: $PROGNAME\n";
        print "Version: $VERSION\n";
        print "By: $PROGBY\n\n";
        exit $ERRORS{'OK'};
}

if ($opt_h) {
        print_help();
        exit $ERRORS{'OK'};
}

if (! $opt_u) {
        print "No USER specified\n\n";
        print_usage();
        exit $ERRORS{'UNKNOWN'};
}



sub print_usage () {
        print "Usage:\n";
        print "  $PROGNAME -u <username> [-w <INTEGER>] [-c <INTEGER>]\n";
        print "  $PROGNAME [-h | --help]\n";
        print "  $PROGNAME [-V | --version]\n";
	print "\n\nOptions:\n";
	print "  -u, --username\n";
	print "     Username to check in /etc/shadow\n";
         print "  -w, --warning\n";
        print "     Days. Less than this days is Warning\n";
        print "     Default Warning value is $opt_w days \n";
        print "  -c, --critical\n";
        print "     Days. Less than this days is Critical\n";
        print "     Default Critical value is $opt_c days \n";
        print "  -h, --help\n";
        print "     Print detailed help screen\n";
        print "  -V, --version\n";
        print "     Print version information\n\n";
        print " **** ATENTION: Perhaps you need change grep path in line *****";
        print " \$USERINFO=`/bin/grep \$opt_u /etc/shadow`; *****";
        print "\n\n";


}

sub print_help () {
        print_usage();
        print "\n";
#        support();
}

# ***** thisplugin ************

#Get user line from /etc/shadow and save all data.
$USERINFO=`/bin/grep $opt_u /etc/shadow`;
($shadow_user, $shadow_passwd, $shadow_last_pass_change, $shadow_minimun, $shadow_maximun, $shadow_warn, $shadow_inactive, $shadow_expire) = split(/:/, $USERINFO);

if (! $shadow_user) {
     #print "User not found\n\n";
     nagexit($ERRORS{'UNKNOWN'}, "UNKNOWN, Invalid user or not found in /etc/shadow")
} 

# Find the epoch time since the user's password was last changed
$EPOCH= int(time/(60*60*24));

# Compute the age of the user's password
$AGE= $EPOCH - $shadow_last_pass_change;

# Compute and display the number of days until password expiration
$EXPIRE= $shadow_maximun - $AGE;

# Compare range values and exit
if ($opt_c >= $EXPIRE) {
     nagexit($ERRORS{'CRITICAL'}, "CRITICAL, password expire in $EXPIRE days")
}elsif ($opt_w >= $EXPIRE) {
    nagexit($ERRORS{'WARNING'}, "WARNING, password expire in $EXPIRE days")
}else {
    nagexit($ERRORS{'OK'}, "OK, password expire in $EXPIRE days")
}

# ***** end thisplugin ************


sub nagexit($$) {
	my $errlevel = shift;
	my $string = shift;
	print "$errlevel: $string\n";
	exit $errlevel;
}

# DEBUG
#print "Given Options:\n";
#printf ("Username: %s\n",$opt_u);
#printf ("Warning %s\n",$opt_w);
#printf ("Critical %s\n",$opt_c);





