Auth.pm 16.8 KB
Newer Older
1
# Auth.pm - This module provides web authentication functions
salaun's avatar
salaun committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# RCS Identication ; $Revision$ ; $Date$ 
#
# Sympa - SYsteme de Multi-Postage Automatique
# Copyright (c) 1997, 1998, 1999, 2000, 2001 Comite Reseau des Universites
# Copyright (c) 1997,1998, 1999 Institut Pasteur & Christophe Wolfhugel
#
# 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
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


package Auth;

25
use Digest::MD5;
salaun's avatar
salaun committed
26

27
use Language;
salaun's avatar
salaun committed
28
29
30
use Log;
use Conf;
use List;
31
use report;
salaun's avatar
salaun committed
32

33
34
35
36
37
38
39
40
41
42
43
44
45
## return the password finger print (this proc allow futur replacement of md5 by sha1 or ....)
sub password_fingerprint{

    do_log('debug', 'Auth::password_fingerprint');

    my $pwd = shift;
    if(&Conf::get_robot_conf('*','password_case') eq 'insensitive') {
	return &tools::md5_fingerprint(lc($pwd));
    }else{
	return &tools::md5_fingerprint($pwd);
    }    
}

salaun's avatar
salaun committed
46

47
## authentication : via email or uid
salaun's avatar
salaun committed
48
 sub check_auth{
49
     my $robot = shift;
salaun's avatar
salaun committed
50
51
     my $auth = shift; ## User email or UID
     my $pwd = shift; ## Password
salaun's avatar
salaun committed
52
     &do_log('debug', 'Auth::check_auth(%s)', $auth);
salaun's avatar
salaun committed
53
54
55
56

     my ($canonic, $user);

     if( &tools::valid_email($auth)) {
57
	 return &authentication($robot, $auth,$pwd);
salaun's avatar
salaun committed
58
59
     }else{
	 ## This is an UID
60
61
62
63
64
65
66
67
68
	 foreach my $ldap (@{$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){
	     
69
	     unless($user = &List::get_global_user($canonic)){
salaun's avatar
salaun committed
70
71
		 $user = {'email' => $canonic};
	     }
72
73
74
75
	     return {'user' => $user,
		     'auth' => 'ldap',
		     'alt_emails' => {$canonic => 'ldap'}
		 };
76
	     
salaun's avatar
salaun committed
77
	 }else{
78
	     &report::reject_report_web('user','incorrect_passwd',{}) unless ($ENV{'SYMPA_SOAP'});
salaun's avatar
salaun committed
79
	     &do_log('err', "Incorrect Ldap password");
salaun's avatar
salaun committed
80
81
82
83
84
	     return undef;
	 }
     }
 }

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## This subroutine if Sympa may use its native authentication for a given user
## It might not if no user_table paragraph is found in auth.conf or if the regexp or
## 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
    foreach my $auth_service (@{$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;
    }
    
    return $ok;
}
salaun's avatar
salaun committed
106
107

sub authentication {
108
    my ($robot, $email,$pwd) = @_;
salaun's avatar
salaun committed
109
    my ($user,$canonic);
sympa-authors's avatar
Fix    
sympa-authors committed
110
    &do_log('debug', 'Auth::authentication(%s)', $email);
salaun's avatar
salaun committed
111

salaun's avatar
salaun committed
112

113
    unless ($user = &List::get_global_user($email)) {
114
	$user = {'email' => $email };
salaun's avatar
salaun committed
115
116
    }    
    unless ($user->{'password'}) {
117
	$user->{'password'} = '';
salaun's avatar
salaun committed
118
119
    }
    
120
121
    if ($user->{'wrong_login_count'} > &Conf::get_robot_conf($robot, 'max_wrong_password')){
	# too many wrong login attemp
122
	&List::update_global_user($email,{wrong_login_count => $user->{'wrong_login_count'}+1}) ;
123
124
125
126
	&report::reject_report_web('user','too_many_wrong_login',{}) unless ($ENV{'SYMPA_SOAP'});
	&do_log('err','login is blocked : too many wrong password submission for %s', $email);
	return undef;
    }
127
    foreach my $auth_service (@{$Conf{'auth_services'}{$robot}}){
128
	next if ($auth_service->{'auth_type'} eq 'authentication_info_url');
salaun's avatar
salaun committed
129
130
	next if ($email !~ /$auth_service->{'regexp'}/i);
	next if (($email =~ /$auth_service->{'negative_regexp'}/i)&&($auth_service->{'negative_regexp'}));
david.verdin's avatar
david.verdin committed
131
132
133

	## Only 'user_table' and 'ldap' backends will need that Sympa collects the user passwords
	## Other backends are Single Sign-On solutions
salaun's avatar
salaun committed
134
	if ($auth_service->{'auth_type'} eq 'user_table') {
135
	    my $fingerprint = &password_fingerprint ($pwd);	    	    
136
	    
137
	    if ($fingerprint eq $user->{'password'}) {
138
		&List::update_global_user($email,{wrong_login_count => 0}) ;
139
140
141
142
		return {'user' => $user,
			'auth' => 'classic',
			'alt_emails' => {$email => 'classic'}
			};
salaun's avatar
salaun committed
143
144
	    }
	}elsif($auth_service->{'auth_type'} eq 'ldap') {
145
	    if ($canonic = &ldap_authentication($robot, $auth_service, $email,$pwd,'email_filter')){
146
		unless($user = &List::get_global_user($canonic)){
salaun's avatar
salaun committed
147
148
		    $user = {'email' => $canonic};
		}
149
		&List::update_global_user($canonic,{wrong_login_count => 0}) ;
150
151
152
153
		return {'user' => $user,
			'auth' => 'ldap',
			'alt_emails' => {$email => 'ldap'}
			};
salaun's avatar
salaun committed
154
155
156
	    }
	}
    }
salaun's avatar
salaun committed
157

158
    # increment wrong login count.
159
    &List::update_global_user($email,{wrong_login_count =>$user->{'wrong_login_count'}+1}) ;
160

161
    &report::reject_report_web('user','incorrect_passwd',{}) unless ($ENV{'SYMPA_SOAP'});
salaun's avatar
salaun committed
162
    &do_log('err','authentication: incorrect password for user %s', $email);
salaun's avatar
salaun committed
163
164
165
166
167
168
169
170

    $param->{'init_email'} = $email;
    $param->{'escaped_init_email'} = &tools::escape_chars($email);
    return undef;
}


sub ldap_authentication {
171
172
     my ($robot, $ldap, $auth, $pwd, $whichfilter) = @_;
     my ($mesg, $host,$ldap_passwd,$ldap_anonymous);
173
174
     &do_log('debug2','Auth::ldap_authentication(%s,%s,%s)', $auth,'****',$whichfilter);
     &do_log('debug3','Password used: %s',$pwd);
salaun's avatar
salaun committed
175

176
     unless (&tools::get_filename('etc',{},'auth.conf', $robot)) {
salaun's avatar
salaun committed
177
178
179
180
	 return undef;
     }

     ## No LDAP entry is defined in auth.conf
181
     if ($#{$Conf{'auth_services'}{$robot}} < 0) {
salaun's avatar
salaun committed
182
183
184
185
	 &do_log('notice', 'Skipping empty auth.conf');
	 return undef;
     }

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
     # 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);
     
     my @alternative_conf = split(/,/,$ldap->{'alternative_email_attribute'});
     my $attrs = $ldap->{'email_attribute'};
     my $filter = $ldap->{'get_dn_by_uid_filter'} if($whichfilter eq 'uid_filter');
     $filter = $ldap->{'get_dn_by_email_filter'} if($whichfilter eq 'email_filter');
     $filter =~ s/\[sender\]/$auth/ig;
     
     ## bind in order to have the user's DN
     my $param = &tools::dup_var($ldap);
     my $ds = new Datasource('LDAP', $param);
     
     unless (defined $ds && ($ldap_anonymous = $ds->connect())) {
       &do_log('err',"Unable to connect to the LDAP server '%s'", $ldap->{'host'});
       return undef;
salaun's avatar
salaun committed
206
     }
207
208
209
210
211
212
213
214
215
216
217
     
     
     $mesg = $ldap_anonymous->search(base => $ldap->{'suffix'},
				     filter => "$filter",
				     scope => $ldap->{'scope'} ,
				     timeout => $ldap->{'timeout'});
     
     if ($mesg->count() == 0) {
       do_log('notice','No entry in the Ldap Directory Tree of %s for %s',$ldap->{'host'},$auth);
       $ds->disconnect();
       return undef;
salaun's avatar
salaun committed
218
     }
219
220
221
222
223
224
225
226
227
     
     my $refhash=$mesg->as_struct();
     my (@DN) = keys(%$refhash);
     $ds->disconnect();
     
     ##  bind with the DN and the pwd
     
     ## Duplicate structure first
     ## Then set the bind_dn and password according to the current user
228
     $param = &tools::dup_var($ldap);
229
230
231
     $param->{'ldap_bind_dn'} = $DN[0];
     $param->{'ldap_bind_password'} = $pwd;
     
232
     $ds = new Datasource('LDAP', $param);
233
234
235
236
     
     unless (defined $ds && ($ldap_passwd = $ds->connect())) {
       do_log('err',"Unable to connect to the LDAP server '%s'", $param->{'host'});
       return undef;
salaun's avatar
salaun committed
237
238
     }
     
239
240
241
242
243
244
245
246
247
248
     $mesg= $ldap_passwd->search ( base => $ldap->{'suffix'},
				   filter => "$filter",
				   scope => $ldap->{'scope'},
				   timeout => $ldap->{'timeout'}
				 );
     
     if ($mesg->count() == 0 || $mesg->code() != 0) {
       do_log('notice',"No entry in the Ldap Directory Tree of %s", $ldap->{'host'});
       $ds->disconnect();
       return undef;
salaun's avatar
salaun committed
249
     }
250
251
252
253
254
255
256
257
258
259
260
261
     
     ## To get the value of the canonic email and the alternative email
     my (@canonic_email, @alternative);
     
     ## 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);
262
     @canonic_email = $entry->get_value($attrs, 'alloptions' => 1);
263
264
265
266
267
268
     foreach my $email (@canonic_email){
       my $e = lc($email);
       $param->{'alt_emails'}{$e} = 'ldap' if ($e);
     }
     
     foreach my $attribute_value (@alternative_conf){
269
       @alternative = $entry->get_value($attribute_value, 'alloptions' => 1);
270
271
272
273
274
275
276
277
278
279
280
281
282
       foreach my $alter (@alternative){
	 my $a = lc($alter); 
	 $param->{'alt_emails'}{$a} = 'ldap' if($a) ;
       }
     }
     
     ## Restore previous emails
     foreach my $alt (keys %{$previous}) {
       $param->{'alt_emails'}{$alt} = $previous->{$alt};
     }
     
     $ds->disconnect() or &do_log('notice', "unable to unbind");
     &do_log('debug3',"canonic: $canonic_email[0]");
283
284
     ## If the identifier provided was a valid email, return the provided email.
     ## Otherwise, return the canonical email guessed after the login.
285
     if( &tools::valid_email($auth) && !$Conf::Conf{'robots'}{$robot}{'ldap_force_canonical_email'}) {
286
287
288
289
290
	 return ($auth);
     }else{
	 return lc($canonic_email[0]);
     } 
}
salaun's avatar
salaun committed
291
292
293


# fetch user email using his cas net_id and the paragrapah number in auth.conf
294
sub get_email_by_net_id {
salaun's avatar
salaun committed
295
    
296
    my $robot = shift;
salaun's avatar
salaun committed
297
    my $auth_id = shift;
298
    my $attributes = shift;
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    
    do_log ('debug',"Auth::get_email_by_net_id($auth_id,$attributes->{'uid'})");
    
    if (defined $Conf{'auth_services'}{$robot}[$auth_id]{'internal_email_by_netid'}) {
	my $sso_config = @{$Conf{'auth_services'}{$robot}}[$auth_id];
	my $netid_cookie = $sso_config->{'netid_http_header'} ;
	
	$netid_cookie =~ s/(\w+)/$attributes->{$1}/ig;
	
	$email = &List::get_netidtoemail_db($robot, $netid_cookie, $Conf{'auth_services'}{$robot}[$auth_id]{'service_id'});
	
	return $email;
    }
 
313
    my $ldap = @{$Conf{'auth_services'}{$robot}}[$auth_id];
314

315
316
317
    my $param = &tools::dup_var($ldap);
    my $ds = new Datasource('LDAP', $param);
    my $ldap_anonymous;
salaun's avatar
salaun committed
318
    
319
320
    unless (defined $ds && ($ldap_anonymous = $ds->connect())) {
	&do_log('err',"Unable to connect to the LDAP server '%s'", $ldap->{'ldap_host'});
salaun's avatar
salaun committed
321
322
323
324
	return undef;
    }

    my $filter = $ldap->{'ldap_get_email_by_uid_filter'} ;
325
    $filter =~ s/\[([\w-]+)\]/$attributes->{$1}/ig;
salaun's avatar
salaun committed
326
327
328
329
330
331
332
333
334
335
336
337

#	my @alternative_conf = split(/,/,$ldap->{'alternative_email_attribute'});
		
	my $emails= $ldap_anonymous->search ( base => $ldap->{'ldap_suffix'},
				      filter => $filter,
				      scope => $ldap->{'ldap_scope'},
				      timeout => $ldap->{'ldap_timeout'},
				      attrs =>  $ldap->{'ldap_email_attribute'}
				      );
	my $count = $emails->count();

	if ($emails->count() == 0) {
sympa-authors's avatar
sympa-authors committed
338
	    do_log('notice',"No entry in the Ldap Directory Tree of %s", $host);
339
340
	$ds->disconnect();
	return undef;
salaun's avatar
salaun committed
341
342
	}

343
344
345
    $ds->disconnect();
    
    ## return only the first attribute
salaun's avatar
salaun committed
346
347
348
349
350
351
352
	my @results = $emails->entries;
	foreach my $result (@results){
	    return (lc($result->get_value($ldap->{'ldap_email_attribute'})));
	}

 }

353
354
355
356
357
358
# check trusted_application_name et trusted_application_password : return 1 or undef;
sub remote_app_check_password {
    
    my ($trusted_application_name,$password,$robot) = @_;
    do_log('debug','Auth::remote_app_check_password (%s,%s)',$trusted_application_name,$robot);
    
359
    my $md5 = &tools::md5_fingerprint($password);
360
361
362
363
364
365
366
367
368
369
370
371
372
373
    
    my $vars;
    # seach entry for trusted_application in Conf
    my @trusted_apps ;
    
    # select trusted_apps from robot context or symap context
    if ((defined $robot) &&  (defined $Conf::Conf{'robots'}{$robot}{'trusted_applications'})) {
 	@trusted_apps = @{$Conf::Conf{'robots'}{$robot}{'trusted_applications'}{'trusted_application'}};
    }else{
 	@trusted_apps = @{$Conf::Conf{'trusted_applications'}{'trusted_application'}};
    }
    
    foreach my $application (@trusted_apps){
	
374
 	if (lc($application->{'name'}) eq lc($trusted_application_name)) {
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
 	    if ($md5 eq $application->{'md5password'}) {
 		# &do_log('debug', 'Auth::remote_app_check_password : authentication succeed for %s',$application->{'name'});
 		my %proxy_for_vars ;
 		foreach my $varname (@{$application->{'proxy_for_variables'}}) {
 		    $proxy_for_vars{$varname}=1;
 		}		
 		return (\%proxy_for_vars);
 	    }else{
 		&do_log('info', 'Auth::remote_app_check_password: bad password from %s', $trusted_application_name);
 		return undef;
 	    }
 	}
    }				 
    # no matching application found
    &do_log('info', 'Auth::remote_app-check_password: unknown application name %s', $trusted_application_name);
    return undef;
}
 
393
394
395
396
397
398
399
# create new entry in one_time_ticket table using a rand as id so later access is authenticated
#

sub create_one_time_ticket {
    my $email = shift;
    my $robot = shift;
    my $data_string = shift;
400
    my $remote_addr = shift; ## Value may be 'mail' if the IP address is not known
401
402

    my $ticket = &SympaSession::get_random();
403
    do_log('info', 'Auth::create_one_time_ticket(%s,%s,%s,%s) value = %s',$email,$robot,$data_string,$remote_addr,$ticket);
404
405
406
407
408
409
410
411
412
413
414
415
416

    my $date = time;
    my $dbh = &List::db_get_handler();
    my $sth;

    ## Check database connection
    unless ($dbh and $dbh->ping) {
	return undef unless &List::db_connect();
    }
    
    my $statement = sprintf "INSERT INTO one_time_ticket_table (ticket_one_time_ticket, robot_one_time_ticket, email_one_time_ticket, date_one_time_ticket, data_one_time_ticket, remote_addr_one_time_ticket, status_one_time_ticket) VALUES ('%s','%s','%s','%s','%s','%s','%s')",$ticket,$robot,$email,time,$data_string,$remote_addr,'open';

    unless ($dbh->do($statement)) {
417
	do_log('err','Unable to insert in table one_time_ticket_table while executing SQL statement "%s" : %s', $statement, $dbh->errstr);
418
419
420
421
422
423
424
425
426
427
428
	return undef;
    }   
    return $ticket;
}

# read one_time_ticket from table and remove it
#
sub get_one_time_ticket {
    my $ticket_number = shift;
    my $addr = shift; 
    
429
    do_log('debug2', '(%s)',$ticket_number);
430
431
432
433
434
435
436
437
438
    
    my $dbh = &List::db_get_handler();
    my $sth;
    
    ## Check database connection
    unless ($dbh and $dbh->ping) {
	return return {'result'=>'error'} unless &List::db_connect();
    }
    my $statement;
439
    $statement = sprintf "SELECT ticket_one_time_ticket AS ticket, robot_one_time_ticket AS robot, email_one_time_ticket AS email, date_one_time_ticket AS \"date\", data_one_time_ticket AS data, remote_addr_one_time_ticket AS remote_addr, status_one_time_ticket as status FROM one_time_ticket_table WHERE ticket_one_time_ticket = '%s' ", $ticket_number;
440
441
442
443
444
445
446
447
448
449
    
    unless ($sth = $dbh->prepare($statement)) {
	do_log('err','Auth::get_one_time_ticket: Unable to prepare SQL statement : %s', $dbh->errstr);
	return {'result'=>'error'};
    }
    unless ($sth->execute) {
	do_log('err','Auth::get_one_time_ticket: Unable to execute SQL statement "%s" : %s', $statement, $dbh->errstr);
	return {'result'=>'error'};
    }    
 
450
    my $ticket = $sth->fetchrow_hashref('NAME_lc');
451
452
453
454
455
456
457
458
459
460
461
462
    $sth->finish();
    
    unless ($ticket) {	
	do_log('info','Auth::get_one_time_ticket: Unable to find one time ticket %s (SQL query %s)%s', $ticket,$statement, $dbh->errstr);
	return {'result'=>'not_found'};
    }
    
    my $result;
    my $printable_date = gettext_strftime "%d %b %Y at %H:%M:%S", localtime($ticket->{'date'});

    if ($ticket->{'status'} ne 'open') {
	$result = 'closed';
463
	do_log('info','Auth::get_one_time_ticket: ticket %s from %s has been used before (%s)',$ticket_number,$ticket->{'email'},$printable_date);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
    }
    elsif (time - $ticket->{'date'} > 48 * 60 * 60) {
	do_log('info','Auth::get_one_time_ticket: ticket %s from %s refused because expired (%s)',$ticket_number,$ticket->{'email'},$printable_date);
	$result = 'expired';
    }else{
	$result = 'success';
    }
    $statement = sprintf "UPDATE one_time_ticket_table SET status_one_time_ticket = '%s' WHERE (ticket_one_time_ticket='%s')", $addr, $ticket_number;
    
    unless ($dbh->do($statement)) {
    	do_log('err','Auth::get_one_time_ticket  Unable to execute SQL statement "%s" : %s', $statement, $dbh->errstr);
    }

    do_log('info', 'xxxx Auth::get_one_time_ticket(%s) : result : %s',$ticket_number,$result);
    return {'result'=>$result,
	    'date'=>$ticket->{'date'},
	    'email'=>$ticket->{'email'},
	    'remote_addr'=>$ticket->{'remote_addr'},
	    'robot'=>$ticket->{'robot'},
	    'data'=>$ticket->{'data'},
	    'status'=>$ticket->{'status'}
	};
}
    
salaun's avatar
salaun committed
488
1;