IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

LDAP search avec un AD [PHP 5.3]


Sujet :

Langage PHP

  1. #1
    Membre actif
    Homme Profil pro
    Inscrit en
    Mars 2005
    Messages
    546
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 546
    Points : 219
    Points
    219
    Par défaut LDAP search avec un AD
    Bonjour,

    Je souhaite pour mon application une authentification ldap sur notre AD.
    Est-ce qu'il existe des moyens de débugger une connexion ldap, car ça ne fonctionne pas et je n'ai pas de messages ni rien...
    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    $ldapconn=ldap_connect('172.17.5.10');
     
    if ($ldapconn) 
    {
      echo "Connexion LDAP anonyme réussie...<br>";
     
      $login = 'monlogin';
      $login_attr = 'samaccountname';
     
      $basedn='DC=DOMAINE,DC=local';
      $filter = '(& (samaccountname='.$login.') (objectClass=user))';
     
      if ($result = ldap_search($ldapconn, $basedn, $filter, array ("dn", "samaccountname")))
      {
        $info = ldap_get_entries($ldapconn, $result);
        if (is_array($info) AND $info['count'] == 1) 
        {
          echo "trouve : ".$info[0]['dn'];
        }
        else
        { 
          echo "pas trouve<br>";
        }
      }
      else
      {
        print_r($result);
        echo "RIEN <br>";
      }
     
    }
    else
    {
        echo "Connexion LDAP anonmye échouée...<br>";
    }
    A chaque fois il m'affiche .
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Connexion LDAP anonyme réussie...
    RIEN
    Donc je suppose qu'il y a un souci dans le ldap_search mais quoi ?
    Merci de votre aide...
    Jérôme

  2. #2
    Membre chevronné
    Avatar de ska_root
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2005
    Messages
    1 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Service public

    Informations forums :
    Inscription : Août 2005
    Messages : 1 203
    Points : 1 839
    Points
    1 839
    Par défaut
    Bonjour,

    Essaie déjà sans les options :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    if($result = ldap_search($ldapconn, $basedn, $filter)) {
       $info= ldap_get_entries($ldapconn, $result);
       print_r($info);
    }
    puis affiche ton filtre de recherche et ton dn pour en vérifier l'intégrité.

  3. #3
    Membre actif
    Homme Profil pro
    Inscrit en
    Mars 2005
    Messages
    546
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 546
    Points : 219
    Points
    219
    Par défaut
    Merci de ta réponse.
    J'ai essayé comme tu me dis c'est pas mieux...
    j'ai affiché le filter et le basedn et à priori pas de souci...
    j'ai aussi rajouté le port 389 dans la connexion
    Mais tout cela ne change rien...
    J'essaie de me connecter sur un AD 2003, peut-être qu'il faut utiliser une version de protocol spéciale ? ou une autre option particulière ?
    Et comment savoir s'il faut s'authentifier en anonyme ou non ?
    Bref pas très bavard ce ldap....
    Merci de ton aide !
    Jérôme

  4. #4
    Membre chevronné
    Avatar de ska_root
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2005
    Messages
    1 203
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Service public

    Informations forums :
    Inscription : Août 2005
    Messages : 1 203
    Points : 1 839
    Points
    1 839
    Par défaut
    je ne connais pas AD2003

    essaie de forcer la version

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
    mais là je t'avoue que c'est complétement au hasard, en fait c'est ce que je fais dans mes connexions LDAP...

    bon courage

  5. #5
    Membre actif
    Homme Profil pro
    Inscrit en
    Mars 2005
    Messages
    546
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 546
    Points : 219
    Points
    219
    Par défaut
    En cherchant j'ai trouvé ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    <?php
     
    /*                                                                            */
    /* Titre          : Fonctions LDAP Active Directory                           */
    /*                                                                            */
    /* Auteur         : SunTsu                                                    */
    /* Date édition   : 07 Jan 2007                                               */
    /*                                                                            */
     
    ?>
      <?php
     
    function convert_time($value)
    {    
      // Cette fonction à été empruntée sur un forum
        $dateLargeInt=$value; // nano secondes depuis 1601 !!!!
        $secsAfterADEpoch = $dateLargeInt / (10000000); 
    // secondes depuis le 1 jan 1601
        $ADToUnixConvertor=((1970-1601) * 365.242190) * 86400; 
    // UNIX start date - AD start date * jours * secondes
        $unixTsLastLogon=intval($secsAfterADEpoch-$ADToUnixConvertor); 
    // Unix time stamp
         $lastlogon=date("d-m-Y", $unixTsLastLogon); // Date formatée
        return $lastlogon;
    }
     
    function list_Sub_OU($server_name)
    {
        $ds=ldap_connect($server_name);
        if ($ds <> "")
      {
        /* echo "connect result seems OK ! <br />"; */
      }
        // Active directory requirement settings
      ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
      ldap_set_option($ds, LDAP_OPT_REFERRALS, );
      ldap_set_option($ds, LDAP_SCOPE_ONELEVEL, );
     
      $name="LDAPUSER";
      $ldap_user = 
    "CN=$name, OU=Users, OU=XXXXXX, OU=Units, DC=domaine1, DC=domaine2, DC=intra";
      $ldap_pass = "password";
     
     
      $r=ldap_bind($ds,$ldap_user,$ldap_pass);
      /* echo $r; */
     
      $dn = "OU=XXXXXX, OU=Units, DC=domaine1, DC=domaine2, DC=intra";
      $filter = "OU=*";
      $test = ldap_list($ds,$dn,$filter);
      $num = ldap_count_entries($ds,$test);
      $info = ldap_get_entries($ds, $test);
     
      echo "Object type : <SELECT name='TYPE'>";
      echo "<option value=''></OPTION>";
     
      $i=;        
      while ($i < $num)
      {
        $val[$i] = $info[$i]["name"][];
        $i++;
      }
     
      $i=;
      sort($val);
      while ($i < $num)
      {
        echo "<option value='".$val[$i]."'>".$val[$i]."</OPTION>";
        $i++;
      }
     
      echo 
    "</SELECT>(this is the target object type <STRONG>only users, Computers," .
    " Groups, Spécial Groups, Spécial computers </STRONG>)<BR />";
     
     
      ldap_unbind($ds);
    }
     
    function Check_DOMAIN_Acces($ldap_server,$ldap_OU,$ldap_user,$ldap_pass)
    {
        $connect=ldap_connect($ldap_server);
     
        if ($connect <> "")
        {
        ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($connect, LDAP_OPT_REFERRALS, );
        ldap_set_option($connect, LDAP_SCOPE_ONELEVEL, );
     
        $ldap_user = 
    "CN=$ldap_user, OU=Users, OU=$ldap_OU, OU=Units, DC=domaine1, DC=domaine2," .
    " DC=intra";
        $check_bind=ldap_bind($connect,$ldap_user,$ldap_pass);
     
        if ($check_bind == 1)
        {
            $_SESSION['LDAPUSER'] = $ldap_user;
            $_SESSION['LDAPPASS'] = $ldap_pass;
            $_SESSION['LDAPOU'] = $ldap_OU;
            $_SESSION['LDAPSERV'] = $ldap_server;
     
            echo "<SCRIPT>alert('Access Granted to domaine1')</SCRIPT>";
        }
        else
        {
           echo "<SCRIPT>alert('Access Denied to domaine1')</SCRIPT>"; 
        }  
      }
      else
      {
          echo 
    "<SCRIPT>alert('domaine1 Server $ldap_server did not respond')</SCRIPT>";
      }
     
      ldap_unbind($connect);
    }
     
     
    function Split_group($input)
    {
        $Group_desc_array = split("=",$input);
     
        if ($Group_desc_array[2] == "Groups,OU" || $Group_desc_array[2] == 
    "Users,OU" || $Group_desc_array[2] == "Computers,OU")
        {
          $Group_array = split(",",$Group_desc_array[1]);
          echo $Group_array[]."<BR>";
          return $Group_array[];
      }
      else
      {
         if ($Group_desc_array[2] == "Special Groups,OU" || $Group_desc_array[2] == 
    "Users,OU" || $Group_desc_array[2] == "Computers,OU")
        {
            $Special_Group_array = split(",",$Group_desc_array[1]);
            echo $Special_Group_array[]."<BR>";
            return $Special_Group_array[];
        } 
      }
    }
     
    function list_OU($server_name)
    {
        $ds=ldap_connect($server_name);
        if ($ds <> "")
      {
        /* echo "connect result seems OK ! <br />"; */
      }
        // Active directory requirement settings
      ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
      ldap_set_option($ds, LDAP_OPT_REFERRALS, );
      ldap_set_option($ds, LDAP_SCOPE_ONELEVEL, );
     
      $name="LDAPUSER";
      $ldap_user = 
    "CN=$name, OU=Users, OU=XXXXXX, OU=Units, DC=domaine1, DC=domaine2, DC=intra";
      $ldap_pass = "password";
     
     
      $r=ldap_bind($ds,$ldap_user,$ldap_pass);
      /* echo $r; */
     
      $dn = "OU=Units, DC=domaine1, DC=domaine2, DC=intra";
      $filter = "OU=*";
      $test = ldap_list($ds,$dn,$filter);
      $num = ldap_count_entries($ds,$test);
      $info = ldap_get_entries($ds, $test);
     
      echo "OU Name : <SELECT name='OU'>";
      echo "<option value=''></OPTION>";
     
      $i=;        
      while ($i < $num)
      {
        $val[$i] = $info[$i]["name"][];
        $i++;
      }
     
      $i=;
      sort($val);
      while ($i < $num)
      {
        echo "<option value='".$val[$i]."'>".$val[$i]."</OPTION>";
        $i++;
      }
     
      echo "</SELECT>(this is the target OU name)<BR />";
     
     
      ldap_unbind($ds);
    }
     
    $server_name = $_POST['SERVNAME'];
    $name = $_POST['NAME'];
    $ldap_pass = $_POST['PASS'];
    $ou = $_POST['OU'];
     
     
    echo "<h3>LDAP query test</h3>";
     
    echo "<PRE>
    This is an alpha version of this application for the moment you can only read" .
    " Active Directory (LDAP).
    My final target is to make easier all of our boring tasks like reset" .
    " password, computer remove/create...
    I also want that the infos you want to get come in a small amount of time in" .
    " one click or so.
    For the moment i'm trying to master what's behind Active Directory so I can" .
    " finish this application.
    Enjoy ! 
    
    SunTsu 
    </PRE>";
    echo "<form method='POST' action='index.php'>";
    echo 
    "Server Name : <input name='SERVNAME' type='text'" .
    " value='domaine1DC121A'>(this is the domain controler used for queries)<BR />";
    /* echo "OU Name : <input name='OU' type='text' value='".$ou."'>(this is the
     target OU name)<BR />"; */
    echo "User Name : <input name='NAME' type='text' value='".$name.
    "'>(this is your user full name ex: John Doe)<BR />";
    echo "Password : <input name='PASS' type='password' value='".$ldap_pass.
    "'>(this is your domaine1 password)<BR />";
    list_OU("domaine1DC121A");
    list_sub_OU("domaine1DC121A");
    echo 
    "Filter : <input name='FILTER' type='text' value=''>(this is a part of the" .
    " full name of the domaine1 Object you are looking for)<BR />";
    echo "<input name='submit' type='submit' value='Select'><BR />";
    echo "</form>";
     
    if ($_POST['SERVNAME'] <> "" && $_POST['NAME'] <> "" && $_POST['PASS'] <> "" && 
    $_POST['FILTER'] <> "" && $_POST['OU'] <> "" && $_POST['TYPE'] <> "")
    {
        $name = $_POST['NAME'];
        $getfilt = $_POST['FILTER'];
        $type = $_POST['TYPE'];
        $ou = $_POST['OU'];
        $ldap_user = 
    "CN=$name, OU=Users, OU=XXXXXX, OU=Units, DC=domaine1, DC=domaine2, DC=intra";
      $ldap_pass = $_POST['PASS'];
      $server_name = $_POST['SERVNAME'];
     
      echo "Connecting ...to ".$server_name." <BR />";
      echo "with user : ".$ldap_user."<BR />";
      $ds=ldap_connect($server_name);
     
      // Active directory requirement settings
      ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
      ldap_set_option($ds, LDAP_OPT_REFERRALS, );
     
      if ($ds <> "")
      {
        echo "connect result seems OK ! <br />";
      }
     
      if ($ds) 
      { 
        echo "Binding ..."; 
        $r=ldap_bind($ds,$ldap_user,$ldap_pass);
     
        if ($r == 1)
        {
            $ok = "OK !";
        }
        else
        {
            $ok = "FAILURE !";
        }
        echo "Bind result is " . $ok . "<br />";
        $dn = "OU=$type, OU=$ou, OU=Units, DC=domaine1, DC=domaine2, DC=intra";
        $filter = "CN=$getfilt*";
        $test = ldap_search($ds,$dn,$filter);
     
        $num = ldap_count_entries($ds,$test);
        echo "object number = ".$num."<BR />";
        $info = ldap_get_entries($ds, $test);
     
        if ($type == "Users")
        {
            $data=array("employeeid","cn","title","physicaldeliveryofficename",
    "telephonenumber","description","displayname","mail","homedirectory",
    "scriptpath","msexchhomeservername","lastlogon","pwdlastset",
    "mdboverhardquotalimit","publicdelegatesbl","memberof");
        }
        else
        {
            if ($type == "Computers" || $type == "Special Computers")
            {
              $data=array("name","location","operatingsystem",
    "operatingsystemversion","operatingsystemservicepack","memberof");
          }
          else
          {
              if ($type == "Groups" || $type == "Special Groups")
              {
                $data=array("name","description","member","memberof","mail");
            }
          }
        }  
     
        $i = ;
        $z = ;
     
        while ($i < $num)
        {
            echo "<BR><BR><STRONG>Data for object : ".$i." : </STRONG><BR />";
            $g = ;
          $x = ;
          while ($x < count($data))
          {
            $thedata=$data[$x];
     
            if ($thedata == "memberof" || $thedata == "publicdelegatesbl" || 
    $thedata == "member")
            {
                $numb_m = $info[$i][$thedata]["count"];
     
                if ($thedata == "memberof")
                {
                  echo "<BR /><STRONG>Member of following groups : </STRONG><BR />";
              }
                else
                {
                    if ($thedata == "publicdelegatesbl")
                    {
                  echo 
    "<BR /><STRONG>Member of following delegated mailbox(es) : </STRONG><BR />";
                }
                else
                {
                    echo 
    "<BR /><STRONG>Contain the following member(s) : </STRONG><BR />";
                }
              }
     
                $m=;
                while ($m < $numb_m)
                {
                   $n = $info[$i][$thedata][$m];
                   $n = utf8_decode($n);
                $ready_to_paste[$m] = Split_group($n);  
                  $m++;
              }
     
              $m=;
     
              echo "<BR>Ready to paste : <BR>";
              while ($m < $numb_m)
              {
                  echo $ready_to_paste[$m].";";
                  $m++;
              }
              echo "<BR>";
            }
            else
            {
     
                if ($thedata == "lastlogon" || $thedata == "pwdlastset")
                {
                    $value = $info[$i][$thedata][$z];
                  $n = convert_time($value); // convert nano seconds
              }
              else
              {
                  $n = $info[$i][$thedata][$z];
                  $n = utf8_decode($n); // conversion en caractère Latin...
                  if ($n == "")
                  {
                    $thefault[$g] = $thedata;
                    $n = "N/A";
                    $g++;
                }
              }
     
                 echo "- ".$thedata." : ".$n."<BR />";
            }
     
               $x++;
          }
     
        echo "<BR>Number of missing parameter for this account : ".$g."<BR>";
        echo "Missing parameter list : <BR>";
     
        $xy = ;
     
        while ($xy < $g)
        {
            echo ($xy+1).") ".$thefault[$xy]."<BR>";
            $xy++;
        }
     
        $i++;
        }
     
        ldap_unbind($ds);
      }
     
    }
    else
    {
      echo "Missing Data !!!";
    }
     
    ?>
    et apparemment :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
        // Active directory requirement settings
      ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
      ldap_set_option($ds, LDAP_OPT_REFERRALS, );
      ldap_set_option($ds, LDAP_SCOPE_ONELEVEL, );
    Donc j'ai modifié mon script pour y ajouter les options demandés et ça marche mieux... Enfin quand on met ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
      ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
      ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
    et je récupère bien mon user...
    Merci de ton aide !
    Jérôme

  6. #6
    Membre actif
    Homme Profil pro
    Inscrit en
    Mars 2005
    Messages
    546
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 546
    Points : 219
    Points
    219
    Par défaut
    Et je précise qu'il faut s'attacher avec ldap_bind car mon annuaire ne veut pas des connexions anonymes...
    Jérôme

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [LDAP] Problème avec la fonction ldap_add
    Par Invité dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 12/06/2008, 15h04
  2. Réponses: 1
    Dernier message: 21/02/2008, 14h47
  3. [LDAP] Pb avec l'arborescence
    Par Longrais dans le forum API standards et tierces
    Réponses: 1
    Dernier message: 21/11/2006, 16h04
  4. [LDAP] problème avec ldap_bind
    Par xave dans le forum Bibliothèques et frameworks
    Réponses: 10
    Dernier message: 01/08/2006, 11h55
  5. [LDAP] Compatibilité avec Thunderbird
    Par benratti dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 29/03/2006, 12h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo