Guest User

Untitled

a guest
Aug 10th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. PHP LDAP, adding new entries into LDAP
  2. <?php
  3. $name=$_REQUEST['name'];
  4. $x=1;
  5. if($x==1)
  6. {
  7. //LDAP stuff here.
  8. $username = "myusername";
  9. $password = "mypass";
  10.  
  11.  
  12. $ds = ldap_connect('ldap://ldap:389');
  13.  
  14. ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
  15. ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
  16.  
  17. //Can't connect to LDAP.
  18. if( !ds )
  19. {
  20. echo "Error in contacting the LDAP server -- contact ";
  21. echo "technical services! (Debug 1)";
  22.  
  23. exit;
  24. }
  25.  
  26. //Connection made -- bind anonymously and get dn for username.
  27. $bind = @ldap_bind($ds);
  28.  
  29. //Check to make sure we're bound.
  30. if( !bind )
  31. {
  32. echo "Anonymous bind to LDAP FAILED. Contact Tech Services! (Debug 2)";
  33.  
  34. exit;
  35. }
  36.  
  37. $search = ldap_search($ds, "ou=People,DC=sde,DC=goliat,DC=com", "uid=$username");
  38.  
  39. //Make sure only ONE result was returned -- if not, they might've thrown a * into the username. Bad user!
  40. if( ldap_count_entries($ds,$search) != 1 )
  41. {
  42. echo "Error processing username -- please try to login again. (Debug 3)";
  43. redirect(_WEBROOT_ . "/try1b.php");
  44.  
  45. exit;
  46. }
  47.  
  48. $info = ldap_get_entries($ds, $search);
  49.  
  50. //Now, try to rebind with their full dn and password.
  51. $bind = @ldap_bind($ds, $info[0][dn], $password);
  52. if( !$bind || !isset($bind))
  53. {
  54. echo "Login failed -- please try again. (Debug 4)";
  55. redirect(_WEBROOT_ . "/try1b.php");
  56.  
  57. exit;
  58. }
  59.  
  60. //Now verify the previous search using their credentials.
  61. $search = ldap_search($ds, "ou=People,DC=sde,DC=goliat,DC=com", "cn=$name");
  62.  
  63. $info = ldap_get_entries($ds, $search);
  64. if( $username == "myusername" )
  65. {
  66.  
  67. /*
  68. very useful set of information to view the LDAP tree info from an array
  69. echo $username;
  70. echo "<pre>".print_r($info[0],true)."</pre><br />";
  71. */
  72. echo $info[0][cn][0];
  73. echo ",";
  74. echo $info[0][mail][0];
  75. echo ",";
  76. echo $info[0][telephonenumber][0];
  77.  
  78. exit;
  79. }
  80. else
  81. {
  82. echo "Error. Access Denied";
  83. redirect(_WEBROOT_ . "/try1b.php");
  84.  
  85. exit;
  86. }
  87. ldap_close($ds);
  88. exit;
  89. }
  90. ?>
Add Comment
Please, Sign In to add comment