<?php
ini_set
('track_errors','on');
ini_set('display_errors','on');

echo 
'<style>';
echo 
'
table {
    width:100%;
}
table thead th {
    text-align:left;
}
'
;
echo 
'</style>';
//phpinfo();
//exit;
echo '<h1>LAB3W &copy ZW3B :-: Lightweight Directory Access Protocol</h1>';

// Eléments d'authentification LDAP
$ldapconfig['rdn'] = NULL;
$ldapconfig['rpw'] = NULL;

// LDAP variables
$ldapconfig['host'] = 'ldaps://ldap.lab3w.fr';
$ldapconfig['port'] = 636;

$ldapconfig['basedn'] = 'dc=lab3w,dc=fr';


// Connexion LDAP
$ldapconn ldap_connect($ldapconfig['host'], $ldapconfig['port'])
          or die(
"Impossible de se connecter au serveur LDAP ".$ldapconfig['host']);

ldap_set_option($ldapconnLDAP_OPT_PROTOCOL_VERSION3);
ldap_set_option($ldapconnLDAP_OPT_REFERRALS0);


if (
$ldapconn) {

    
//if(ldap_start_tls($ldapconn)):

    // Connexion au serveur LDAP
    
$ldapbind = @ldap_bind($ldapconn$ldapconfig['rdn'], $ldapconfig['rpw']);

    
// Vérification de l'authentification
    
if ($ldapbind) {
        
//echo "Connexion LDAP r&eacute;ussie...";
        //-------------------------------------------------------------------------------------

        
if ( !$search = @ldap_search($ldapconn'ou=IdMap,'.$ldapconfig['basedn'], '(cn=*)') ) {

            echo(
"Unable to search ldap server<br>");
            echo(
"msg:'".ldap_error($ldapbind)."'</br>");#check the message again
            
echo 0;

        } else {

            
$number_returned ldap_count_entries($ldapconn$search);
            
$info ldap_get_entries($ldapconn$search);

            
//echo '<p>'.$number_returned.' entrr&eacute;es pour la recherche : <code>'.$filter.'</code> sur  <code>'.$ldapconfig['basedn'].'</code><p>';

            //          for ($i=0; $i<$info["count"]; $i++) {
            //            var_dump($info[$i]);#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry
            //          }

            
echo '<h3>LDAP IdMap : '.$number_returned.'</h3>'."\n";

            echo 
'<table>'."\n";

            echo 
'<thead>'."\n";
            echo 
'<tr>'."\n";
            echo 
'<th>';
            
//echo 'uidNumber:gidNumber';
            
echo '</th>';
            echo 
'<th>';
            echo 
'cn';
            echo 
'</th>';
            echo 
'<th>';
            
//echo 'gecos';
            
echo '</th>';
            echo 
'</tr>'."\n";
            echo 
'</thead>'."\n";

            echo 
'<tbody>'."\n";
            foreach (
$info as $infos) {
                if(
is_array($infos)):
                echo 
'<tr>'."\n";
                echo 
'<td>';
                
//echo $infos['uidnumber'][0].':'.$infos['gidnumber'][0];
                
echo '</td>';
                echo 
'<td>';
                echo 
''.$infos['cn'][0];
                echo 
'</td>';
                echo 
'<td>';
                
//echo ''.(isset($infos['gecos'][0]) && !empty($infos['gecos'][0]) ? $infos['gecos'][0]  : '');
                
echo '</td>';
                echo 
'</tr>'."\n";
                endif;
            }
            echo 
'</tbody>'."\n";


            echo 
'</table>'."\n";

            
//echo '<pre>'.print_r($info,1).'</pre>';#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry

        
}

        
//-------------------------------------------------------------------------------------

        
if ( !$search = @ldap_search($ldapconn'ou=Domains,'.$ldapconfig['basedn'], '(cn=*)') ) {

            echo(
"Unable to search ldap server<br>");
            echo(
"msg:'".ldap_error($ldapbind)."'</br>");#check the message again
            
echo 0;

        } else {

            
$number_returned ldap_count_entries($ldapconn$search);
            
$info ldap_get_entries($ldapconn$search);

            
//echo '<p>'.$number_returned.' entrr&eacute;es pour la recherche : <code>'.$filter.'</code> sur  <code>'.$ldapconfig['basedn'].'</code><p>';

            //          for ($i=0; $i<$info["count"]; $i++) {
            //            var_dump($info[$i]);#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry
            //          }

            
echo '<h3>LDAP Domains : '.$number_returned.'</h3>'."\n";

            echo 
'<table>'."\n";

            echo 
'<thead>'."\n";
            echo 
'<tr>'."\n";
            echo 
'<th>';
            
//echo 'uidNumber:gidNumber';
            
echo '</th>';
            echo 
'<th>';
            echo 
'cn';
            echo 
'</th>';
            echo 
'<th>';
            
//echo 'gecos';
            
echo '</th>';
            echo 
'</tr>'."\n";
            echo 
'</thead>'."\n";

            echo 
'<tbody>'."\n";
            foreach (
$info as $infos) {
                if(
is_array($infos)):
                echo 
'<tr>'."\n";
                echo 
'<td>';
                
//echo $infos['uidnumber'][0].':'.$infos['gidnumber'][0];
                
echo '</td>';
                echo 
'<td>';
                echo 
''.$infos['cn'][0];
                echo 
'</td>';
                echo 
'<td>';
                
//echo ''.(isset($infos['gecos'][0]) && !empty($infos['gecos'][0]) ? $infos['gecos'][0]  : '');
                
echo '</td>';
                echo 
'</tr>'."\n";
                endif;
            }
            echo 
'</tbody>'."\n";


            echo 
'</table>'."\n";

            
//echo '<pre>'.print_r($info,1).'</pre>';#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry

        
}
        
//-------------------------------------------------------------------------------------

        
if ( !$search = @ldap_search($ldapconn''.$ldapconfig['basedn'], '(sambaDomainName=*)') ) {

            echo(
"Unable to search ldap server<br>");
            echo(
"msg:'".ldap_error($ldapbind)."'</br>");#check the message again
            
echo 0;

        } else {

            
$number_returned ldap_count_entries($ldapconn$search);
            
$info ldap_get_entries($ldapconn$search);

            
//echo '<p>'.$number_returned.' entrr&eacute;es pour la recherche : <code>'.$filter.'</code> sur  <code>'.$ldapconfig['basedn'].'</code><p>';

            //          for ($i=0; $i<$info["count"]; $i++) {
            //            var_dump($info[$i]);#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry
            //          }

            
echo '<h3>LDAP SambaDomain : '.$number_returned.'</h3>'."\n";

            echo 
'<table>'."\n";

            echo 
'<thead>'."\n";
            echo 
'<tr>'."\n";
            echo 
'<th>';
            echo 
'uidnumber:gidNumber';
            echo 
'</th>';
            echo 
'<th>';
            echo 
'sambadomainname';
            echo 
'</th>';
            echo 
'<th>';
            echo 
'sambanextrid';
            echo 
'</th>';
            echo 
'<th>';
            echo 
'sambasid';
            echo 
'</th>';
            echo 
'</tr>'."\n";
            echo 
'</thead>'."\n";

            echo 
'<tbody>'."\n";
            foreach (
$info as $infos) {
                if(
is_array($infos)):
                echo 
'<tr>'."\n";
                echo 
'<td>';
                echo 
$infos['uidnumber'][0].':'.$infos['gidnumber'][0];
                echo 
'</td>';
                echo 
'<td>';
                echo 
''.$infos['sambadomainname'][0];
                echo 
'</td>';
                echo 
'<td>';
                echo 
''.$infos['sambanextrid'][0];
                echo 
'</td>';
                echo 
'<td>';
                echo 
''.(isset($infos['sambasid'][0]) && !empty($infos['sambasid'][0]) ? $infos['sambasid'][0]  : 'NO SAMBASID');
                echo 
'</td>';
                echo 
'</tr>'."\n";
                endif;
            }
            echo 
'</tbody>'."\n";


            echo 
'</table>'."\n";

            
//echo '<pre>'.print_r($info,1).'</pre>';#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry

        
}

        
//-------------------------------------------------------------------------------------

        
if ( !$search = @ldap_search($ldapconn'ou=Computers,'.$ldapconfig['basedn'], '(cn=*)') ) {

            echo(
"Unable to search ldap server<br>");
            echo(
"msg:'".ldap_error($ldapbind)."'</br>");#check the message again
            
echo 0;

        } else {

            
$number_returned ldap_count_entries($ldapconn$search);
            
$info ldap_get_entries($ldapconn$search);

            
//echo '<p>'.$number_returned.' entrr&eacute;es pour la recherche : <code>'.$filter.'</code> sur  <code>'.$ldapconfig['basedn'].'</code><p>';

            //          for ($i=0; $i<$info["count"]; $i++) {
            //            var_dump($info[$i]);#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry
            //          }

            
echo '<h3>LDAP Computers : '.$number_returned.'</h3>'."\n";

            echo 
'<table>'."\n";

            echo 
'<thead>'."\n";
            echo 
'<tr>'."\n";
            echo 
'<th>';
            
//echo 'uidNumber:gidNumber';
            
echo '</th>';
            echo 
'<th>';
            echo 
'cn';
            echo 
'</th>';
            echo 
'<th>';
            
//echo 'gecos';
            
echo '</th>';
            echo 
'</tr>'."\n";
            echo 
'</thead>'."\n";

            echo 
'<tbody>'."\n";
            foreach (
$info as $infos) {
                if(
is_array($infos)):
                echo 
'<tr>'."\n";
                echo 
'<td>';
                
//echo $infos['uidnumber'][0].':'.$infos['gidnumber'][0];
                
echo '</td>';
                echo 
'<td>';
                echo 
''.$infos['cn'][0];
                echo 
'</td>';
                echo 
'<td>';
                
//echo ''.(isset($infos['gecos'][0]) && !empty($infos['gecos'][0]) ? $infos['gecos'][0]  : '');
                
echo '</td>';
                echo 
'</tr>'."\n";
                endif;
            }
            echo 
'</tbody>'."\n";


            echo 
'</table>'."\n";

            
//echo '<pre>'.print_r($info,1).'</pre>';#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry

        
}

        
//-------------------------------------------------------------------------------------

        //$filter = "(objectClass=*)";
        //$filter = "(cn=*)";
        //$filter = "(cn=*)";

        
if ( !$search = @ldap_search($ldapconn'ou=Group,'.$ldapconfig['basedn'], '(cn=*)') ) {

          echo(
"Unable to search ldap server<br>");
          echo(
"msg:'".ldap_error($ldapbind)."'</br>");#check the message again
          
echo 0;

        } else {

          
$number_returned ldap_count_entries($ldapconn$search);
          
$info ldap_get_entries($ldapconn$search);

          
//echo '<p>'.$number_returned.' entrr&eacute;es pour la recherche : <code>'.$filter.'</code> sur  <code>'.$ldapconfig['basedn'].'</code><p>';

//          for ($i=0; $i<$info["count"]; $i++) {
//            var_dump($info[$i]);#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry
//          }

          
echo '<h3>LDAP Groups : '.$number_returned.'</h3>'."\n";

          echo 
'<table>'."\n";

          echo 
'<thead>'."\n";
          echo 
'<tr>'."\n";
              echo 
'<th>';
                  echo 
'gidNumber';
              echo 
'</th>';
              echo 
'<th>';
                  echo 
'cn';
              echo 
'</th>';
             echo 
'<th>';
                  echo 
'description';
              echo 
'</th>';
          echo 
'</tr>'."\n";
          echo 
'</thead>'."\n";

          echo 
'<tbody>'."\n";
          foreach (
$info as $infos) {
              if(
is_array($infos)):
              echo 
'<tr>'."\n";
              echo 
'<td>';
                  echo 
''.$infos['gidnumber'][0];
              echo 
'</td>';
              echo 
'<td>';
                  echo 
''.$infos['cn'][0];
              echo 
'</td>';
              echo 
'<td>';
                  echo 
''.$infos['description'][0];
              echo 
'</td>';
              echo 
'</tr>'."\n";
              endif;
          }
          echo 
'</tbody>'."\n";


          echo 
'</table>'."\n";

            
//echo '<pre>'.print_r($info,1).'</pre>';#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry

        
}

        
//-------------------------------------------------------------------------------------

        
if ( !$search = @ldap_search($ldapconn'ou=People,'.$ldapconfig['basedn'], '(cn=*)') ) {

            echo(
"Unable to search ldap server<br>");
            echo(
"msg:'".ldap_error($ldapbind)."'</br>");#check the message again
            
echo 0;

        } else {

            
$number_returned ldap_count_entries($ldapconn$search);
            
$info ldap_get_entries($ldapconn$search);

            
//echo '<p>'.$number_returned.' entrr&eacute;es pour la recherche : <code>'.$filter.'</code> sur  <code>'.$ldapconfig['basedn'].'</code><p>';

            //          for ($i=0; $i<$info["count"]; $i++) {
            //            var_dump($info[$i]);#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry
            //          }

            
echo '<h3>LDAP Accounts : '.$number_returned.'</h3>'."\n";

            echo 
'<table>'."\n";

            echo 
'<thead>'."\n";
            echo 
'<tr>'."\n";
            echo 
'<th>';
            echo 
'uidNumber:gidNumber';
            echo 
'</th>';
            echo 
'<th>';
            echo 
'cn';
            echo 
'</th>';
            echo 
'<th>';
            echo 
'gecos';
            echo 
'</th>';
            echo 
'</tr>'."\n";
            echo 
'</thead>'."\n";

            echo 
'<tbody>'."\n";
            foreach (
$info as $infos) {
                if(
is_array($infos)):
                echo 
'<tr>'."\n";
                echo 
'<td>';
                    echo 
$infos['uidnumber'][0].':'.$infos['gidnumber'][0];
                echo 
'</td>';
                echo 
'<td>';
                    echo 
''.$infos['cn'][0];
                echo 
'</td>';
                echo 
'<td>';
                    echo 
''.(isset($infos['gecos'][0]) && !empty($infos['gecos'][0]) ? $infos['gecos'][0]  : '');
                echo 
'</td>';
                echo 
'</tr>'."\n";
                endif;
            }
            echo 
'</tbody>'."\n";


            echo 
'</table>'."\n";

            
//echo '<pre>'.print_r($info,1).'</pre>';#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry

        
}

        
//-------------------------------------------------------------------------------------



/*


          $filter = "(group=*)";
        if($search = ldap_search($ldapconn, $ldapconfig['basedn'], $filter)) {

            $number_returned = ldap_count_entries($ldapconn, $search);
            $info = ldap_get_entries($ldapconn, $search);

            echo "The number of entries returned is ". $number_returned."<p>";
            echo 'il y a des groups';

        }
*/


        
}



        else {
            echo 
"Connexion LDAP &eacute;chou&eacute;e...";
            
//echo("msg:'".ldap_error($ldapconn)."'</br>");#check the message again
        
}


        
//endif;

}

