-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemAccountAnalyzer.pl
More file actions
231 lines (200 loc) · 5.34 KB
/
systemAccountAnalyzer.pl
File metadata and controls
231 lines (200 loc) · 5.34 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/perl
=head1 TITLE
systemAccountAnalyzer.pl
=head1 SYNOPSIS
perl_example.pl [ -h|-v ]
=head1 DESCRIPTION
This Perl code reads in a shadow file and alerts on any user whos password is going to expire inside of the warning period.
=head1 ARGUMENTS
Place
--help print Options and Arguments
--version print version information
=head1 OPTIONS
=head1 LICENSE
=head1 AUTHOR
Copyright (C) Jon Wright 2010
All rights reserved
=cut
use strict 'vars';
use warnings;
use Getopt::Std;
use Pod::Usage;
use Sys::Syslog;
use Fcntl;
use Switch;
use Time::Local;
use constant VERSION => '1.0.0';
use constant LOGTYPE => '3';
use constant DEFSHADOWFILE => '/etc/shadow';
use constant LOGABR => 'pep';
use constant LVLREGEX => qr/emerg|alert|crit|err|warning|notice|info|debug/;
use constant MSGREGEX => qr/([-\@\w.]+)$/;
use constant DEFSYSLVL => 'err';
use constant DEFSYSFLY => 'local7';
use constant EXPDATE => '60';
use constant WARNDATE => '14';
use constant DELDATE => '90';
use constant SECSINDAY => '86400';
sub logit{
my ($level, $message) = @_;
if ($level =~ LVLREGEX) {
if ($message =~ MSGREGEX ) {
switch (LOGTYPE) {
case 0 {
&printMsg(STDOUT,$level,$message);
}
case 1 {
&printMsg(STDOUT,$level,$message);
}
case 2 {
&syslogMsg($level,$message);
}
case 3 {
&printMsg(STDOUT,$level,$message);
&syslogMsg($level,$message);
}
case 4 {
&printMsg(STDERR,$level,$message);
&syslogMsg($level,$message);
}
}
}
else {
&printMsg(STDERR,DEFSYSLVL,"The Message contains values that are non-alphanumeric or non-valid special characters".$message);
}
}
else {
&printMsg(STDERR,DEFSYSLVL,"The Level Argument contains values to are not a valid Message Level");
}
return 1;
}
sub printMsg {
my $fileHandler = shift;
my ($prLvl, $prMsg) = @_;
if ($prLvl =~ LVLREGEX) {
if ($prMsg =~ MSGREGEX) {
printf($fileHandler "%s [%s] %s\n",LOGABR, $prLvl,$prMsg);
}
else {
printf(STDERR "%s [%s] %s\n",LOGABR, DEFSYSLVL,"The Message contains values that are non-alphanumeric or non-valid special characters");
}
}
else {
printf(STDERR "%s [%s] %s\n",LOGABR, DEFSYSLVL,"The Level Argument contains values to are not a valid Message Level");
}
return 1;
}
sub syslogMsg{
my ($sysLvl, $sysMsg) = @_;
if ($sysLvl =~ LVLREGEX) {
if ($sysMsg =~ MSGREGEX) {
openlog(LOGABBR, 'cons,pid', 'user');
syslog(DEFSYSFLY.".".$sysLvl, $sysMsg);
closelog();
}
else {
$sysMsg = "The Message contains values that are non-alphanumeric or non-valid special characters";
&printMsg(STDERR,DEFSYSLVL,$sysMsg);
openlog(LOGABBR, 'cons,pid', 'user');
syslog(DEFSYSFLY.".".DEFSYSLVL, $sysMsg);
closelog();
}
}
else {
$sysMsg = "The Level Argument contains values to are not a valid Message Level";
&printMsg(STDERR,DEFSYSLVL,$sysMsg);
openlog(LOGABBR, 'cons,pid', 'user');
syslog(DEFSYSFLY.".".DEFSYSLVL, $sysMsg);
closelog();
}
return 1;
}
sub alertMsg{
my ($altUsr, $altLvl, $altMsg) = @_;
if ($altLvl =~ LVLREGEX) {
if ($altMsg =~ MSGREGEX) {
logit($altLvl,$altMsg." : ".$altUsr);
}
else {
&logit(DEFSYSLVL,"The Message contains values that are non-alphanumeric or non-valid special characters");
}
}
else {
&logit(DEFSYSLVL,"The Level Argument contains values to are not a valid Message Level");
}
return 1;
}
sub main::HELP_MESSAGE{
&pod2usage();
return 1;
}
sub main::VERSION_MESSAGE{
printf(STDERR "Version: %s",VERSION);
return 1;
}
sub determineAlert {
my ($name,$lastSetDate,$expDate,$warnDate) = @_;
my $currentTime = timegm(gmtime());
my $dateValue;
if((!defined($expDate)) || ($expDate !~ m/^\d+$/)){
$expDate=EXPDATE;
}
if((!defined($warnDate)) || ($warnDate !~ m/^\d+$/)){
$warnDate=WARNDATE;
}
if((!defined($expDate)) || ($lastSetDate !~ m/^\d+$/)){
$lastSetDate=0;
&alertMsg($name,"alert","This user account has never logged in");
}
$dateValue = (($currentTime - ($lastSetDate + (($expDate * SECSINDAY) - ($warnDate * SECSINDAY))))/SECSINDAY);
if (($dateValue >= ($warnDate * -1) && ($dateValue <=1))) {
&alertMsg($name,"info","This user account expires in ".$dateValue." days.");
}
elsif (($dateValue > -1) && ($dateValue <= 0)){
&alertMsg($name,"notice","This user account expires today.");
}
elsif(($dateValue > 0) && ($dateValue < DELDATE)){
&alertMsg($name,"warning","This user account expired today.");
}
elsif($dateValue > DELDATE){
&alertMsg($name,"alert", "This user account should be deleted");
}
return 1;
}
sub alertOnExpiringAccounts {
my ($filename) = @_;
my @shadowLineArray = ();
my $line;
if (-e $filename) {
if(sysopen(FH,$filename,O_RDONLY)){
while($line = <FH>){
@shadowLineArray = split(/:/, $line);
chomp(@shadowLineArray);
if (($shadowLineArray[1] !~ /^NP$/) && ($shadowLineArray[0] !~ /^root$/)){
determineAlert($shadowLineArray[0],$shadowLineArray[2],$shadowLineArray[4],$shadowLineArray[5]);
}
}
}
close(FH);
}
else {
logit(INFO,"Unable to locate 'SHADOW File: ".$filename);
}
}
sub main {
my %options;
&getopts('hvf:', \%options); # options as above. Values in %opts
if (defined $options{h}){
&main::HELP_MESSAGE;
}
elsif (defined $options{v}){
&main::VERSION_MESSAGE;
}
elsif (defined $options{f}){
&alertOnExpiringAccounts($options{f});
}
else {
&alertOnExpiringAccounts(DEFSHADOWFILE);
}
}
&main;