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>
13 use Net::LDAP qw(LDAP_SUCCESS LDAP_PROTOCOL_ERROR);
15 my %config = &Util::ReadConfigFile;
18 my $proto = ($ENV{HTTPS} ? "https" : "http");
20 if ($proto eq "http" || !($query->param('username')) || !($query->param('password'))) {
21 print "Location: https://$ENV{SERVER_NAME}/$config{webloginhtml}\n\n";
25 my $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
26 &Util::UpgradeConnection($ldap) unless $config{usessl} eq 'False';
28 my $username = $query->param('username');
29 my $password = $query->param('password');
30 my $binddn = "uid=$username,$config{basedn}";
32 my $mesg = $ldap->bind($binddn, password => $password);
35 if ($mesg->code == LDAP_SUCCESS) {
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";
50 my $newpassword = '{crypt}'.crypt($password, &Util::CreateCryptSalt(1));
51 &Util::LDAPUpdate($ldap, $dn, 'userPassword', $newpassword);
55 my $authtoken = &Util::SavePasswordToFile($username, $password);
57 if ($query->param('update')) {
58 my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$username;authtoken=$authtoken";
59 print "Location: $url\n\n";
61 my $url = "$proto://$ENV{SERVER_NAME}/$config{websearchurl}?id=$username;authtoken=$authtoken";
62 print "Location: $url\n\n";
67 print "Content-type: text/html; charset=utf-8\n\n";
68 print "<html><body><h1>Not authenticated</h1></body></html>\n";