Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/LDAP.pm

raw
1 ##@file
2 # LDAP configuration backend
3
4 ##@class
5 # Implements LDAP backend for Lemonldap::NG
6 package Lemonldap::NG::Common::Conf::Backends::LDAP;
7
8 use strict;
9 use utf8;
10 use Net::LDAP;
11 use Lemonldap::NG::Common::Conf::Constants; #inherits
12 use Lemonldap::NG::Common::Conf::Serializer;
13
14 our $VERSION = '2.23.0';
15
16 BEGIN {
17 *Lemonldap::NG::Common::Conf::getLdapConnection = \&getLdapConnection;
18 }
19
20 sub prereq {
21 my $self = shift;
22 foreach ( 'ldapServer', 'ldapConfBase', 'ldapBindDN', 'ldapBindPassword' ) {
23 unless ( $self->{$_} ) {
24 $Lemonldap::NG::Common::Conf::msg .=
25 "$_ is required in LDAP configuration type \n";
26 return 0;
27 }
28 }
29 $self->{ldapObjectClass} ||= "applicationProcess";
30 $self->{ldapAttributeId} ||= "cn";
31 $self->{ldapAttributeContent} ||= "description";
32 1;
33 }
34
35 sub available {
36 my $self = shift;
37
38 my $ldap = $self->getLdapConnection;
39 unless ($ldap) {
40 return 0;
41 }
42
43 my $search = $ldap->search(
44 base => $self->{ldapConfBase},
45 filter => '(objectClass=' . $self->{ldapObjectClass} . ')',
46 scope => 'one',
47 attrs => [ $self->{ldapAttributeId} ],
48 );
49 $ldap->unbind;
50
51 if ( $search->code ) {
52 $self->logError($search);
53 return 0;
54 }
55
56 my @entries = $search->entries();
57 my @conf;
58 foreach (@entries) {
59 my $cn = $_->get_value( $self->{ldapAttributeId} );
60 my ($cfgNum) = ( $cn =~ /lmConf-(\d*)/ );
61 push @conf, $cfgNum;
62 }
63 return sort { $a <=> $b } @conf;
64 }
65
66 sub lastCfg {
67 my $self = shift;
68 my @avail = $self->available;
69 return $avail[$#avail];
70 }
71
72 sub getLdapConnection {
73 my $self = shift;
74
75 # Parse servers configuration
76 my $useTls = 0;
77 my $tlsParam;
78 my @servers = ();
79 foreach my $server ( split /[\s,]+/, $self->{ldapServer} ) {
80 if ( $server =~ m{^ldap\+tls://([^/]+)/?\??(.*)$} ) {
81 $useTls = 1;
82 $server = $1;
83 $tlsParam = $2 || "";
84 }
85 else {
86 $useTls = 0;
87 }
88 push @servers, $server;
89 }
90
91 # Connect
92 my $ldap = Net::LDAP->new(
93 \@servers,
94 keepalive => 1,
95 onerror => undef,
96 verify => ( $self->{ldapVerify} || "require" ),
97 ( $self->{ldapCAFile} ? ( cafile => $self->{ldapCAFile} ) : () ),
98 ( $self->{ldapCAPath} ? ( capath => $self->{ldapCAPath} ) : () ),
99 ( $self->{ldapPort} ? ( port => $self->{ldapPort} ) : () ),
100 raw => qr/(?i:^jpegPhoto|;binary)/
101 );
102
103 unless ($ldap) {
104 $Lemonldap::NG::Common::Conf::msg .= "$@\n";
105 return;
106 }
107 elsif ( $Net::LDAP::VERSION < '0.64' ) {
108
109 # CentOS7 has a bug in which IO::Socket::SSL will return a broken
110 # socket when certificate validation fails. Net::LDAP does not catch
111 # it, and the process ends up crashing.
112 # As a precaution, make sure the underlying socket is doing fine:
113 if ( $ldap->socket->isa('IO::Socket::SSL')
114 and $ldap->socket->errstr < 0 )
115 {
116 $Lemonldap::NG::Common::Conf::msg .=
117 "SSL connection error: " . $ldap->socket->errstr;
118 return;
119 }
120 }
121
122 # Start TLS if needed
123 if ($useTls) {
124 my %h = split( /[&=]/, $tlsParam );
125 $h{verify} ||= $self->{ldapVerify} || "require";
126 $h{cafile} ||= $self->{ldapCAFile} if ( $self->{ldapCAFile} );
127 $h{capath} ||= $self->{ldapCAPath} if ( $self->{ldapCAPath} );
128 my $start_tls = $ldap->start_tls(%h);
129 if ( $start_tls->code ) {
130 $self->logError($start_tls);
131 return;
132 }
133 }
134
135 # Bind with credentials
136 my $bind =
137 $ldap->bind( $self->{ldapBindDN}, password => $self->{ldapBindPassword} );
138 if ( $bind->code ) {
139 $self->logError($bind);
140 return;
141 }
142
143 return $ldap;
144 }
145
146 sub lock {
147
148 # No lock for LDAP
149 return 1;
150 }
151
152 sub isLocked {
153
154 # No lock for LDAP
155 return 0;
156 }
157
158 sub unlock {
159
160 # No lock for LDAP
161 return 1;
162 }
163
164 sub store {
165 my ( $self, $fields ) = @_;
166
167 my $ldap = $self->getLdapConnection;
168 unless ($ldap) {
169 return 0;
170 }
171
172 $fields = $self->serialize($fields);
173
174 my $lastCfg = $self->lastCfg;
175
176 my $confName = "lmConf-" . $fields->{cfgNum};
177 my $confDN =
178 $self->{ldapAttributeId} . "=$confName," . $self->{ldapConfBase};
179
180 # Store values as {key}value
181 my @confValues;
182 while ( my ( $k, $v ) = each(%$fields) ) {
183 push @confValues, "{$k}$v";
184 }
185
186 my $operation;
187
188 if ( $lastCfg == $fields->{cfgNum} ) {
189 $operation =
190 $ldap->modify( $confDN,
191 replace => { $self->{ldapAttributeContent} => \@confValues } );
192 }
193 else {
194 $operation = $ldap->add(
195 $confDN,
196 attrs => [
197 objectClass => [ 'top', $self->{ldapObjectClass} ],
198 $self->{ldapAttributeId} => $confName,
199 $self->{ldapAttributeContent} => \@confValues,
200 ]
201 );
202 }
203 $ldap->unbind;
204
205 if ( $operation->code ) {
206 $self->logError($operation);
207 return 0;
208 }
209
210 return $fields->{cfgNum};
211 }
212
213 sub load {
214 my ( $self, $cfgNum, $fields ) = @_;
215
216 my $ldap = $self->getLdapConnection;
217 unless ($ldap) {
218 return;
219 }
220
221 my $f;
222 my $confName = "lmConf-" . $cfgNum;
223 my $confDN =
224 $self->{ldapAttributeId} . "=$confName," . $self->{ldapConfBase};
225
226 my $search = $ldap->search(
227 base => $confDN,
228 filter => '(objectClass=' . $self->{ldapObjectClass} . ')',
229 scope => 'base',
230 attrs => [ $self->{ldapAttributeContent} ],
231 );
232 $ldap->unbind;
233
234 if ( $search->code ) {
235 $self->logError($search);
236 return;
237 }
238
239 my $entry = $search->shift_entry();
240 my @confValues = $entry->get_value( $self->{ldapAttributeContent} );
241 foreach (@confValues) {
242 my ( $k, $v ) = ( $_ =~ /\{(.*?)\}(.*)/s );
243 if ($fields) {
244 $f->{$k} = $v if ( grep { $_ eq $k } @$fields );
245 }
246 else {
247 $f->{$k} = $v;
248 }
249 }
250 return $self->unserialize($f);
251 }
252
253 sub delete {
254 my ( $self, $cfgNum ) = @_;
255
256 my $ldap = $self->getLdapConnection;
257 unless ($ldap) {
258 return 0;
259 }
260
261 my $confDN =
262 $self->{ldapAttributeId}
263 . "=lmConf-"
264 . $cfgNum . ","
265 . $self->{ldapConfBase};
266 my $delete = $ldap->delete($confDN);
267 $ldap->unbind;
268 $self->logError($delete) if ( $delete->code );
269 }
270
271 sub logError {
272 my $self = shift;
273 my $ldap_operation = shift;
274 $Lemonldap::NG::Common::Conf::msg .=
275 "LDAP error "
276 . $ldap_operation->code . ": "
277 . $ldap_operation->error . " \n";
278 }
279
280 1;
281 __END__