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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
package Fripost::Schema::Auth;
=head1 NAME
Auth.pm - Authentication methods for the Fripost schema.
=cut
=head1 DESCRIPTION
This module allows simple and SASL authentication against the Fripost
LDAP schema. It is also possible for authenticated users to change their
(or another user's) password.
=cut
use 5.010_000;
use strict;
use warnings;
use utf8;
use Net::LDAP;
use Net::LDAP::Extension::SetPassword;
use Authen::SASL;
use Fripost::Schema::Util qw/canonical_dn ldap_explode_dn ldap_error
split_addr email_valid assert softdie/;
=head1 METHODS
=over 4
=item B<SASLauth> (I<username>, I<cfg>, I<OPTIONS>)
Start a LDAP connection, and SASL-authenticate using proxy
authentication for the given (fully qualified) user.
The following keys in the hash reference I<cfg> are considered:
=over 4
=item B<ldap_uri> => I<host>
The host (or LDAP URI) to connect to. Defaults to
I<ldap://127.0.0.1:389/>.
=item B<ldap_SASL_mechanism> => I<mech>
The SASL mechanism to use. This is mandatory.
=item B<ldap_authcID> => I<user>
The authentication ID for SASL binds. Its format and whether or not it
is required depends on the used SASL mechanism.
=item B<ldap_authcPW> => I<password>
The password to use for the SASL authentication. It may or not be
required, depending on the used SASL mechanism.
=item B<ldap_SASL_service_instance> => I<service>
The SASL service instance. Defaults to I<localhost>.
=item B<ldap_suffix> => I<[RDN1,RDN2,...]>
An array of the Relative Distinguished Name determining where to store
the virtual entries.
=back
Errors can be caught with options B<-die> and B<-error>; See
B<Fripost::Schema::Util> for details.
=cut
sub SASLauth {
my $class = shift;
my $user = shift;
my $cfg = shift;
my %options = @_;
return unless defined $cfg->{ldap_SASL_mechanism};
my $self = bless {}, $class;
$self->suffix( ldap_explode_dn(@{$cfg->{ldap_suffix}}) );
$self->whoami( $self->mail2dn($user) );
$self->ldap( Net::LDAP::->new( $cfg->{ldap_uri}
// 'ldap://127.0.0.1:389/' ) );
assert( $self->ldap, -die => "Couldn't connect to the LDAP server." );
my $callback;
if ($cfg->{ldap_SASL_mechanism} eq 'DIGEST-MD5') {
my ($id,$pw) = ($cfg->{ldap_authcID}, $cfg->{ldap_authcPW});
$callback = { user => $id
, pass => $pw
, authname => 'dn:'.$self->whoami
};
}
elsif ($cfg->{ldap_SASL_mechanism} eq 'GSSAPI') {
$callback = { user => 'dn:'.$self->whoami };
}
else {
softdie( "Unknown SASL mechanism: ".$cfg->{ldap_SASL_mechanism},
%options );
}
my $sasl = Authen::SASL::->new( mechanism => $cfg->{ldap_SASL_mechanism}
, callback => $callback );
my $host = $cfg->{ldap_SASL_service_instance} // 'localhost';
my $conn = $sasl->client_new( 'ldap', $host );
ldap_error ($conn, %options) // return;
my $mesg = $self->ldap->bind( undef, sasl => $conn );
ldap_error ($mesg, %options) // return;
# These are private options, we don't want to pass them around.
delete $cfg->{$_} for qw/ldap_authcID ldap_authcPW/;
$self->{_cfg} = $cfg;
return $self;
}
=item B<auth> (I<username>, I<password>, I<cfg>, I<OPTIONS>)
Start a LDAP connection, and simple authenticate the given I<username>.
An option B<ldap_bind_dn> may be given to override I<username>, which
may then be left undefined.
The following keys in the hash reference I<cfg> are considered:
=over 4
=item B<ldap_uri> => I<host>
The host (or LDAP URI) to connect to. Defaults to
I<ldap://127.0.0.1:389/>.
=item B<ldap_bind_dn> => I<[RDN1,RDN2,...]>
Bind using the Distinguished Name formed by the concatenation of these
RDNs instead.
=item B<ldap_suffix> => I<[RDN1,RDN2,...]>
An array of the Relative Distinguished Name determining where to store
the virtual entries.
=back
Errors can be caught with options B<-die> and B<-error>; See
B<Fripost::Schema::Util> for details.
=cut
sub auth {
my $class = shift;
my $user = shift;
my $pw = shift // return;
my $cfg = shift;
my %options = @_;
my $self = bless {}, $class;
$self->suffix( ldap_explode_dn(@{$cfg->{ldap_suffix}}) );
if (defined $cfg->{ldap_bind_dn}) {
$self->whoami( join ',', @{$cfg->{ldap_bind_dn}} );
}
else {
return unless email_valid($user, -nodie => 1, -exact => 1);
$self->whoami( $self->mail2dn($user) );
}
$self->ldap( Net::LDAP::->new( $cfg->{ldap_uri}
// 'ldap://127.0.0.1:389/' ) );
assert( $self->ldap, -die => "Couldn't connect to the LDAP server." );
my $mesg = $self->ldap->bind( $self->whoami, password => $pw );
ldap_error ($mesg, %options) // return;
$self->{_cfg} = $cfg;
return $self;
}
=item B<passwd> (I<username>, I<oldpassword>, I<newpassword>, I<OPTIONS>)
Change the password of the given I<username> (the current user is chosen
when no I<username> is passed). The current user, whose current password
is I<oldpassword>, must have write access on the C<< userPassword >>
attribute of the DN associated with I<username>.
If I<newpassword> is left undefined, the new password is generated at
random, and returned upon success.
Errors can be caught with options B<-die> and B<-error>, see
B<Fripost::Schema::Util> for details.
=cut
sub passwd {
my $self = shift;
my $user = $self->mail2dn(shift) // $self->whoami;
my $oldpw = shift;
my $newpw = shift;
my %options = @_;
assert ($oldpw, %options); # Must give a password
my %args = (user => $user, password => $oldpw);
$args{newpasswd} = $newpw if defined $newpw;
my $mesg = $self->ldap->set_password( %args );
ldap_error ($mesg, %options) // return;
$self->ldap->gen_password unless defined $newpw;
}
=item B<whoami> ([I<DN>])
Set or get the identity of the user that is currently associated with
the LDAP session. I<DN> is given in string canonical form, defined in
B<Net::LDAP::Util>.
=cut
sub whoami { shift->_set_or_get('_whoami',@_); }
=item B<ldap> ([I<session>])
Set or get the current LDAP I<session>, a B<Net::LDAP> object.
=cut
sub ldap { shift->_set_or_get('_ldap',@_); }
=item B<suffix> ([I<DN>])
Set or get the current Distinguished Name determining where to store the
virtual entries. I<DN> must be given in exploded canonical form (array
of hashes), defined in B<Net::LDAP::Util>.
=cut
sub suffix { shift->_set_or_get('_suffix',@_); }
=item B<mail2dn> ({I<username>|I<domainname>})
Create the Distinguished Name associated with the I<username> (may be an
alias or a list name regardless) or I<domainname>. The argument is first
converted to ASCII.
=cut
sub mail2dn {
my $self = shift;
my $mail = shift // return;
$mail =~ s/^\@//;
my ($l,$d) = split_addr($mail, -encode => 'ascii') or return;
my @dn = ({fvd => $d}, @{$self->suffix});
unshift @dn, {fvl => $l} if $l;
canonical_dn( @dn );
}
=item B<cfg> ([key1, [key2, ...]])
Give the value of configuration options. Without arguments, returns the
whole configuration (as a hash in array context, as a hash reference
otherwise). Keys can be specify to fetch one value (or more) of the
configuration; In that case, the list of the corresponding values are
returned.
=cut
sub cfg {
my $self = shift;
return @{$self->{_cfg}}{@_} if @_;
return unless defined wantarray;
return wantarray ? %{$self->{_cfg}} : $self->{_cfg};
}
# Set or get a key (the first argument), depending on whether or not a second
# argument is given. The value after optional assignation is returned.
sub _set_or_get {
my $self = shift;
my ($k,$v) = @_;
$self->{$k} = $v if defined $v;
return $self->{$k};
}
=item B<done>
Unbind from the LDAP server and close the connection.
=cut
sub done {
my $self = shift;
$self->ldap->unbind if defined $self and defined $self->ldap;
}
=back
=head1 AUTHOR
Guilhem Moulin C<< <guilhem at fripost.org> >>
=head1 COPYRIGHT
Copyright 2012,2013 Guilhem Moulin.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as perl itself.
=cut
1;
__END__
|