Auth.pm 15.8 KB
Newer Older
salaun's avatar
salaun committed
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
# wwslib.pm - This module includes functions used by wwsympa.fcgi
# 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;
use lib '--LIBDIR--';

use Exporter;
@ISA = ('Exporter');
@EXPORT = ();

use Log;
use Conf;
use List;
33
use report;
34
use Digest::MD5;
salaun's avatar
salaun committed
35
36
37
38
39
40

# use Net::SSLeay qw(&get_https);
# use Net::SSLeay;



41
## authentication : via email or uid
salaun's avatar
salaun committed
42
 sub check_auth{
43
     my $robot = shift;
salaun's avatar
salaun committed
44
45
     my $auth = shift; ## User email or UID
     my $pwd = shift; ## Password
salaun's avatar
salaun committed
46
     &do_log('debug', 'Auth::check_auth(%s)', $auth);
salaun's avatar
salaun committed
47
48
49
50

     my ($canonic, $user);

     if( &tools::valid_email($auth)) {
51
	 return &authentication($robot, $auth,$pwd);
salaun's avatar
salaun committed
52
53
54

     }else{
	 ## This is an UID
55
	 if ($canonic = &ldap_authentication($robot, $auth,$pwd,'uid_filter')){
salaun's avatar
salaun committed
56
57
58
59

	     unless($user = &List::get_user_db($canonic)){
		 $user = {'email' => $canonic};
	     }
60
61
62
63
	     return {'user' => $user,
		     'auth' => 'ldap',
		     'alt_emails' => {$canonic => 'ldap'}
		 };
64
	     
salaun's avatar
salaun committed
65
	 }else{
66
	     &report::reject_report_web('user','incorrect_passwd',{}) unless ($ENV{'SYMPA_SOAP'});
salaun's avatar
salaun committed
67
	     &do_log('err', "Incorrect Ldap password");
salaun's avatar
salaun committed
68
69
70
71
72
73
74
	     return undef;
	 }
     }
 }


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

salaun's avatar
salaun committed
79
80
81
82
83
84
85
86
87
88
89
90

    unless ($user = &List::get_user_db($email)) {
	$user = {'email' => $email,
		 'password' => &tools::tmp_passwd($email)
		 };
    }    
    unless ($user->{'password'}) {
	$user->{'password'} = &tools::tmp_passwd($email);
    }
    


91
    foreach my $auth_service (@{$Conf{'auth_services'}{$robot}}){
salaun's avatar
salaun committed
92
93
94
	next if ($email !~ /$auth_service->{'regexp'}/i);
	next if (($email =~ /$auth_service->{'negative_regexp'}/i)&&($auth_service->{'negative_regexp'}));
	if ($auth_service->{'auth_type'} eq 'user_table') {
95
96
	    
	    if(((&Conf::get_robot_conf('*','password_case') eq 'insensitive') && (lc($pwd) eq lc($user->{'password'}))) || 
salaun's avatar
salaun committed
97
	       ($pwd eq $user->{'password'})) {
98
99
100
101
		return {'user' => $user,
			'auth' => 'classic',
			'alt_emails' => {$email => 'classic'}
			};
salaun's avatar
salaun committed
102
103
	    }
	}elsif($auth_service->{'auth_type'} eq 'ldap') {
104
	    if ($canonic = &ldap_authentication($robot, $email,$pwd,'email_filter')){
salaun's avatar
salaun committed
105
106
107
		unless($user = &List::get_user_db($canonic)){
		    $user = {'email' => $canonic};
		}
108
109
110
111
		return {'user' => $user,
			'auth' => 'ldap',
			'alt_emails' => {$email => 'ldap'}
			};
salaun's avatar
salaun committed
112
113
114
	    }
	}
    }
salaun's avatar
salaun committed
115
116
117
118

    ## If web context and password has never been changed
    ## Then prompt user
    unless ($ENV{'SYMPA_SOAP'}) {
119
	foreach my $auth_service (@{$Conf{'auth_services'}{$robot}}){
salaun's avatar
salaun committed
120
121
122
123
	    next unless ($email !~ /$auth_service->{'regexp'}/i);
	    next unless (($email =~ /$auth_service->{'negative_regexp'}/i)&&($auth_service->{'negative_regexp'}));
	    if ($auth_service->{'auth_type'} eq 'user_table') {
		if ($user->{'password'} =~ /^init/i) {
124
		    &report::reject_report_web('user','init_passwd',{});
salaun's avatar
salaun committed
125
126
		    last;
		}
salaun's avatar
salaun committed
127
128
129
	    }
	}
    }
