-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetLdap.pm
263 lines (232 loc) · 6.07 KB
/
NetLdap.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# Net::LDAP wrapper routines.
#
# Copyright (C) 2008 - 2009 Nano-opt
# Licence: GPL
package NetLdap;
use strict;
use Net::LDAP;
use Constants;
use LMConfig;
use ParseExop;
use base qw(Exporter);
%NetLdap::EXPORT = qw(
new
bind
SearchUID
GetAllUID
SearchMemberGroups
SearchMemberGroupsGN
GetAllGID
GetAllGN
GetDNFromUID
GetAttrsFromUID
GetAllUIDPhoto
Entry_AddAttr
Entry_ReplaceAttr
Entry_DelAttr
ChangePassword
);
my ($obj_ldap, $ldap_bind);
sub new {
my ($self) = @_;
$obj_ldap = Net::LDAP->new(LDAP_URI) or return undef;
$ldap_bind = FALSE;
return $self;
}
sub bind {
my ($self, $dn, $opt) = @_;
my $msg;
if ($ldap_bind) {
$obj_ldap->unbind;
}
if (defined($dn)) {
$msg = $obj_ldap->bind($dn, %$opt);
} else {
$msg = $obj_ldap->bind;
}
if (! $msg->code) {
$ldap_bind = TRUE;
}
return $ldap_bind;
}
sub SearchUID {
my ($self, $uid, @attrs) = @_;
if (! $ldap_bind) {return undef; }
my %ldap_attr;
$ldap_attr{base} = LDAP_BASEDN;
$ldap_attr{filter} = "(uid=$uid)";
$ldap_attr{scope} = "sub";
if ($#attrs > -1) {
push(@attrs);
$ldap_attr{attrs} = \@attrs;
}
my $ldap_res = $obj_ldap->search(%ldap_attr);
return $ldap_res->entries;
}
sub GetAllUID {
my ($self, @attrs) = @_;
if (! $ldap_bind) {return undef; }
my %ldap_attr;
$ldap_attr{base} = LDAP_BASEDN;
$ldap_attr{filter} = "(uid=*)";
$ldap_attr{scope} = "sub";
if ($#attrs > -1) {
push(@attrs);
$ldap_attr{attrs} = \@attrs;
}
my $ldap_res = $obj_ldap->search(%ldap_attr);
return $ldap_res->entries;
}
sub GetAllGID {
my ($self, @attrs) = @_;
if (! $ldap_bind) {return undef; }
my %ldap_attr;
$ldap_attr{base} = LDAP_BASEDN;
$ldap_attr{filter} = "(objectClass=posixGroup)";
$ldap_attr{scope} = "sub";
if ($#attrs > -1) {
push(@attrs);
$ldap_attr{attrs} = \@attrs;
}
my $ldap_res = $obj_ldap->search(%ldap_attr);
return $ldap_res->entries;
}
sub GetAllGN {
my ($self, @attrs) = @_;
if (! $ldap_bind) {return undef; }
my %ldap_attr;
$ldap_attr{base} = LDAP_BASEDN;
$ldap_attr{filter} = "(objectClass=groupOfNames)";
$ldap_attr{scope} = "sub";
if ($#attrs > -1) {
push(@attrs);
$ldap_attr{attrs} = \@attrs;
}
my $ldap_res = $obj_ldap->search(%ldap_attr);
return $ldap_res->entries;
}
sub GetAllUIDPhoto {
my ($self, @attrs) = @_;
if (! $ldap_bind) {return undef; }
my %ldap_attr;
$ldap_attr{base} = LDAP_BASEDN;
$ldap_attr{filter} = "(jpegPhoto=*)";
$ldap_attr{scope} = "sub";
if ($#attrs > -1) {
push(@attrs);
$ldap_attr{attrs} = \@attrs;
}
my $ldap_res = $obj_ldap->search(%ldap_attr);
return $ldap_res->entries;
}
sub SearchMemberGroups {
my ($self, $uid) = @_;
if (! $ldap_bind) {return undef; }
if (! defined($uid)) {return undef; }
my @groups;
my %ldap_attr;
$ldap_attr{base} = LDAP_BASEDN;
$ldap_attr{filter} = "(memberUid=$uid)";
$ldap_attr{scope} = "sub";
my $ldap_res = $obj_ldap->search(%ldap_attr);
if ($ldap_res->code != 0) {return undef; }
foreach ($ldap_res->entries) {
push(@groups, $_->get_value('cn'));
}
return \@groups;
}
sub SearchMemberGroupsGN {
my ($self, $dn) = @_;
if (! $ldap_bind) {return undef; }
if (! defined($dn)) {return undef; }
my @groups;
my %ldap_attr;
$ldap_attr{base} = LDAP_BASEDN;
$ldap_attr{filter} = "(member=$dn)";
$ldap_attr{scope} = "sub";
my $ldap_res = $obj_ldap->search(%ldap_attr);
if ($ldap_res->code != 0) {return undef; }
foreach ($ldap_res->entries) {
push(@groups, $_->get_value('cn'));
}
return \@groups;
}
sub Entry_AddAttr {
my ($self, $target, $name, $value) = @_;
return $obj_ldap->modify($target, add => {$name => $value} );
}
sub Entry_ReplaceAttr {
my ($self, $target, $name, $value) = @_;
return $obj_ldap->modify($target, replace => {$name => [ $value] } );
}
sub Entry_DelAttr {
my ($self, $target, $name, $value) = @_;
return $obj_ldap->modify($target, delete => {$name => $value} );
}
sub GetDNFromUID {
my ($self, $uid) = @_;
my @entries = $self->SearchUID($uid);
if ($#entries != 0) {
return undef;
}
return $entries[0]->dn;
}
sub GetAttrsFromUID {
my ($self, $uid, @attr) = @_;
my %ret;
my @entries = $self->SearchUID($uid, @attr);
if ($#entries == 0) {
my $entry = $entries[0];
foreach (@attr) {
if ($entry->exists($_)) {
$ret{$_} = $entry->get_value($_, asref => 1);
}
}
}
return \%ret;
}
sub ChangePassword {
my ($self, $dn, $oldpass, $newpass) = @_;
# check
my ($chash, $nhash);
if (HASH_HISTORY ne '') {
my $hist = Constants::LOCATIONS()->{'data'} . '/' . HASH_HISTORY;
open(HDAT, $hist);
foreach (<HDAT>) {
chomp;
# assume {SSHA}
$chash = ParseExop::ParseSSHA(substr($_, 6));
$nhash = ParseExop::ExecSSHA($newpass, $chash->{'salt'});
$nhash = ParseExop::ExecHash('ssha', $nhash);
if ($nhash eq $_) {
return ERROR_PASS_HISTORY;
}
}
close(HDAT);
}
# exec change
my $ldap = Net::LDAP->new(LDAP_URI);
my $msg;
$msg = $ldap->bind($dn, password => $oldpass);
if ($msg->code) {
# might be better to return $msg object..
return ERROR_AUTH_PASS;
}
my $hash = ParseExop::ExecSSHA($newpass, ParseExop::GetRandomSalt);
$hash = ParseExop::ExecHash('ssha', $hash);
$msg = $ldap->modify($dn, replace => {'userPassword' => $hash} );
if ($msg->code) {
return ERROR_LDAP;
}
# write
if (HASH_HISTORY ne '') {
my $hist = Constants::LOCATIONS()->{'data'} . '/' . HASH_HISTORY;
open(OHIST, ">> $hist");
print OHIST "$hash\n";
close(OHIST);
}
return ERROR_SUCCESS;
}
1;