Auth.pm 13.8 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, 2017 GIP RENATER
11
12
13
# Copyright 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
# <https://github.com/sympa-community/sympa.git>.
salaun's avatar
salaun committed
14
15
16
17
18
19
20
21
22
23
24
25
#
# 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
26
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
salaun's avatar
salaun committed
27

IKEDA Soji's avatar
IKEDA Soji committed
28
package Sympa::WWW::Auth;
salaun's avatar
salaun committed
29

30
31
use strict;
use warnings;
32
use Digest::MD5;
salaun's avatar
salaun committed
33

34
use Sympa;
salaun's avatar
salaun committed
35
use Conf;
36
use Sympa::Database;
37
use Sympa::Log;
38
use Sympa::Robot;
39
use Sympa::Tools::Data;
40
use Sympa::Tools::Text;
41
use Sympa::User;
IKEDA Soji's avatar
IKEDA Soji committed
42
use Sympa::WWW::Report;
salaun's avatar
salaun committed
43

44
45
my $log = Sympa::Log->instance;

46
47
# Moved to: Sympa::User::password_fingerprint().
#sub password_fingerprint;
48

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

    my ($canonic, $user);

58
    if (Sympa::Tools::Text::valid_email($auth)) {
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        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',
            };

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

90
## This subroutine if Sympa may use its native authentication for a given user
91
92
## It might not if no user_table paragraph is found in auth.conf or if the
## regexp or
93
94
95
96
97
98
99
100
## 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
101
102
103
104
105
106
107
108
109
110
111
112
    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;
113
    }
114

115
116
    return $ok;
}
salaun's avatar
salaun committed
117
118

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

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

130
    if (($user->{'wrong_login_count'} || 0) >
131
132
133
134
        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});
Luc Didry's avatar
Luc Didry committed
135
136
        Sympa::WWW::Report::reject_report_web('user', 'too_many_wrong_login',
            {})
137
            unless ($ENV{'SYMPA_SOAP'});
138
        $log->syslog('err',
139
            'Login is blocked: too many wrong password submission for %s',
140
141
            $email);
        return undef;
142
    }
143
144
145
146
    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
147
            if $auth_service->{'negative_regexp'}
148
            and $email =~ /$auth_service->{'negative_regexp'}/i;
149
150
151
152
153

        ## 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') {
154
155
            my $fingerprint =
                Sympa::User::password_fingerprint($pwd, $user->{'password'});
156
157

            if ($fingerprint eq $user->{'password'}) {
158
                Sympa::User::update_password_hash($user, $pwd);
159
160
161
162
163
164
165
166
167
168
169
                Sympa::User::update_global_user($email,
                    {wrong_login_count => 0});
                return {
                    'user'       => $user,
                    'auth'       => 'classic',
                };
            }
        } elsif ($auth_service->{'auth_type'} eq 'ldap') {
            if ($canonic = ldap_authentication(
                    $robot, $auth_service, $email, $pwd, 'email_filter'
                )
Luc Didry's avatar
Luc Didry committed
170
            ) {
171
172
173
174
175
176
177
178
179
180
181
                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',
                };
            }
        }
salaun's avatar
salaun committed
182
    }
salaun's avatar
salaun committed
183

184
    # increment wrong login count.
185
    Sympa::User::update_global_user($email,
186
        {wrong_login_count => ($user->{'wrong_login_count'} || 0) + 1});
187

IKEDA Soji's avatar
IKEDA Soji committed
188
    Sympa::WWW::Report::reject_report_web('user', 'incorrect_passwd', {})
189
        unless $ENV{'SYMPA_SOAP'};
190
    $log->syslog('err', 'Incorrect password for user %s', $email);
salaun's avatar
salaun committed
191

192
    my $param;    #FIXME FIXME: not used.
193
    $param->{'init_email'} = $email;
salaun's avatar
salaun committed
194
195
196
197
    return undef;
}

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

203
    unless (Sympa::search_fullpath($robot, 'auth.conf')) {
204
205
206
207
208
        return undef;
    }

    ## No LDAP entry is defined in auth.conf
    if ($#{$Conf::Conf{'auth_services'}{$robot}} < 0) {
209
        $log->syslog('notice', 'Skipping empty auth.conf');
210
211
212
213
214
215
216
217
218
219
        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);

220
221
222
223
224
225
226
    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'};
    }
227
228
229
    $filter =~ s/\[sender\]/$auth/ig;

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

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

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

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

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

    ##  bind with the DN and the pwd

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

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

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

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

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

291
    my $values = $entry->get_value($attr, alloptions => 1);
292
    my @emails =
293
294
295
        map { lc $_ }
        grep {$_} map { @{$values->{$_}} } sort keys %{$values || {}};

296
    $db->disconnect() or $log->syslog('notice', 'Unable to unbind');
297
    $log->syslog('debug3', 'Canonic: %s', $emails[0]);
298
299
300
    ## If the identifier provided was a valid email, return the provided
    ## email.
    ## Otherwise, return the canonical email guessed after the login.
301
    if (Sympa::Tools::Text::valid_email($auth)
302
303
        and not Conf::get_robot_conf($robot, 'ldap_force_canonical_email')) {
        return $auth;
304
    } else {
305
        return $emails[0];
306
307
    }
}
salaun's avatar
salaun committed
308

309
# fetch user email using their cas net_id and the paragrapah number in auth.conf
310
# NOTE: This might be moved to Robot package.
311
sub get_email_by_net_id {
312
313
314

    my $robot      = shift;
    my $auth_id    = shift;
315
    my $attributes = shift;
316

317
    $log->syslog('debug', '(%s, %s)', $auth_id, $attributes->{'uid'});
318
319
320
321
322
323
324
325
326

    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 =
327
            Sympa::Robot::get_netidtoemail_db($robot, $netid_cookie,
328
329
330
            $Conf::Conf{'auth_services'}{$robot}[$auth_id]{'service_id'});

        return $email;
331
    }
332

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

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

337
    unless ($db and $db->connect()) {
338
        $log->syslog('err', 'Unable to connect to the LDAP server "%s"',
339
            $ldap->{'host'});
340
        return undef;
salaun's avatar
salaun committed
341
342
    }

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

346
347
    my $mesg = $db->do_operation(
        'search',
348
        base    => $ldap->{'suffix'},
349
        filter  => $filter,
350
351
352
        scope   => $ldap->{'scope'},
        timeout => $ldap->{'timeout'},
        attrs   => [$ldap->{'email_attribute'}],
353
354
    );

355
    unless ($mesg and $mesg->count()) {
356
        $log->syslog('notice', "No entry in the LDAP Directory Tree of %s",
357
            $ldap->{'host'});
358
        $db->disconnect();
359
360
        return undef;
    }
salaun's avatar
salaun committed
361

362
    $db->disconnect();
363

364
    ## return only the first attribute
365
    my @results = $mesg->entries;
366
    foreach my $result (@results) {
367
        return (lc($result->get_value($ldap->{'email_attribute'})));
368
    }
salaun's avatar
salaun committed
369

370
}
salaun's avatar
salaun committed
371

372
373
# check trusted_application_name et trusted_application_password : return 1 or
# undef;
374
sub remote_app_check_password {
375
    my ($trusted_application_name, $password, $robot, $service) = @_;
376
    $log->syslog('debug', '(%s, %s, %s)', $trusted_application_name, $robot,
377
        $service);
378

379
    my $md5 = Digest::MD5::md5_hex($password);
380

381
    # seach entry for trusted_application in Conf
382
383
    my @trusted_apps;

384
    # select trusted_apps from robot context or sympa context
385
386
387
388
389
390
    @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'}) {
391
                # $log->syslog('debug', 'Authentication succeed for %s',$application->{'name'});
392
                my %proxy_for_vars;
393
                my %set_vars;
394
395
396
397
                foreach my $varname (@{$application->{'proxy_for_variables'}})
                {
                    $proxy_for_vars{$varname} = 1;
                }
398
399
400
401
402
403
404
405
406
                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;
                    }
407
                    $log->syslog(
408
409
410
411
412
413
                        'info',   'Illegal command %s received from %s',
                        $service, $trusted_application_name
                    );
                    return;
                }
                return (\%proxy_for_vars, \%set_vars);
414
            } else {
415
                $log->syslog('info', 'Bad password from %s',
416
                    $trusted_application_name);
417
                return;
418
419
420
            }
        }
    }
421
    # no matching application found
422
    $log->syslog('info', 'Unknown application name %s',
423
        $trusted_application_name);
424
    return;
425
}
426

427
428
# Moved to Sympa::Ticket::create().
#sub create_one_time_ticket;
429

430
431
# Moved to Sympa::Tickect::load().
#sub get_one_time_ticket;
432

salaun's avatar
salaun committed
433
1;