salaun's avatar
salaun committed
130
    
131
    &report::reject_report_web('user','incorrect_passwd',{}) unless ($ENV{'SYMPA_SOAP'});
salaun's avatar
salaun committed
132
    &do_log('err','authentication: incorrect password for user %s', $email);
salaun's avatar
salaun committed
133
134
135
136
137
138
139
140

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


sub ldap_authentication {
141
     my ($robot, $auth,$pwd,$whichfilter) = @_;
salaun's avatar
salaun committed
142
     my ($cnx, $mesg, $host,$ldap_passwd,$ldap_anonymous);
143
     &do_log('debug2','Auth::ldap_authentication(%s,%s,%s)', $auth,$pwd,$whichfilter);
salaun's avatar
salaun committed
144

145
     unless (&tools::get_filename('etc',{},'auth.conf', $robot)) {
salaun's avatar
salaun committed
146
147
148
149
	 return undef;
     }

     ## No LDAP entry is defined in auth.conf
150
     if ($#{$Conf{'auth_services'}{$robot}} < 0) {
salaun's avatar
salaun committed
151
152
153
154
	 &do_log('notice', 'Skipping empty auth.conf');
	 return undef;
     }

155
     unless (eval "require Net::LDAP") {
salaun's avatar
salaun committed
156
157
158
	 do_log ('err',"Unable to use LDAP library, Net::LDAP required, install perl-ldap (CPAN) first");
	 return undef;
     }
159
160
     require Net::LDAP;

161
     unless (eval "require Net::LDAP::Entry") {
salaun's avatar
salaun committed
162
163
164
	 do_log ('err',"Unable to use LDAP library,Net::LDAP::Entry required install perl-ldap (CPAN) first");
	 return undef;
     }
165
     require Net::LDAP::Entry;
salaun's avatar
salaun committed
166

167
     unless (eval "require Net::LDAP::Message") {
salaun's avatar
salaun committed
168
169
170
	 do_log ('err',"Unable to use LDAP library,Net::LDAP::Entry required install perl-ldap (CPAN) first");
	 return undef;
     }
171
     require Net::LDAP::Message;
salaun's avatar
salaun committed
172
     
173
     foreach my $ldap (@{$Conf{'auth_services'}{$robot}}){
salaun's avatar
salaun committed
174
175
176
	 # only ldap service are to be applied here
	 next unless ($ldap->{'auth_type'} eq 'ldap');

177
178
179
	 # skip ldap auth service if the an email address was provided
	 # and this email address does not match the corresponding regexp 
	 next if ($auth =~ /@/ && $auth !~ /$ldap->{'regexp'}/i);
salaun's avatar
salaun committed
180
181
182
183
184
185
186
187
188
189
190
191
  
	 foreach $host (split(/,/,$ldap->{'host'})){

	     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;

	     ##anonymous bind in order to have the user's DN
	     my $ldap_anonymous;
	     if ($ldap->{'use_ssl'}) {
192
		 unless (eval "require Net::LDAPS") {
salaun's avatar
salaun committed
193
194
195
		     do_log ('err',"Unable to use LDAPS library, Net::LDAPS required");
		     return undef;
		 } 
196
		 require Net::LDAPS;
salaun's avatar
salaun committed
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

		 my %param;
		 $param{'timeout'} = $ldap->{'timeout'} if ($ldap->{'timeout'});
		 $param{'sslversion'} = $ldap->{'ssl_version'} if ($ldap->{'ssl_version'});
		 $param{'ciphers'} = $ldap->{'ssl_ciphers'} if ($ldap->{'ssl_ciphers'});

		 $ldap_anonymous = Net::LDAPS->new($host,%param);
	     }else {
		 $ldap_anonymous = Net::LDAP->new($host,timeout => $ldap->{'timeout'});
	     }

	     unless ($ldap_anonymous ){
		 do_log ('err','Unable to connect to the LDAP server %s',$host);
		 next;
	     }

	     my $cnx;
	     ## Not always anonymous...
	     if (defined ($ldap->{'bind_dn'}) && defined ($ldap->{'bind_password'})) {
		 $cnx = $ldap_anonymous->bind($ldap->{'bind_dn'}, password =>$ldap->{'bind_password'});
	     }else {
		 $cnx = $ldap_anonymous->bind;
	     }

	     unless(defined($cnx) && ($cnx->code() == 0)){
		 do_log('notice',"Can\'t bind to LDAP server $host");
		 last;
		 #do_log ('err','Ldap Error : %s, Ldap server error : %s',$cnx->error,$cnx->server_error);
		 #$ldap_anonymous->unbind;
	     }

	     $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',$host,$auth);
		 $ldap_anonymous->unbind;
		 last;
	     }

	     my $refhash=$mesg->as_struct();
	     my (@DN) = keys(%$refhash);
	     $ldap_anonymous->unbind;

	     ##  bind with the DN and the pwd
	     my $ldap_passwd;
	     if ($ldap->{'use_ssl'}) {
246
		 unless (eval "require Net::LDAPS") {
salaun's avatar
salaun committed
247
248
249
		     do_log ('err',"Unable to use LDAPS library, Net::LDAPS required");
		     return undef;
		 } 
250
		 require Net::LDAPS;
salaun's avatar
salaun committed
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

		 my %param;
		 $param{'timeout'} = $ldap->{'timeout'} if ($ldap->{'timeout'});
		 $param{'sslversion'} = $ldap->{'ssl_version'} if ($ldap->{'ssl_version'});
		 $param{'ciphers'} = $ldap->{'ssl_ciphers'} if ($ldap->{'ssl_ciphers'});

		 $ldap_passwd = Net::LDAPS->new($host,%param);
	     }else {
		 $ldap_passwd = Net::LDAP->new($host,timeout => $ldap->{'timeout'});
	     }

	     unless ($ldap_passwd) {
		 do_log('err','Unable to (re) connect to the LDAP server %s', $host);
		 do_log ('err','Ldap Error : %s, Ldap server error : %s',$ldap_passwd->error,$ldap_passwd->server_error);
		 next;
	     }

	     $cnx = $ldap_passwd->bind($DN[0], password => $pwd);
	     unless(defined($cnx) && ($cnx->code() == 0)){
		 do_log('notice', 'Incorrect password for user %s ; host: %s',$auth, $host);
		 #do_log ('err','Ldap Error : %s, Ldap server error : %s',$cnx->error,$cnx->server_error);
		 $ldap_passwd->unbind;
		 last;
	     }
	     # this bind is anonymous and may return 
	     # $ldap_passwd->bind($DN[0]);
	     $mesg= $ldap_passwd->search ( base => $ldap->{'suffix'},
					   filter => "$filter",
					   scope => $ldap->{'scope'},
					   timeout => $ldap->{'timeout'}
					   );

	     if ($mesg->count() == 0) {
sympa-authors's avatar
sympa-authors committed
284
		 do_log('notice',"No entry in the Ldap Directory Tree of %s", $host);
salaun's avatar
salaun committed
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
		 $ldap_passwd->unbind;
		 last;
	     }

	     ## 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);
	     @canonic_email = $entry->get_value($attrs,alloptions);
	     foreach my $email (@canonic_email){
		 my $e = lc($email);
		 $param->{'alt_emails'}{$e} = 'ldap' if ($e);
	     }

	     foreach my $attribute_value (@alternative_conf){
		 @alternative = $entry->get_value($attribute_value,alloptions);
		 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};
	     }

	     $ldap_passwd->unbind or do_log('notice', "unable to unbind");
	     do_log('debug3',"canonic: $canonic_email[0]");
	     return lc($canonic_email[0]);
	 }

	 next unless ($ldap_anonymous);
	 next unless ($ldap_passwd);
	 next unless (defined($cnx) && ($cnx->code() == 0));
	 next if($mesg->count() == 0);
	 next if($mesg->code() != 0);
	 next unless ($host);
     }
 }


