| 1 | <?php
|
|---|
| 2 | require_once("config.php");
|
|---|
| 3 |
|
|---|
| 4 | function login()
|
|---|
| 5 | {
|
|---|
| 6 | global $ldaphost;
|
|---|
| 7 | global $baseDN;
|
|---|
| 8 | global $groupDN;
|
|---|
| 9 |
|
|---|
| 10 | $username = "";
|
|---|
| 11 |
|
|---|
| 12 | if( isset($_POST['Uname']) )
|
|---|
| 13 | {
|
|---|
| 14 | $username = $_POST['Uname'];
|
|---|
| 15 | }
|
|---|
| 16 | if( isset($_POST['Passwd']) )
|
|---|
| 17 | {
|
|---|
| 18 | $password = $_POST['Passwd'];
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | $con = @ldap_connect($ldaphost);
|
|---|
| 22 | if (!$con)
|
|---|
| 23 | return "ldap_connect failed to ".$ldaphost;
|
|---|
| 24 |
|
|---|
| 25 | //------------------ Look for user common name
|
|---|
| 26 | $attributes = array('cn', 'mail');
|
|---|
| 27 | $dn = 'ou=People,'.$baseDN;
|
|---|
| 28 | $filter = '(uid='.$username.')';
|
|---|
| 29 |
|
|---|
| 30 | $sr = @ldap_search($con, $dn, $filter, $attributes);
|
|---|
| 31 | if (!$sr)
|
|---|
| 32 | return "ldap_search failed for dn=".$dn.": ".ldap_error($con);
|
|---|
| 33 |
|
|---|
| 34 | $srData = @ldap_get_entries($con, $sr);
|
|---|
| 35 | if ($srData["count"]==0)
|
|---|
| 36 | return "No results returned by ldap_get_entries for dn=".$dn.".";
|
|---|
| 37 |
|
|---|
| 38 | $email =$srData[0]['mail'][0];
|
|---|
| 39 | $userCommonName=$srData[0]['cn'][0];
|
|---|
| 40 | $userDN =$srData[0]['dn'];
|
|---|
| 41 |
|
|---|
| 42 | //------------------ Authenticate user
|
|---|
| 43 | if (!@ldap_bind($con, $userDN, $password))
|
|---|
| 44 | return "ldap_bind failed: ".ldap_error($con);
|
|---|
| 45 |
|
|---|
| 46 | //------------------ Check if the user is in FACT ldap group
|
|---|
| 47 | $attributes= array("member");
|
|---|
| 48 | $filter= '(objectClass=*)';
|
|---|
| 49 |
|
|---|
| 50 | // Get all members of the group.
|
|---|
| 51 | $sr = @ldap_read($con, $groupDN, $filter, $attributes);
|
|---|
| 52 | if (!$sr)
|
|---|
| 53 | return "ldap_read failed for dn=".$groupDN.": ".ldap_error($con);
|
|---|
| 54 |
|
|---|
| 55 | // retrieve the corresponding data
|
|---|
| 56 | $srData = @ldap_get_entries($con, $sr);
|
|---|
| 57 | if ($srData["count"]==0)
|
|---|
| 58 | return "No results returned by ldap_get_entries for dn=".$dn.".";
|
|---|
| 59 |
|
|---|
| 60 | @ldap_unbind($con);
|
|---|
| 61 |
|
|---|
| 62 | foreach ($srData[0]['member'] as $member)
|
|---|
| 63 | if (strpos($member, "cn=".$userCommonName.",")===0)
|
|---|
| 64 | return "";
|
|---|
| 65 |
|
|---|
| 66 | return "Sorry, your credentials don't match!";
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | ?>
|
|---|
| 70 |
|
|---|