Various fixes for XSS and bad crypto. No claim to completeness.
[mirror/userdir-ldap-cgi.git] / login.cgi
1 #!/usr/bin/perl
2
3 # $Id: login.cgi,v 1.10 2006/12/22 08:58:50 rmurray Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5 # (c) 2006 Ryan Murray. Licensed under the GPL. <rmurray@debian.org>
6
7 use lib '.';
8 use strict;
9 #use Apache::Registry;
10 use CGI;
11 use Util;
12 use URI::Escape;
13 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
14
15 my %config = &Util::ReadConfigFile;
16
17 my $query = new CGI;
18 my $proto = ($ENV{HTTPS} ? "https" : "http");
19
20 if ($proto eq "http" || !($query->param('username')) || !($query->param('password'))) {
21   print "Location: https://$ENV{SERVER_NAME}/$config{webloginhtml}\n\n";
22   exit;
23 }
24
25 my $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
26 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
27
28 my $username = $query->param('username');
29 my $password = $query->param('password');
30 my $binddn = "uid=$username,$config{basedn}";
31
32 my $mesg = $ldap->bind($binddn, password => $password);
33 $mesg->sync;
34
35 if ($mesg->code == LDAP_SUCCESS) {
36   # HACK HACK HACK
37   # Check for md5 password, and update as necessary
38   $mesg = $ldap->search(base   => $config{basedn}, 
39                         filter => "(uid=$username)");
40   $mesg->code && &Util::HTMLError($mesg->error);
41   my $entries = $mesg->as_struct;
42   my $dn = (keys %$entries)[0];
43   my $oldpassword = $entries->{$dn}->{userpassword}->[0];
44   if ($oldpassword !~ /^{crypt}\$1\$/) {
45     # Update their password to md5
46     open (LOG, ">>$config{weblogfile}");
47     print LOG scalar(localtime);
48     print LOG ": Updating MD5 password for $dn\n";
49     close LOG;
50     my $newpassword = '{crypt}'.crypt($password, &Util::CreateCryptSalt(1));
51     &Util::LDAPUpdate($ldap, $dn, 'userPassword', $newpassword);
52   }
53   ## END HACK HACK HACK
54   
55   my $authtoken = &Util::SavePasswordToFile($username, $password);
56
57   if ($query->param('update')) {
58     my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$username;authtoken=$authtoken";
59     print "Location: $url\n\n";
60   } else {
61     my $url = "$proto://$ENV{SERVER_NAME}/$config{websearchurl}?id=$username;authtoken=$authtoken";
62     print "Location: $url\n\n";
63   }
64
65   $ldap->unbind;
66 } else {
67   print "Content-type: text/html; charset=utf-8\n\n";
68   print "<html><body><h1>Not authenticated</h1></body></html>\n";
69 }
70