# fetch user email using his cas net_id and the paragrapah number in auth.conf
335
sub get_email_by_net_id {
salaun's avatar
salaun committed
336
    
337
    my $robot = shift;
salaun's avatar
salaun committed
338
    my $auth_id = shift;
339
    my $attributes = shift;
340
341
342
343
344
345
346
347
348
349
350
351
352
353
    
    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;
    }
 
354
    unless (eval "require Net::LDAP") {
salaun's avatar
salaun committed
355
356
357
	do_log ('err',"Unable to use LDAP library, Net::LDAP required, install perl-ldap (CPAN) first");
	return undef;
    }
358
359
    require Net::LDAP;

360
    unless (eval "require Net::LDAP::Entry") {
salaun's avatar
salaun committed
361
362
363
	do_log ('err',"Unable to use LDAP library,Net::LDAP::Entry required install perl-ldap (CPAN) first");
	return undef;
    }
364
    require Net::LDAP::Entry;
salaun's avatar
salaun committed
365
    
366
    unless (eval "require Net::LDAP::Message") {
salaun's avatar
salaun committed
367
368
369
	do_log ('err',"Unable to use LDAP library,Net::LDAP::Entry required install perl-ldap (CPAN) first");
	return undef;
    }
370
    require Net::LDAP::Message;
