-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupdump.pm
53 lines (46 loc) · 1.44 KB
/
groupdump.pm
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
package SRP::Command::groupdump;
use Mojo::Base 'Mojolicious::Command';
use Net::LDAP qw(LDAP_SUCCESS LDAP_INSUFFICIENT_ACCESS);
use Net::LDAP::Util qw(canonical_dn);
has description => 'Dump security groups';
has usage => "Usage: APPLICATION groupdump\n";
sub run {
my $self = shift;
my $app = $self->app;
# ldap operations
my $ldap = Net::LDAP->new($app->config('ldapservers'), port => 389, timeout => 10);
unless ($ldap) {
$app->log->fatal("Ldap connection error. Create object failed. $@");
exit 1;
}
my $mesg = $ldap->bind($app->config('ldapuser'), password => $app->config('ldappass'), version => 3);
if ($mesg->code) {
$app->log->fatal('Ldap bind error: '.$mesg->error);
exit 1;
}
foreach my $sg (@{$app->config('sec_groups')}) {
say "Group: ".$sg->{dn};
# search ldap for group members
my $res = $ldap->search(base => $sg->{dn}, filter => '(objectClass=group)', scope => 'base', attrs => ['member']);
if ($res->code) {
$app->log->fatal('Ldap group search error: '.$res->error);
$ldap->unbind;
exit 1;
}
if ($res->count > 0) {
my $entry = $res->entry(0);
foreach my $m ($entry->get_value('member')) {
say "Member: ".canonical_dn($m);
}
} else {
$app->log->fatal("Ldap group $sg->{dn} is not found in active directory");
$ldap->unbind;
exit 1;
}
print "\n";
} #foreach my $sg
$ldap->unbind;
$ldap = undef;
exit 0;
}
1;