forked from binfalse/monitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_passwd_exp.pl
executable file
·141 lines (114 loc) · 3.84 KB
/
check_passwd_exp.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/perl
# Copyright (C) 2017 Martin Scharm <https://binfalse.de/contact/>
#
# This file is part of bf-monitoring.
# <https://binfalse.de/software/nagios/>
# <https://github.com/binfalse/monitoring>
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use Net::LDAP;
use Getopt::Long;
sub get_days {
my $seconds = shift;
return int($seconds/(24*60*60)) . "d";
}
sub ldap_to_unix_time {
my $ldap = shift;
return $ldap / 10000000 - ((1970 - 1601) * 365 - 3 + (1970 - 1601) / 4) * 86400;
}
sub unix_to_ldap_time {
my $unix = shift;
return ($unix + ((1970 - 1601) * 365 - 3 + (1970 - 1601) / 4) * 86400) * 10000000;
}
sub ymd {
my $t = shift;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime ($t);
return sprintf ("%.4d-%.2d-%.2d", ($year + 1900), $mon, $mday);
}
sub usage {
my $msg = shift;
print $msg . "\n" if $msg;
print "Usage: $0 --ldapserver <LDAP SERVER> --ldapbase <LDAP BASE DN> [--max-age <SECONDS>] [--warning <SECONDS>] [--critical <SECONDS>] [--ignore-expired]\n";
print " max-age in seconds as configured in your institution, defaults to 999*24*60*60 (999 days)\n";
print " warning defaults to 30*24*60*60 (30 days)\n";
print " critical defaults to 5*24*60*60 (5 days)\n";
exit 23;
}
my $warning = 30*24*60*60;
my $critical = 5*24*60*60;
my $maxage = 999*24*60*60;
my $ldapserver = "";
my $ldapbase = "";
my $ignoreexpired = '';
GetOptions (
"ldapserver|s=s" => \$ldapserver,
"ldapbase|b=s" => \$ldapbase,
"ignore-expired|i" => \$ignoreexpired,
"max-age|m=s" => \$maxage,
"warning|w:i" => \$warning,
"critical|c:i" => \$critical)
or usage ("Error in command line arguments\n");
my $ldap = Net::LDAP->new ($ldapserver) or usage ($@);
$ldap->bind;
my $result = $ldap->search (
base => $ldapbase,
filter => "(&(objectClass=User)(objectClass=posixAccount))",
);
$ldap->unbind;
usage ($result->error) if $result->code;
my %ok = ();
my %problem = ();
my $serious = 1;
foreach my $entry ($result->entries) {
my $pwd_age = time() - ldap_to_unix_time ($entry->get_value("PWDLASTSET"));
my $last_day = ldap_to_unix_time ($entry->get_value("PWDLASTSET")) + $maxage;
if ($entry->get_value("ACCOUNTEXPIRES") == 0)
{
$ok{$entry->get_value("PWDLASTSET") + unix_to_ldap_time (time ())} = sprintf ("%s: does not expire (%s) ", $entry->get_value("gecos"), ymd ($last_day));
}
else
{
my $time_left = $last_day - time();
next if ($time_left < 0 and $ignoreexpired);
if ($time_left > $warning) {
$ok{$entry->get_value("PWDLASTSET")} = sprintf ("%s: %s left (%s) ", $entry->get_value("gecos"), get_days ($time_left), ymd ($last_day));
}
else {
$serious = 2 if ($time_left < $critical);
$problem{$entry->get_value("PWDLASTSET")} = sprintf ("%s: %s left (%s) ", $entry->get_value("gecos"), get_days ($time_left), ymd ($last_day));
}
}
}
if (keys %problem == 0) {
print "all " . $result->count . " accounts ok|";
foreach my $key (sort keys %ok) {
print $ok{$key};
}
print "\n";
exit 0;
}
else {
print "serious! " if ($serious == 2);
print ((keys %problem) . " problematic accounts: ");
foreach my $key (sort keys %problem) {
print $problem{$key};
}
print "|";
foreach my $key (sort keys %ok) {
print $ok{$key};
}
exit $serious;
}