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 # Copyright (c) 2008, 2011, 2015 Peter Palfrader
10 #use Apache::Registry;
14 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
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 $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
27 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
29 my $username = $query->param('username');
30 my $password = $query->param('password');
31 my $binddn = "uid=$username,$config{basedn}";
33 my $mesg = $ldap->bind($binddn, password => $password);
36 if ($mesg->code == LDAP_SUCCESS) {
38 # Check for md5 password, and update as necessary
39 $mesg = $ldap->search(base => $config{basedn},
40 filter => "(uid=$username)");
41 $mesg->code && &Util::HTMLError($mesg->error);
42 my $entries = $mesg->as_struct;
43 my $dn = (keys %$entries)[0];
44 my $oldpassword = $entries->{$dn}->{userpassword}->[0];
45 if ($oldpassword !~ /^{crypt}\$1\$/) {
46 # Update their password to md5
47 open (LOG, ">>$config{weblogfile}");
48 print LOG scalar(localtime);
49 print LOG ": Updating MD5 password for $dn\n";
51 my $newpassword = '{crypt}'.crypt($password, &Util::CreateCryptSalt(1));
52 &Util::LDAPUpdate($ldap, $dn, 'userPassword', $newpassword);
56 my $authtoken = &Util::SavePasswordToFile($username, $password);
58 if ($query->param('update')) {
59 my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$username;authtoken=$authtoken";
60 print "Location: $url\n\n";
62 my $url = "$proto://$ENV{SERVER_NAME}/$config{websearchurl}?id=$username;authtoken=$authtoken";
63 print "Location: $url\n\n";
68 print "Content-type: text/html; charset=utf-8\n\n";
69 print "<html><body><h1>Not authenticated</h1></body></html>\n";