-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathspreadPwdChangedTime.pl
More file actions
executable file
·216 lines (163 loc) · 5.19 KB
/
spreadPwdChangedTime.pl
File metadata and controls
executable file
·216 lines (163 loc) · 5.19 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
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use Net::LDAP;
use Net::LDAP::Control;
use Net::LDAP::Constant qw( LDAP_CONTROL_RELAX );
use Getopt::Long;
################################################################################
# Variables
################################################################################
my $force;
my $verbose;
my $help;
GetOptions ( "force|f" => \$force,
"verbose|v" => \$verbose,
"help|h" => \$help
)
or &usage();
my (
$uri,
$base,
$filter,
$binddn,
$bindpw,
$min,
$max
) = @ARGV;
my $users; # { dn => { replace => { "pwdChangedTime" => "20250101120000Z" } } }
################################################################################
# Functions
################################################################################
sub usage
{
print "Missing or bad argument\n";
print "USAGE: $0 [-h] [-f] [-v] <uri> <base> <filter> <binddn> <bindpw> <min> <max>\n";
print "DESCRIPTION: find users and spread homogeneously their pwdChangedTime from min to max days ago\n";
print " * -f: option to force applying modifications of pwdChangedTime\n";
print " * -h: display this help message\n";
print " * -v: verbose mode\n";
print " * uri: FQDN or LDAP uri, like ldap://host.domain.com or ldaps://host.domain.com\n";
print " * base: LDAP search base\n";
print " * filter: LDAP filter for selecting users\n";
print " * binddn: service account that binds for searching users and modifying pwdChangedTime\n";
print " * bindpw: password for service account\n";
print " * min: change password from min days ago\n";
print " * max: change password up to max days ago\n";
exit 1;
}
sub get_users_dn
{
my ($uri, $base, $filter, $binddn, $bindpw) = @_;
my $result;
my $ldap = Net::LDAP->new( $uri )
or die "Unable to connect to LDAP server $uri: $@";
my $bind_result = $ldap->bind( "$binddn",
password => "$bindpw" );
$bind_result->code and die $bind_result->error;
my $search_result = $ldap->search(
base => $base,
filter => $filter,
scope => "sub",
attrs => [ 'pwdChangedTime' ]
);
$search_result->code and die $search_result->error;
foreach my $entry ($search_result->entries)
{
$result->{$entry->dn} = {};
}
$ldap->unbind;
return $result;
}
sub compute_pwd_changed_time
{
my ( $users, $min, $max ) = @_;
my $now = DateTime->now;
my $date;
my $days = $min;
foreach my $user ( keys %$users )
{
$date = $now->clone();
$date->subtract(days => $days);
#print "days: $days " . $date->strftime('%Y%m%d%H%M%SZ')."\n";
$users->{$user} = { replace => { "pwdChangedTime" => $date->strftime('%Y%m%d000000Z') } };
if( $days < $max)
{
$days++;
}
else
{
$days = $min;
}
}
return $users;
}
sub display_modifications
{
my ( $users ) = @_;
print "\nModifications to apply to LDAP directory\n";
print "----------------------------------------\n";
foreach my $user ( keys %$users )
{
my $date = $users->{$user}->{replace}->{pwdChangedTime};
$date =~ s/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z/$1-$2-$3 $4:$5:$6/;
print sprintf "User: %-64s pwdChangedTime: %s\n", "$user", "$date";
}
}
sub apply_modifications
{
my ($uri, $base, $filter, $binddn, $bindpw, $users) = @_;
my $ldap = Net::LDAP->new( $uri )
or die "Unable to connect to LDAP server $uri: $@";
my $bind_result = $ldap->bind( "$binddn",
password => "$bindpw" );
$bind_result->code and die $bind_result->error;
my $relax_control = Net::LDAP::Control->new( type => LDAP_CONTROL_RELAX );
my $mod;
foreach my $user ( keys %$users )
{
$mod = $ldap->modify( $user, %{ $users->{$user} }, control => [ $relax_control ] );
$mod->code and die "Error while modifying $user: " . $mod->error;
}
$ldap->unbind;
}
################################################################################
# Entry point
################################################################################
if( $help or !$uri or !$base or !$filter or !$binddn or !$bindpw or !$min or !$max )
{
&usage();
}
unless( $min =~ /^\d+$/ )
{
print "min: $min is not an integer\n";
exit 1;
}
unless( $max =~ /^\d+$/ )
{
print "max: $max is not an integer\n";
exit 1;
}
$users = &get_users_dn($uri, $base, $filter, $binddn, $bindpw);
if(! keys %$users)
{
print "No users found, aborting\n";
exit 2;
}
$users = &compute_pwd_changed_time( $users, $min, $max );
if($verbose)
{
&display_modifications( $users );
}
print "\nNumber of modifications to apply: ".scalar(keys %$users)."\n";
if($force)
{
&apply_modifications($uri, $base, $filter, $binddn, $bindpw, $users);
print "Modifications successfully applied\n";
}
else
{
print "Modifications not applied (use -f if you want to)\n";
}
exit 0;