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>
14 use Net::LDAP qw(:all);
16 my %config = &Util::ReadConfigFile;
19 my $proto = ($ENV{HTTPS} ? "https" : "http");
21 if ($proto eq "http" || !($query->param('username')) || !($query->param('password'))) {
22 print "Location: https://$ENV{SERVER_NAME}/$config{webloginhtml}\n\n";
26 my $key = &Util::CreateKey($config{blowfishkeylen}); # human-readable version of the key
27 my $hrkey = unpack("H".($config{blowfishkeylen}*2), $key);
28 my $cipher = new Crypt::Blowfish $key;
30 my $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
32 my $username = $query->param('username');
33 my $password = $query->param('password');
34 my $binddn = "uid=$username,$config{basedn}";
36 my $mesg = $ldap->bind($binddn, password => $password);
39 if ($mesg->code == LDAP_SUCCESS) {
41 # Check for md5 password, and update as necessary
42 $mesg = $ldap->search(base => $config{basedn},
43 filter => "(uid=$username)");
44 $mesg->code && &Util::HTMLError($mesg->error);
45 my $entries = $mesg->as_struct;
46 my $dn = (keys %$entries)[0];
47 my $oldpassword = $entries->{$dn}->{userpassword}->[0];
48 if ($oldpassword !~ /^{crypt}\$1\$/) {
49 # Update their password to md5
50 open (LOG, ">>$config{weblogfile}");
51 print LOG scalar(localtime);
52 print LOG ": Updating MD5 password for $dn\n";
54 my $newpassword = '{crypt}'.crypt($password, &Util::CreateCryptSalt(1));
55 &Util::LDAPUpdate($ldap, $dn, 'userPassword', $newpassword);
59 my $cryptid = &Util::SavePasswordToFile($username, $password, $cipher);
61 if ($query->param('update')) {
62 my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$username&authtoken=$cryptid,$hrkey&editdn=";
63 $url .= uri_escape("uid=$username,$config{basedn}", "\x00-\x40\x7f-\xff");
64 print "Location: $url\n\n";
66 my $url = "$proto://$ENV{SERVER_NAME}/$config{websearchurl}?id=$username&authtoken=$cryptid,$hrkey";
67 print "Location: $url\n\n";
72 print "Content-type: text/html; charset=utf-8\n\n";
73 print "<html><body><h1>Not authenticated</h1></body></html>\n";