Auth.pm 14.7 KB
Newer Older
1
2
3
4
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

salaun's avatar
salaun committed
5
# Sympa - SYsteme de Multi-Postage Automatique
6
7
8
9
#
# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
10
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 GIP RENATER
salaun's avatar
salaun committed
11
12
13
14
15
16
17
18
19
20
21
22
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
23
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
salaun's avatar
salaun committed
24

25
package Sympa::Auth;
salaun's avatar
salaun committed
26

27
28
use strict;
use warnings;
29
use Digest::MD5;
salaun's avatar
salaun committed
30

31
use Sympa;
salaun's avatar
salaun committed
32
use Conf;
33
use Sympa::Database;
34
use Sympa::Log;
35
36
use Sympa::Report;
use Sympa::Robot;
37
use Sympa::Tools::Data;
38
use Sympa::Tools::Text;
39
use Sympa::User;
salaun's avatar
salaun committed
40

41
42
my $log = Sympa::Log->instance;

43
44
# Moved to: Sympa::User::password_fingerprint().
#sub password_fingerprint;
45

46
## authentication : via email or uid
47
48
49
50
sub check_auth {
    my $robot = shift;
    my $auth  = shift;    ## User email or UID
    my $pwd   = shift;    ## Password
51
    $log->syslog('debug', '(%s)', $auth);
52
53
54

    my ($canonic, $user);

55
    if (Sympa::Tools::Text::valid_email($auth)) {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        return authentication($robot, $auth, $pwd);
    } else {
        ## This is an UID
        foreach my $ldap (@{$Conf::Conf{'auth_services'}{$robot}}) {
            # only ldap service are to be applied here
            next unless ($ldap->{'auth_type'} eq 'ldap');

            $canonic =
                ldap_authentication($robot, $ldap, $auth, $pwd, 'uid_filter');
            last if ($canonic);    ## Stop at first match
        }
        if ($canonic) {

            unless ($user = Sympa::User::get_global_user($canonic)) {
                $user = {'email' => $canonic};
            }
            return {
                'user'       => $user,
                'auth'       => 'ldap',
                'alt_emails' => {$canonic => 'ldap'}
            };

        } else {
79
            Sympa::Report::reject_report_web('user', 'incorrect_passwd', {})
80
                unless ($ENV{'SYMPA_SOAP'});
81
            $log->syslog('err', "Incorrect LDAP password");
82
83
84
85
            return undef;
        }
    }
}
salaun's avatar
salaun committed
86

87
## This subroutine if Sympa may use its native authentication for a given user
88
89
## It might not if no user_table paragraph is found in auth.conf or if the
## regexp or
90
91
92
93
94
95
96
97
## negative_regexp exclude this user
## IN : robot, user email
## OUT : boolean
sub may_use_sympa_native_auth {
    my ($robot, $user_email) = @_;

    my $ok = 0;
    ## check each auth.conf paragrpah
98
99
100
101
102
103
104
105
106
107
108
109
    foreach my $auth_service (@{$Conf::Conf{'auth_services'}{$robot}}) {
        next unless ($auth_service->{'auth_type'} eq 'user_table');

        next
            if ($auth_service->{'regexp'}
            && ($user_email !~ /$auth_service->{'regexp'}/i));
        next
            if ($auth_service->{'negative_regexp'}
            && ($user_email =~ /$auth_service->{'negative_regexp'}/i));

        $ok = 1;
        last;
110
    }
111

112
113
    return $ok;
}
salaun's avatar
salaun committed
114
115

sub authentication {
116
117
    my ($robot, $email, $pwd) = @_;
    my ($user, $canonic);
118
    $log->syslog('debug', '(%s)', $email);
salaun's avatar
salaun committed
119

120
    unless ($user = Sympa::User::get_global_user($email)) {
121
122
        $user = {'email' => $email};
    }
salaun's avatar
salaun committed
123
    unless ($user->{'password'}) {
124
        $user->{'password'} = '';
salaun's avatar
salaun committed
125
    }
126

127
    if (($user->{'wrong_login_count'} || 0) >
128
129
130
131
        Conf::get_robot_conf($robot, 'max_wrong_password')) {
        # too many wrong login attemp
        Sympa::User::update_global_user($email,
            {wrong_login_count => $user->{'wrong_login_count'} + 1});
132
        Sympa::Report::reject_report_web('user', 'too_many_wrong_login', {})
133
            unless ($ENV{'SYMPA_SOAP'});
134
        $log->syslog('err',
135
            'Login is blocked: too many wrong password submission for %s',
136
137
            $email);
        return undef;
138
    }
139
140
141
142
    foreach my $auth_service (@{$Conf::Conf{'auth_services'}{$robot}}) {
        next if ($auth_service->{'auth_type'} eq 'authentication_info_url');
        next if ($email !~ /$auth_service->{'regexp'}/i);
        next
143
            if $auth_service->{'negative_regexp'}
144
            and $email =~ /$auth_service->{'negative_regexp'}/i;
145
146
147
148
149

        ## Only 'user_table' and 'ldap' backends will need that Sympa collects
        ## the user passwords
        ## Other backends are Single Sign-On solutions
        if ($auth_service->{'auth_type'} eq 'user_table') {
150
            my $fingerprint = Sympa::User::password_fingerprint($pwd);
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

            if ($fingerprint eq $user->{'password'}) {
                Sympa::User::update_global_user($email,
                    {wrong_login_count => 0});
                return {
                    'user'       => $user,
                    'auth'       => 'classic',
                    'alt_emails' => {$email => 'classic'}
                };
            }
        } elsif ($auth_service->{'auth_type'} eq 'ldap') {
            if ($canonic = ldap_authentication(
                    $robot, $auth_service, $email, $pwd, 'email_filter'
                )
                ) {
                unless ($user = Sympa::User::get_global_user($canonic)) {
                    $user = {'email' => $canonic};
                }
                Sympa::User::update_global_user($canonic,
                    {wrong_login_count => 0});
                return {
                    'user'       => $user,
                    'auth'       => 'ldap',
                    'alt_emails' => {$email => 'ldap'}
                };
            }
        }
salaun's avatar
salaun committed
178
    }
salaun's avatar
salaun committed
179

180
    # increment wrong login count.
181
    Sympa::User::update_global_user($email,
182
        {wrong_login_count => ($user->{'wrong_login_count'} || 0) + 1});
183

184
    Sympa::Report::reject_report_web('user', 'incorrect_passwd', {})
185
        unless $ENV{'SYMPA_SOAP'};
186
    $log->syslog('err', 'Incorrect password for user %s', $email);
salaun's avatar
salaun committed
187

188
    my $param;    #FIXME FIXME: not used.
189
    $param->{'init_email'} = $email;
salaun's avatar
salaun committed
190
191
192
193
    return undef;
}

