-
Notifications
You must be signed in to change notification settings - Fork 0
/
egroup2anything.pl
151 lines (119 loc) · 4.33 KB
/
egroup2anything.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
141
142
143
144
145
146
147
148
149
#! /usr/bin/perl -w
# Example: egroup2anything.pl -egroup lxsoft-admins -user mejias -format "%[email protected]"
# TODO:
# - include newline in format...
#
#
use strict;
use diagnostics;
use Getopt::Long;
use Data::Dumper;
sub egroup2username(@);
my $debug = my $unique = my $outfile = my $recursion = my $chkaccount = my $attribute = undef;
my @egroup = my @username = (); #qw(lxsoft-admins it-dep-pes-ps);
my $FORMAT = "%s";
my %opts = ("debug" => \$debug,
"unique" => \$unique,
"format=s" => \$FORMAT,
"egroup=s@" => \@egroup,
"user=s@" => \@username,
"outfile=s" => \$outfile,
"allow-nested-egroups" => \$recursion,
"check-account" => \$chkaccount,
"attribute=s" => \$attribute,
);
my $options = GetOptions(%opts);
print "\$FORMAT = \"$FORMAT\"\n" if $debug;
print "0 \@username = @username\n" if $debug;
push(@username,egroup2username({recursion => $recursion,
attribute => $attribute},
@egroup)) if @egroup;
# prune the list of usernames
print "1 \@username = @username\n" if $debug;
@username = sort keys %{{map{$_=>1} @username}} if $unique; # remove duplicate entries
print "2 \@username = @username\n" if $debug;
@username = grep {getpwnam($_) && $_} @username if $chkaccount; # remove non-existent accounts
print "3 \@username = @username\n" if $debug;
if ($outfile){
open(F,"> $outfile") or die "Cannot open $outfile: $!";
map {printf F "$FORMAT\n",$_} @username;
close(F);
}else{
map {printf STDOUT "$FORMAT\n",$_} @username;
}
sub egroup2username(@){
my %opt = ();
if (ref($_[0]) eq "HASH"){
my $href = shift @_;
%opt = %$href;
}
my @egroup = @_;
#print Dumper(\%opt,\@egroup);exit;
@egroup = keys %{{map{$_=>1} @egroup}}; # remove duplicate entries
my $recursion = $opt{recursion};
my $attribute = $opt{attribute} || "sAMAccountName";
my @result = ();
# Set up LDAP connection
use Net::LDAP;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
my $ldapserver = "xldap.cern.ch";
my $ldap = Net::LDAP->new($ldapserver,
version => 3,
multihomed => 1,
timeout => 15 );
if (not $ldap){
print SDTERR "Error connecting to LDAP: $@\n";
return undef;
}
my $page = Net::LDAP::Control::Paged->new( size => 1000 );
my $mesg = $ldap->bind;
if (not $mesg){
print SDTERR "Error binding to LDAP: $@\n";
return undef;
}
# Define the query
my $query = undef;
if (scalar(@egroup) == 1){
$query = "memberOf". ($recursion ? ":1.2.840.113556.1.4.1941:" : "") ."=CN=" . $egroup[0] . ",OU=e-groups,OU=Workgroups,DC=cern,DC=ch";
}else{
$query = "|";
for my $egroup (@egroup){
$query .= "(memberOf". ($recursion ? ":1.2.840.113556.1.4.1941:" : "") ."=CN=" . $egroup . ",OU=e-groups,OU=Workgroups,DC=cern,DC=ch)";
}
}
print "\$query = \"$query\"\n" if $debug;
my @args = (base => "ou=users,ou=organic units,dc=cern,dc=ch",
scope => "sub",
filter => "$query",
attrs => [$attribute],
control => [$page],
);
my $cookie;
while (1) {
# Perform query
my $mesg = $ldap->search(@args);
# Only continue on LDAP_SUCCESS
$mesg->code and last;
# Process the result
my @entries = $mesg->entries;
printf "LDAP return: %d entries.\n",scalar(@entries) if $debug;
foreach my $entr (@entries) {
my $result = $entr->get_value($attribute);
push(@result,$result) if $result;
}
# Get cookie from paged control
my ($resp) = $mesg->control( LDAP_CONTROL_PAGED ) or last;
$cookie = $resp->cookie or last;
# Set cookie in paged control
$page->cookie($cookie);
}
if ($cookie) {
# We had an abnormal exit, so let the server know we do not want any more
$page->cookie($cookie);
$page->size(0);
$ldap->search(@args);
}
return @result;
}
__END__