author | Dan Fuhry <dan@fuhry.us> |
Fri, 18 Jan 2013 19:59:50 -0500 | |
changeset 5 | cdd708efa505 |
parent 2 | 700d61d93b1b |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/perl |
2 |
||
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
use DB_File; |
|
6 |
use Net::LDAP; |
|
7 |
use YAML; |
|
8 |
||
9 |
open my $fp, "<", "/usr/local/etc/ssoinabox/webcreds.yml" or die "failed to open yaml"; |
|
10 |
my $config = YAML::LoadFile $fp; |
|
11 |
close $fp; |
|
12 |
||
13 |
# connect to LDAP |
|
14 |
my $ldap = Net::LDAP->new($config->{'ldap_server'}) |
|
15 |
or die "Failed to connect to LDAP: $!"; |
|
16 |
||
17 |
$ldap->bind($config->{'ldap_manager'}->{'dn'}, password => $config->{'ldap_manager'}->{'password'}) |
|
18 |
or die "Failed to bind to LDAP: $!"; |
|
19 |
||
20 |
# search for POSIX groups |
|
21 |
my $lr = $ldap->search( |
|
2
700d61d93b1b
Fix accidentally hardcoded ldap basedn
Dan Fuhry <dan@fuhry.us>
parents:
0
diff
changeset
|
22 |
base => $config->{'LDAP_BASEDN'} |
0 | 23 |
, filter => '(objectClass=posixGroup)' |
24 |
); |
|
25 |
||
26 |
die "Failed to search LDAP..." if ( $lr->code ); |
|
27 |
||
28 |
# Fetch each group from LDAP... |
|
29 |
my %users; |
|
30 |
||
31 |
foreach my $entry ($lr->entries) |
|
32 |
{ |
|
33 |
my $groupname = $entry->get_value('cn'); |
|
34 |
my $attrs = $entry->get_value('memberUID', asref => 1); |
|
35 |
foreach my $member (@$attrs) |
|
36 |
{ |
|
37 |
# Make this a user-based map, as that is what the DBM uses. |
|
38 |
$users{$member} = [] if !defined($users{$member}); |
|
39 |
push @{$users{$member}}, $groupname; |
|
40 |
} |
|
41 |
} |
|
42 |
||
43 |
# We're done with LDAP |
|
44 |
$ldap->unbind; |
|
45 |
||
46 |
# Prepare to write database file |
|
47 |
my $dbm_file = "/etc/apache2/ldap-groups"; |
|
48 |
my %dbm_hash; |
|
49 |
my ($key, $value); |
|
50 |
||
51 |
# Open database file |
|
52 |
tie %dbm_hash, "DB_File", $dbm_file, O_WRONLY or |
|
53 |
die "Unable to open DBM file $dbm_file: $!"; |
|
54 |
||
55 |
# write everything out |
|
56 |
while ( ($key, $value) = each(%users) ) |
|
57 |
{ |
|
58 |
$dbm_hash{$key} = sprintf('*:%s', join(',', @{$users{$key}})); |
|
59 |
} |
|
60 |
||
61 |
# Save and close database |
|
62 |
untie %dbm_hash; |
|
63 |
||
64 |
exit 0; |
|
65 |
||
66 |
# debug - for viewing contents of the map |
|
67 |
||
68 |
tie %dbm_hash, "DB_File", $dbm_file, O_RDONLY or |
|
69 |
die "Unable to open DBM file $dbm_file: $!"; |
|
70 |
||
71 |
while ( ($key, $value) = each(%dbm_hash) ) |
|
72 |
{ |
|
73 |
print "$key => $value\n"; |
|
74 |
} |