salaun's avatar
salaun committed
371

372
    my $ldap = @{$Conf{'auth_services'}{$robot}}[$auth_id];
salaun's avatar
salaun committed
373
374
    my $filter = $ldap->{'ldap_get_email_by_uid_filter'} ;

375
    $filter =~ s/\[([\w-]+)\]/$attributes->{$1}/ig;
salaun's avatar
salaun committed
376
377
378
379
380
381
382
383

    foreach my $host (split(/,/,$ldap->{'ldap_host'})){

#	my @alternative_conf = split(/,/,$ldap->{'alternative_email_attribute'});
		
	my $ldap_anonymous;

	my %param;
384
385
	$param{'timeout'} = $ldap->{'ldap_timeout'} || 3;
	$param{'async'} = 1;
salaun's avatar
salaun committed
386
387
	
	if ($ldap->{'ldap_use_ssl'}) {
388
389
390
	    $param{'sslversion'} = $ldap->{'ldap_ssl_version'} if ($ldap->{'ldap_ssl_version'});
	    $param{'ciphers'} = $ldap->{'ldap_ssl_ciphers'} if ($ldap->{'ldap_ssl_ciphers'});

391
	    unless (eval "require Net::LDAPS") {
salaun's avatar
salaun committed
392
393
394
		do_log ('err',"Unable to use LDAPS library, Net::LDAPS required");
		return undef;
	    } 
395
	    require Net::LDAPS;
salaun's avatar
salaun committed
396
397
398
	    
	    $ldap_anonymous = Net::LDAPS->new($host,%param);
	}else {
399
	    $ldap_anonymous = Net::LDAP->new($host,%param);
salaun's avatar
salaun committed
400
401
402
403
404
405
406
407
408
	}
	
	unless ($ldap_anonymous ){
	    do_log ('err','Unable to connect to the LDAP server %s',$host);
	    next;
	}
	
	my $cnx;
	## Not always anonymous...
409
	if (defined ($ldap->{'ldap_bind_dn'}) && defined ($ldap->{'ldap_bind_password'})) {
salaun's avatar
salaun committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
	    $cnx = $ldap_anonymous->bind($ldap->{'ldap_bind_dn'}, password =>$ldap->{'ldap_bind_password'});
	}else {
	    $cnx = $ldap_anonymous->bind;
	}
	
	unless(defined($cnx) && ($cnx->code() == 0)){
	    do_log('notice',"Can\'t bind to LDAP server $host");
	    last;
	    #do_log ('err','Ldap Error : %s, Ldap server error : %s',$cnx->error,$cnx->server_error);
	    #$ldap_anonymous->unbind;
	}
	do_log ('debug',"Binded to LDAP host $host, search base=$ldap->{'ldap_suffix'},filter=$filter,scope=$ldap->{'ldap_scope'},attrs=$ldap->{'ldap_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
432
	    do_log('notice',"No entry in the Ldap Directory Tree of %s", $host);
salaun's avatar
salaun committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	    $ldap_anonymous->unbind;
	    last;
	}

	my @results = $emails->entries;
	foreach my $result (@results){
	    return (lc($result->get_value($ldap->{'ldap_email_attribute'})));
	}

	
	## return only the first attribute
			
	my $entry = $emails->entry(0);
	my @canonic_email = $entry->get_value($ldap->{'ldap_email_attribute'},alloptions);
	foreach my $email (@canonic_email){
	    return(lc($email));
	}
    }

 }

454
455
456
457
458
459
# 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);
    
460
    my $md5 = &tools::md5_fingerprint($password);
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
    
    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'}};
    }
    # open TMP2, ">>/tmp/yy"; printf TMP2 "xxxxxxxxxxx\@ trusted_apps \n"; &tools::dump_var(\@trusted_apps, 0, \*TMP2);printf TMP2 "--------\n"; close TMP2;
    
    foreach my $application (@trusted_apps){
	
 	if ($application->{'name'} eq $trusted_application_name) {
 	    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;
}
 
  
salaun's avatar
salaun committed
496
497
498
1;