//
//echo '<hr/>';

exit;

















$username = isset($_GET['user']) && !empty($_GET['user']) ? $_GET['user'] : 'orj';
$password '';

$ldapconfig['host'] = 'ldap.lab3w.fr';
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = 'dc=lab3w,dc=fr';

$ds=ldap_connect($ldapconfig['host'], $ldapconfig['port']);

$dn="uid=".$username.",ou=people,".$ldapconfig['basedn'];

if (
$bind=ldap_bind($ds$dn$password)) {
  echo(
"Login correct");
} else {

  echo(
"Unable to bind to server.</br>");

  echo(
"msg:'".ldap_error($ds)."'</br>");#check if the message isn't: Can't contact LDAP server :)
  #if it say something about a cn or user then you are trying with the wrong $dn pattern i found this by looking at OpenLDAP source code :)
  #we can figure out the right pattern by searching the user tree
  #remember to turn on the anonymous search on the ldap server

  
if ($bind=ldap_bind($ds)) {

    
$filter "(cn=*)";

    if (!(
$search=@ldap_search($ds$ldapconfig['basedn'], $filter))) {

      echo(
"Unable to search ldap server<br>");
      echo(
"msg:'".ldap_error($ds)."'</br>");#check the message again

    
} else {

      
$number_returned ldap_count_entries($ds,$search);
      
$info ldap_get_entries($ds$search);
      
//echo "The number of entries returned is ". $number_returned."<p>";

      //for ($i=0; $i<$info["count"]; $i++) {
        //var_dump($info[$i]);#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry
      //}

       
echo '<h3>LDAP Accounts : '.$number_returned.'</h3>'."\n";
       echo 
'<table>'."\n";

       echo 
'<thead>'."\n";
       echo 
'<tr>'."\n";
               echo 
'<td>';
                   echo 
'pid:gid';
               echo 
'<td>';
               echo 
'<td>';
                   echo 
'cn';
               echo 
'<td>';
               echo 
'<td>';
                   echo 
'geos';
               echo 
'<td>';
       echo 
'</tr>'."\n";
       echo 
'</thead>'."\n";

       echo 
'<tbody>'."\n";
       foreach (
$info as $infos) {
               echo 
'<tr>'."\n";
               echo 
'<td>';
                   echo 
''.$infos['uidNumber'].' : '.$infos['gidNumber'];
               echo 
'<td>';
               echo 
'<td>';
                   echo 
''.$infos['cn'];
               echo 
'<td>';
               echo 
'<td>';
                   echo 
''.$infos['geos'];
               echo 
'<td>';
               echo 
'</tr>'."\n";
       }
       echo 
'</tbody>'."\n";
       echo 
'</table>'."\n";


       
//echo '<pre>'.print_r($info,1).'</pre>';#look for your user account in this pile of junk and apply the whole pattern where you build $dn to match exactly the ldap tree entry


    
}
  }


  else {
    echo(
"Unable to bind anonymously<br>");
    echo(
"msg:".ldap_error($ds)."<br>");
  }


}


?>