sub ldap_authentication {
194
    my ($robot, $ldap, $auth, $pwd, $whichfilter) = @_;
195
    my $mesg;
196
197
    $log->syslog('debug2', '(%s, %s, %s)', $auth, '****', $whichfilter);
    $log->syslog('debug3', 'Password used: %s', $pwd);
198

199
    unless (Sympa::search_fullpath($robot, 'auth.conf')) {
200
201
202
203
204
        return undef;
    }

    ## No LDAP entry is defined in auth.conf
    if ($#{$Conf::Conf{'auth_services'}{$robot}} < 0) {
205
        $log->syslog('notice', 'Skipping empty auth.conf');
206
207
208
209
210
211
212
213
214
215
        return undef;
    }

    # only ldap service are to be applied here
    return undef unless ($ldap->{'auth_type'} eq 'ldap');

    # skip ldap auth service if the an email address was provided
    # and this email address does not match the corresponding regexp
    return undef if ($auth =~ /@/ && $auth !~ /$ldap->{'regexp'}/i);

216
217
218
219
220
221
222
223
224
    my @alt_attrs =
        split /\s*,\s*/, ($ldap->{'alternative_email_attribute'} || '');
    my $attr = $ldap->{'email_attribute'};
    my $filter;
    if ($whichfilter eq 'uid_filter') {
        $filter = $ldap->{'get_dn_by_uid_filter'};
    } elsif ($whichfilter eq 'email_filter') {
        $filter = $ldap->{'get_dn_by_email_filter'};
    }
225
226
227
    $filter =~ s/\[sender\]/$auth/ig;

    ## bind in order to have the user's DN
228
    my $db = Sympa::Database->new('LDAP', %$ldap);
229

230
    unless ($db and $db->connect()) {
231
        $log->syslog('err', 'Unable to connect to the LDAP server "%s"',
232
233
234
235
            $ldap->{'host'});
        return undef;
    }

236
237
    $mesg = $db->do_operation(
        'search',
238
239
240
241
242
243
        base    => $ldap->{'suffix'},
        filter  => "$filter",
        scope   => $ldap->{'scope'},
        timeout => $ldap->{'timeout'}
    );

244
    unless ($mesg and $mesg->count()) {
245
        $log->syslog('notice',
246
247
            'No entry in the LDAP Directory Tree of %s for %s',
            $ldap->{'host'}, $auth);
248
        $db->disconnect();
249
250
251
252
253
        return undef;
    }

    my $refhash = $mesg->as_struct();
    my (@DN) = keys(%$refhash);
254
    $db->disconnect();
255
256
257

    ##  bind with the DN and the pwd

258
    # Then set the bind_dn and password according to the current user
259
260
261
262
263
    $db = Sympa::Database->new(
        'LDAP',
        %$ldap,
        bind_dn       => $DN[0],
        bind_password => $pwd,
264
    );
salaun's avatar
salaun committed
265

266
    unless ($db and $db->connect()) {
267
        $log->syslog('err', 'Unable to connect to the LDAP server "%s"',
268
            $ldap->{'host'});
269
270
271
        return undef;
    }

272
273
    $mesg = $db->do_operation(
        'search',
274
275
276
277
278
279
        base    => $ldap->{'suffix'},
        filter  => "$filter",
        scope   => $ldap->{'scope'},
        timeout => $ldap->{'timeout'}
    );

280
    unless ($mesg and $mesg->count()) {
281
        $log->syslog('notice', "No entry in the LDAP Directory Tree of %s",
282
            $ldap->{'host'});
283
        $db->disconnect();
284
285
286
287
        return undef;
    }

    ## To get the value of the canonic email and the alternative email
288
    my (@emails, @alt_emails);
289

290
    #FIXME FIXME: After all, $param->{'alt_emails'} is never used!
291
    my $param = Sympa::Tools::Data::dup_var($ldap);
292
293
294
295
296
297
298
299
300
301
    ## Keep previous alt emails not from LDAP source
    my $previous = {};
    foreach my $alt (keys %{$param->{'alt_emails'}}) {
        $previous->{$alt} = $param->{'alt_emails'}{$alt}
            if ($param->{'alt_emails'}{$alt} ne 'ldap');
    }
    $param->{'alt_emails'} = {};

    my $entry = $mesg->entry(0);

302
303
304
305
306
307
308
309
310
311
312
313
314
    my $values = $entry->get_value($attr, alloptions => 1);
    @emails =
        map { lc $_ }
        grep {$_} map { @{$values->{$_}} } sort keys %{$values || {}};

    @alt_emails = map {
        my $values = $entry->get_value($_, alloptions => 1);
        map { lc $_ }
            grep {$_} map { @{$values->{$_}} } sort keys %{$values || {}};
    } @alt_attrs;

    foreach my $email (@emails, @alt_emails) {
        $param->{'alt_emails'}{$email} = 'ldap';
315
316
317
318
319
320
321
    }

    ## Restore previous emails
    foreach my $alt (keys %{$previous}) {
        $param->{'alt_emails'}{$alt} = $previous->{$alt};
    }

322
    $db->disconnect() or $log->syslog('notice', 'Unable to unbind');
323
    $log->syslog('debug3', 'Canonic: %s', $emails[0]);
324
325
326
    ## If the identifier provided was a valid email, return the provided
    ## email.
    ## Otherwise, return the canonical email guessed after the login.
327
    if (Sympa::Tools::Text::valid_email($auth)
328
329
        and not Conf::get_robot_conf($robot, 'ldap_force_canonical_email')) {
        return $auth;
330
    } else {
331
        return $emails[0];
332
333
    }
}
salaun's avatar
salaun committed
334
335

