Added a hack to update passwords to md5 when a user logs in.
[mirror/userdir-ldap-cgi.git] / login.cgi
1 #!/usr/bin/perl
2
3 # $Id: login.cgi,v 1.6 2000/05/06 06:10:05 tausq Exp $
4 # (c) 1999 Randolph Chung. Licensed under the GPL. <tausq@debian.org>
5
6 use lib '.';
7 use strict;
8 #use Apache::Registry;
9 use CGI;
10 use Util;
11 use URI::Escape;
12 use Crypt::Blowfish;
13 use Net::LDAP qw(:all);
14
15 my %config = &Util::ReadConfigFile;
16
17 my $query = new CGI;
18 my $proto = ($ENV{HTTPS} ? "https" : "http");
19
20 if (!($query->param('username')) || !($query->param('password'))) {
21   print "Location: $proto://$ENV{SERVER_NAME}/$config{webloginurl}\n\n";
22   exit;
23 }
24
25 my $key = &Util::CreateKey($config{blowfishkeylen}); # human-readable version of the key
26 my $hrkey = unpack("H".($config{blowfishkeylen}*2), $key);
27 my $cipher = new Crypt::Blowfish $key;
28
29 my $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
30
31 my $username = $query->param('username');
32 my $password = $query->param('password');
33 my $binddn = "uid=$username,$config{basedn}";
34
35 my $mesg = $ldap->bind($binddn, password => $password);
36 $mesg->sync;
37
38 if ($mesg->code == LDAP_SUCCESS) {
39   # HACK HACK HACK
40   # Check for md5 password, and update as necessary
41   $mesg = $ldap->search(base   => $config{basedn}, 
42                         filter => "(uid=$username)");
43   $mesg->code && &Util::HTMLError($mesg->error);
44   my $entries = $mesg->as_struct;
45   my $dn = (keys %$entries)[0];
46   my $oldpassword = $entries->{$dn}->{userpassword}->[0];
47   if ($oldpassword !~ /^{crypt}\$1\$/) {
48     # Update their password to md5
49     open (LOG, ">$config{weblogfile}");
50     print LOG scalar(localtime);
51     print LOG ": Updating MD5 password for $dn\n";
52     close LOG;
53     my $newpassword = '{crypt}'.crypt($password, &Util::CreateCryptSalt(1));
54     &Util::LDAPUpdate($ldap, $dn, 'userPassword', $newpassword);
55   }
56   ## END HACK HACK HACK
57   
58   my $cryptid = &Util::SavePasswordToFile($username, $password, $cipher);
59
60   if ($query->param('update')) {
61     my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$username&authtoken=$cryptid,$hrkey&editdn=";
62     $url .= uri_escape("uid=$username,$config{basedn}", "\x00-\x40\x7f-\xff");
63     print "Location: $url\n\n";
64   } else {
65     my $url = "$proto://$ENV{SERVER_NAME}/$config{websearchurl}?id=$username&authtoken=$cryptid,$hrkey";
66     print "Location: $url\n\n";
67   }
68
69   $ldap->unbind;
70 } else {
71   print "Content-type: text/html\n\n";
72   print "<html><body><h1>Not authenticated</h1></body></html>\n";
73 }
74