-
Notifications
You must be signed in to change notification settings - Fork 0
/
aplol-adlds.pl
executable file
·249 lines (197 loc) · 6.38 KB
/
aplol-adlds.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/perl
use warnings;
use strict;
use Net::LDAP;
use Net::LDAP::Control::Paged;
use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
use Net::LDAP::Util qw(ldap_error_text);
use Fcntl qw(:flock);
# Updates LDAP-database with MAC-address from our database
# Load aplol
my $aplol_dir;
BEGIN {
use FindBin;
$aplol_dir = "$FindBin::Bin"; # Assume working-folder is the path where this script resides
}
use lib $aplol_dir;
use aplol;
my $aplol = aplol->new();
my %config = $aplol->get_config();
# variabler
my ($ldap, $pi_info);
my %ldap_info;
my ($added, $deleted) = (0, 0);
# Log
sub log_it{
$aplol->log_it("adlds", "@_");
}
# Logs debug-stuff if debug has been turned on
sub debug_log{
$aplol->debug_log("adlds", "@_");
}
# Logs error-stuff
sub error_log{
$aplol->error_log("adlds", "@_");
}
# Wrapper for a Net::LDAP call- assert the server message is a "success"
# code- die with a decoded error message if it isn't
sub ldapassert{
my $mesg = shift;
my $op = shift;
$mesg->code && die error_log("LDAP error" . ($op?" during $op":"") . ": " . ldap_error_text($mesg->code));
$mesg;
}
# Fetch stuff from LDAP into hash
sub fetch_from_ldap{
# Default LDAP allows 1000 entries per "page" requested
# We have to do pagination of results -- request 999 each time
my $page = Net::LDAP::Control::Paged->new(size => 999);
my $cookie;
my @search_args = (
base => $config{adlds}->{path},
scope => "sub",
filter => "(&)",
control => [$page],
);
while (1) {
my $ldap_search = ldapassert($ldap->search(@search_args), "MAC search");
if ($ldap_search->count < 1){
die error_log("No results.");
}
foreach my $ldap_entry ($ldap_search->entries){
# Fetch name of entry (so that we can skip entry equal to $config{adlds}->{path}).
my $dn = $ldap_entry->get_value('distinguishedName');
next if ($dn =~ m/^${config{adlds}->{path}}$/);
# Fetch other stuff
my $mac = $ldap_entry->get_value('displayName');
my $apname = $ldap_entry->get_value('adminDisplayName');
# Skip if no values
next unless ($mac && $apname);
# TODO: Check values for $mac-entry against MAC-regex
# Replace MAC so that we get same as from Cisco PI
$mac =~ s/\-/\:/g;
my %host = (
mac => $mac,
apname => $apname,
);
$ldap_info{$mac} = \%host;
}
# Do pagination-stuff
my $resp = $ldap_search->control(LDAP_CONTROL_PAGED) or last;
$cookie = $resp->cookie or last;
# Paging Control
$page->cookie($cookie);
}
if ($cookie){
error_log("Abnormal exit from LDAP-search.");
# Abnormal exit, so let the server know we do not want any more
$page->cookie($cookie);
$page->size(0);
$ldap->search(@search_args);
exit(-1);
}
}
# Sync entries
sub sync_entries{
# After fetching everything from PI and LDAP, we want to sync them
# PI-database should be authorative -- that is, if an item is deleted
# from PI, it should be deleted on LDAP, and if an item is added to
# PI, it should be added on LDAP. However, units deleted/added to LDAP
# should not reflect back to the PI-server.
# Since PI is authorative, we iterate through all entries found in PI.
# We then check if it's present on both PI and LDAP. We delete entries
# from the hashes that is present both places. Once iterated through all
# PI-entries, we can go ahead to add/delete from LDAP.
# 1) All entries still in the PI-hash should be added to LDAP.
# 2) All entries still in the LDAP-hash should be deleted from LDAP.
foreach my $ethmac (keys %$pi_info){
if ($ldap_info{$ethmac}){
# Entry exists both in LDAP and PI
# Delete from both places
delete($pi_info->{$ethmac});
delete($ldap_info{$ethmac});
## TODO: Update VD/location in LDAP here
}
}
# At this point we should add/delete to LDAP-database
# First, we delete
foreach my $mac (keys %ldap_info){
# delete from LDAP
delete_ldap_entry($mac, \%ldap_info);
}
# Then we add from PI
foreach my $ethmac (keys %$pi_info){
# add to LDAP
add_ldap_entry($ethmac, $pi_info);
}
}
# Delete entry from LDAP
sub delete_ldap_entry{
my ($mac, $hash) = @_;
debug_log("Deleting entry $mac ($hash->{$mac}{apname})...");
(my $prettymac = $mac) =~ s/\:/\-/g;
my $ldap_mac = "MAC_" . $prettymac;
my $entry_dn = "CN=$ldap_mac," . $config{adlds}->{path};
ldapassert($ldap->delete($entry_dn), "entry deletion");
$deleted++;
}
# Add entry to LDAP
sub add_ldap_entry{
my ($ethmac, $hash) = @_;
my $ap_name = $hash->{$ethmac}{name};
debug_log("Adding entry $ethmac ($ap_name)...");
(my $prettymac = $ethmac) =~ s/\:/\-/g;
my $ldap_mac = "MAC_" . $prettymac;
my $mac_dn = "CN=$ldap_mac," . $config{adlds}->{path};
my @attributes = (
objectClass => [ "top", "person", "organizationalPerson", "user" ],
cn => "$ldap_mac",
name => "$ldap_mac",
displayName => "$prettymac",
adminDisplayName => "$ap_name",
houseIdentifier => $config{adlds}->{create_message},
);
ldapassert($ldap->add($mac_dn, attrs => \@attributes), "MAC adding");
$added++;
# Add to group
# First, fetch group-entry
my @search_args = (
base => $config{adlds}->{group_dn},
scope => "sub",
filter => "(&)",
);
my $sr = ldapassert($ldap->search(@search_args), "group search");
if ($sr->count == 0) {
die error_log("Unknown group.");
} elsif ($sr->count > 1) {
die error_log("Ambiguous group.");
}
my $group_entry = $sr->shift_entry;
# Update the group entry
$group_entry->add(member => $mac_dn);
# Update LDAP
ldapassert($group_entry->update($ldap), "group update");
}
# We only want 1 instance of this script running
# Check if already running -- if so, abort.
unless (flock(DATA, LOCK_EX|LOCK_NB)) {
die("$0 is already running. Exiting.");
}
# connect
$aplol->connect();
$ldap = Net::LDAP->new($config{adlds}->{hostname} . ":" . $config{adlds}->{port}) or die error_log("LDAP server could not be contacted. Error: $@");
# Bind to LDAP
ldapassert($ldap->bind($config{adlds}->{username}, password=> $config{adlds}->{password}), "bind");
# Fetch AP's from PI
$pi_info = $aplol->get_active_aps();
# do our magic
fetch_from_ldap();
sync_entries();
# print stats
log_it("$added entries added.");
log_it("$deleted entries deleted.");
# disconnect
my $disconnect = $ldap->unbind;
$aplol->disconnect();
__DATA__
Do not remove. Makes sure flock() code above works as it should.