forked from RackTables/racktables-contribs
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ad-import-to-racktables.php
77 lines (54 loc) · 1.97 KB
/
ad-import-to-racktables.php
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
#!/usr/bin/php -q
<?php
// Path to racktables secret.php file with db credentials
require "/var/www/html/inc/secret.php";
$racktables_perms = "# NOTE: Do not edit this file since it is created automatically by a cron job every night\n\n";
$ds=ldap_connect("ad.yourcompany.com");
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
// Create this normal Domain User first to be able to read from AD
$ldapbind = ldap_bind($ds, "ServiceAccount", "ThePasswordHere");
if (!$ldapbind) {
echo "LDAP bind failed...";
}
// START
$group = "MyAdminGroup";
$racktables_perms .= "# Users imported from Active Directory group $group at " . date("Y-m-d H:i:s") . "\n\n";
$dn = "DC=ad,DC=yourcompany,DC=com";
$filter="(|(cn=$group))";
$props = array("member");
$sr=ldap_search($ds, $dn, $filter, $props);
$entry = ldap_first_entry($ds, $sr);
$attrs = ldap_get_attributes($ds, $entry);
$values = ldap_get_values($ds, $entry,"member");
for ($i=0; $i < $values["count"]; $i++)
{
$filter = "(objectclass=*)";
$props = array("sAMAccountName");
$sr=ldap_read($ds, $values[$i], $filter, $props);
$entry = ldap_get_entries($ds, $sr);
$user = strtolower($entry[0]["samaccountname"][0]);
$racktables_perms .= "allow {\$username_$user}\n";
}
$racktables_perms .= "\n";
//END
//ADD ANOTHER GROUP IMPORT HERE IF NEEDED
ldap_close($ds);
$racktables_perms .= "\n";
$racktables_perms .= "# Admin and Default (read-only)\n";
$racktables_perms .= "allow {\$userid_1} or {\$tab_default}\n";
//Now update the database
try {
$dbh = new PDO($pdo_dsn, $db_username, $db_password);
$count = $dbh->exec("UPDATE Script SET script_text = '$racktables_perms' WHERE script_name = 'RackCode'");
if ($count != 1) {
echo "Warning: No records affected by UPDATE statement!";
}
$dbh->exec ('UPDATE Script SET script_text = NULL WHERE script_name = "RackCodeCache"');
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>