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 !!!";
}
?> |
Partager