Initial import
[mirror/userdir-ldap.git] / web / login.cgi
1 #!/usr/bin/perl
2
3 # (c) 1999 Debian and Randolph Chung. Licensed under the GPL. <tausq@debian.org>
4
5 use lib '.';
6 use strict;
7 #use Apache::Registry;
8 use CGI;
9 use Util;
10 use URI::Escape;
11 use Crypt::Blowfish;
12 use Net::LDAP qw(:all);
13
14 my %config = &Util::ReadConfigFile;
15
16 my $query = new CGI;
17 my $proto = ($ENV{HTTPS} ? "https" : "http");
18
19 if (!($query->param('username')) || !($query->param('password'))) {
20   print "Location: $proto://$ENV{SERVER_NAME}/$config{webloginurl}\n\n";
21   exit;
22 }
23
24 my $key = &Util::CreateKey($config{blowfishkeylen}); # human-readable version of the key
25 my $hrkey = unpack("H".($config{blowfishkeylen}*2), $key);
26 my $cipher = new Crypt::Blowfish $key;
27
28 my $ldap = Net::LDAP->new($config{ldaphost}) || &Util::HTMLError($!);
29
30 my $username = $query->param('username');
31 my $password = $query->param('password');
32 my $binddn = "uid=$username,$config{basedn}";
33
34 my $mesg = $ldap->bind($binddn, password => $password);
35 $mesg->sync;
36
37 if ($mesg->code == LDAP_SUCCESS) {
38   my $cryptid = &Util::SavePasswordToFile($username, $password, $cipher);
39
40   if ($query->param('update')) {
41     my $url = "$proto://$ENV{SERVER_NAME}/$config{webupdateurl}?id=$username&authtoken=$cryptid:$hrkey&editdn=";
42     $url .= uri_escape("uid=$username,$config{basedn}", "\x00-\x40\x7f-\xff");
43     print "Location: $url\n\n";
44   } else {
45     print "Location: $proto://$ENV{SERVER_NAME}/$config{websearchurl}?id=$username&authtoken=$cryptid:$hrkey\n\n";
46   }
47
48   $ldap->unbind;
49 } else {
50   print "Content-type: text/html\n\n";
51   print "<html><body><h1>Not authenticated</h1></body></html>\n";
52 }