# fetch user email using his cas net_id and the paragrapah number in auth.conf
336
# NOTE: This might be moved to Robot package.
337
sub get_email_by_net_id {
338
339
340

    my $robot      = shift;
    my $auth_id    = shift;
341
    my $attributes = shift;
342

343
    $log->syslog('debug', '(%s, %s)', $auth_id, $attributes->{'uid'});
344
345
346
347
348
349
350
351
352

    if (defined $Conf::Conf{'auth_services'}{$robot}[$auth_id]
        {'internal_email_by_netid'}) {
        my $sso_config   = @{$Conf::Conf{'auth_services'}{$robot}}[$auth_id];
        my $netid_cookie = $sso_config->{'netid_http_header'};

        $netid_cookie =~ s/(\w+)/$attributes->{$1}/ig;

        my $email =
353
            Sympa::Robot::get_netidtoemail_db($robot, $netid_cookie,
354
355
356
            $Conf::Conf{'auth_services'}{$robot}[$auth_id]{'service_id'});

        return $email;
357
    }
358

359
    my $ldap = $Conf::Conf{'auth_services'}{$robot}->[$auth_id];
360

361
    my $db = Sympa::Database->new('LDAP', %$ldap);
362

363
    unless ($db and $db->connect()) {
364
        $log->syslog('err', 'Unable to connect to the LDAP server "%s"',
365
            $ldap->{'host'});
366
        return undef;
salaun's avatar
salaun committed
367
368
    }

369
    my $filter = $ldap->{'get_email_by_uid_filter'};
370
    $filter =~ s/\[([\w-]+)\]/$attributes->{$1}/ig;
salaun's avatar
salaun committed
371

372
373
    # my @alt_attrs =
    #     split /\s*,\s*/, $ldap->{'alternative_email_attribute'} || '';
374

375
376
    my $mesg = $db->do_operation(
        'search',
377
        base    => $ldap->{'suffix'},
378
        filter  => $filter,
379
380
381
        scope   => $ldap->{'scope'},
        timeout => $ldap->{'timeout'},
        attrs   => [$ldap->{'email_attribute'}],
382
383
    );

384
    unless ($mesg and $mesg->count()) {
385
        $log->syslog('notice', "No entry in the LDAP Directory Tree of %s",
386
            $ldap->{'host'});
387
        $db->disconnect();
388
389
        return undef;
    }
salaun's avatar
salaun committed
390

391
    $db->disconnect();
392

393
    ## return only the first attribute
394
    my @results = $mesg->entries;
395
    foreach my $result (@results) {
396
        return (lc($result->get_value($ldap->{'email_attribute'})));
397
    }
salaun's avatar
salaun committed
398

399
}
salaun's avatar
salaun committed
400

401
402
# check trusted_application_name et trusted_application_password : return 1 or
# undef;
403
sub remote_app_check_password {
404
    my ($trusted_application_name, $password, $robot, $service) = @_;
405
    $log->syslog('debug', '(%s, %s, %s)', $trusted_application_name, $robot,
406
        $service);
407

408
    my $md5 = Digest::MD5::md5_hex($password);
409

410
    # seach entry for trusted_application in Conf
411
412
    my @trusted_apps;

413
    # select trusted_apps from robot context or sympa context
414
415
416
417
418
419
    @trusted_apps = @{Conf::get_robot_conf($robot, 'trusted_applications')};

    foreach my $application (@trusted_apps) {

        if (lc($application->{'name'}) eq lc($trusted_application_name)) {
            if ($md5 eq $application->{'md5password'}) {
420
                # $log->syslog('debug', 'Authentication succeed for %s',$application->{'name'});
421
                my %proxy_for_vars;
422
                my %set_vars;
423
424
425
426
                foreach my $varname (@{$application->{'proxy_for_variables'}})
                {
                    $proxy_for_vars{$varname} = 1;
                }
427
428
429
430
431
432
433
434
435
                foreach my $varname (@{$application->{'set_variables'}}) {
                    $set_vars{$1} = $2 if $varname =~ /(\S+)=(.*)/;
                }
                if ($application->{'allow_commands'}) {
                    foreach my $cmdname (@{$application->{'allow_commands'}})
                    {
                        return (\%proxy_for_vars, \%set_vars)
                            if $cmdname eq $service;
                    }
436
                    $log->syslog(
437
438
439
440
441
442
                        'info',   'Illegal command %s received from %s',
                        $service, $trusted_application_name
                    );
                    return;
                }
                return (\%proxy_for_vars, \%set_vars);
443
            } else {
444
                $log->syslog('info', 'Bad password from %s',
445
                    $trusted_application_name);
446
                return;
447
448
449
            }
        }
    }
450
    # no matching application found
451
    $log->syslog('info', 'Unknown application name %s',
452
        $trusted_application_name);
453
    return;
454
}
455

456
457
# Moved to Sympa::Ticket::create().
#sub create_one_time_ticket;
458

459
460
# Moved to Sympa::Tickect::load().
#sub get_one_time_ticket;
461

salaun's avatar
salaun committed
462
1;