<?php
////
//
// Read through the comments in this script for configuration guides
// Disclaimer:
// This software is distributed as-is. If it causes your network to catch fire, it's not my fault.
//
// ASAP v0.0.1 - login.php
//
// 06.01.07 Paul Venezia (pvenezia@pvenezia.net)
//


$_SESSION = Array();
session_register("_SESSION");

if (isset($_SESSION['uname'])) {
	header("Location: index.php");
	exit;
}

//
// WTF. I was continuously able to get successful binds with a blank pw, never with >0 chars
// Crazy.

if (isset($_REQUEST['form_username']) && strlen($_REQUEST['form_password']) > 0) {
	checkAD();
#	$auth = checkAD();
#	if ($auth) {
#		header("Location: index.php");
#	} else {
#        session_unregister("_SESSION");
#        session_destroy();
#	}

} 
	showLogin();
 
function checkAD() { 
   //Connect
   $ad = ldap_connect("172.16.1.103", 3268);
  
   //Set some variables
   ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
   ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
  
   //Bind to the ldap directory with a specific bind user
   $bd = ldap_bind($ad,"ldapaccess@example.com","passwd")
       or die("Couldn't bind to AD!");

   //Search the directory
   $result = ldap_search($ad, "OU=System Administrators,DC=example,DC=com", "samaccountname=" . $_REQUEST['form_username']);

   //Create result set
   $info = ldap_get_entries($ad, $result);

    if ($info["count"] == 0)
    {
      $auth = false;
    }
    else
    {
      $auth = true;
      $user_cn = $info[0]["cn"][0];
      $user_fn = $info[0]["givenname"][0];
      $user_sn = $info[0]["sn"][0];
      $user_mail = $info[0]["mail"][0];
    }
    // disconnect from LDAP server
    ldap_unbind($ad);

// Verify
   $ad = ldap_connect("172.16.1.103", 3268);
  
   //Bind to the ldap directory
   $adcheck = ldap_bind($ad,$_REQUEST['form_username'] . "@example.com",$_REQUEST['form_password']);
  if ($adcheck) {
	$auth = 1;
	$_SESSION['uname'] = $user_cn;
	$_SESSION['sn'] = $user_sn;
	$_SESSION['fn'] = $user_fn;
	$_SESSION['mail'] = $user_mail;
	$_SESSION['admin'] = 1;
	header("Location: index.php");
	exit;
   } else {
	$auth = 0;
	$GLOBALS['msg'] = "Invalid password";
   }

   ldap_unbind($ad);
	return($auth);
}

function showLogin() {
?>
<HTML>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<LINK href="/style.css" type=text/css rel=StyleSheet>
<BR>
    <TABLE CLASS="tblgroup" WIDTH="100%">
     <FORM METHOD="post">
     <TR><TH COLSPAN=2>ASAP Admin Login</TH>
       <TR><TD align="right" width="45%">Username:</TD><TD align="left"><input type="text" value="<? print $_REQUEST['form_username']; ?>" name="form_username"></TD><TR>
       <TD align="right">Password:</TD><TD align="left"><input type="password" name="form_password"></TD>
        <TR class="tbl_content_small"><TD COLSPAN=2 ALIGN=CENTER><?php print $GLOBALS['msg']; ?></TD><TR>
       <TD COLSPAN=2 ALIGN=CENTER><input type="submit" value="Log in"></TD><TR>
     </FORM>
    </TABLE>
</BODY></HTML>
<?
}

?>
