Posted by mcutting on Fri 1st May 10:46 (modification of post by view diff)
download | new post
- <?php
- /********************
- A highly verbose PHP script to import new Active Directory users into Zentrack via LDAP
- Adds new user accounts into Zentrack's database users table (zentrack_users)
- Updates existing user accounts / Removes accounts that no longer exist
- ********************/
- // AD/LDAP settings
- // AD USER SHOULD BE AN ACCOUNT THAT HAS RIGHTS TO READ FROM ACTIVE DIRECTORY
- // ENTER THE ADDRESS OF YOUR LDAP SERVER HERE
- $dc = "ldap://";
- // ENTER THE FULL AD PATH OF YOUR USER HERE
- $user = "";
- // ENTER THE PASSWORD OF YOUR AD USER HERE
- $pw = "";
- //ENTER THE FULL DN FOR YOUR DOMAIN, EXAMPLE dc=microsoft,dc=com
- $dn = "";
- // ENTER ATTRIBUTES THAT YOU WISH TO PULL FROM AD, FOR EXAMPLE "displayname" AND "givenname" ETC
- // ENTER FILTER ATTRIBUTES HERE, FOR EXAMPLE "person" AND "telephoneNumber"
- $filter = "(&(objectClass=person)(objectCategory=person)(telephoneNumber=*)(mail=*youremail.com)(cn=*))";
- // MySQL settings
- $mysql_server = "localhost";
- $mysql_user = "";
- $mysql_password = "";
- $mysql_database = "zentrack";
- /*
- The following allow us to define users in the local db that should not be deleted.
- The second variable defines a string for the zentrack_users table 'notes' field
- that you can use to prevent deleteing users in addition to the local admin specified in adminexempt.
- To use this go into your Zentrack instance, click admin, then go to the edit users page and enter the exact same text as 'otherexempt'
- in the NOTES field for any users AFTER they are populated into the DB.
- You could also modify the code below to use this field to prevent updates as well.
- If you don't want to keep any non-AD users just set these to a empty strings.
- */
- $adminexempt = "Administrator"; // will be compared against the login field of zentrack_users
- $otherexempt = "DO NOT DELETE"; // will be compared against the notes field of zentrack_users
- // Begin code to do the import/update
- echo "<HTML>\n<HEAD><TITLE>Active Directory >> Zentrack Import</TITLE></HEAD>\n<BODY>\n";
- echo "<b>Active Directory >> Zentrack Users Database Import Tool</b> <BR><BR>\n";
- // Query AD for user accounts
- echo "Connected to AD via LDAP<br>\n";
- // Required for AD/Server 2003?
- ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
- ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
- echo "Bound to AD via LDAP<br>\n";
- // Recursive Query:
- $ldapResults = ldap_search($ad, $dn, $filter, $attributes);
- // Same-Level Query:
- // Uncomment the following line and comment out the line above to search only the same OU level
- // $ldapResults = ldap_list($ad, $dn, $filter, $attributes);
- $entries = ldap_get_entries($ad, $ldapResults);
- // Match AD users accounts to MySQL user table
- if (!$db) {
- }
- // begin loop through the DB usernames and if they aren't in AD delete them
- $sql = "SELECT login,notes FROM zentrack_users";
- if (!$result)
- {
- $message .= 'Whole query: ' . $sql . "<BR>";
- }
- $deletedcount = 0;
- echo "<br>";
- {
- $otherarray[] = $row['notes'];
- }
- for ($j=0; $j<($totalrows); $j++)
- {
- $inAD = false;
- $exemptuser = false;
- if ($namearray[$j] == $adminexempt)
- {
- $exemptuser = true;
- }
- if (($otherarray[$j] == $otherexempt) && (!$exemptuser))
- {
- $exemptuser = true;
- }
- if (!$exemptuser)
- {
- for ($i=0; $i<$entries["count"]; $i++)
- {
- {
- $i=$entries["count"];
- $inAD=true;
- }
- }
- if (!($inAD))
- {
- DeleteUser( $j, $db, $namearray[$j]);
- ++$deletedcount;
- }
- }
- }
- // end loop through the DB usernames and if they aren't in AD delete them
- $usersadded=0;
- $usersupdated=0;
- echo "<BR>\n";
- for ($i=0; $i<$entries["count"]; $i++)
- {
- // look up AD user account in MySQL database
- // add to database if user does not exist in database already
- // update all user DB fields if they are already in the database so the DB matches AD
- // PLEASE NOTE: All "index" values must be in lower-case! This is a PHP array handling quirk?
- $username = $entries[$i]["samaccountname"][0];
- if (DatabaseLookup($username, $db))
- {
- // echo $i." : AD user: ".$username." found in database<br>\n";
- if (UpdateUser( $i, $db,
- $entries[$i]["givenname"][0],
- $entries[$i]["sn"][0],
- $entries[$i]["samaccountname"][0],
- $entries[$i]["mail"][0]))
- {
- ++$usersupdated;
- }
- }
- else
- {
- // echo $i." : AD user: ".$username." <b><i>not</i></b> found in database<br>\n";
- AddUser( $i, $db,
- $entries[$i]["givenname"][0],
- $entries[$i]["sn"][0],
- $entries[$i]["samaccountname"][0],
- $entries[$i]["mail"][0]);
- ++$usersadded;
- }
- }
- echo "<br>";
- echo "</BODY></HTML>\n";
- ldap_unbind($ad);
- //
- // DatabaseLookup - Check for match between AD account and Users table in MySQL
- //
- function DatabaseLookup( $cn, $db ) {
- // check for match against mysql database
- if (!$result) {
- $message .= 'Whole query: ' . $sql . "<BR>";
- }
- {
- }
- {
- }
- return ($num_rows > 0);
- }
- // AddUser - Adds record to MySQL Zentrack Users table
- function AddUser ($i, $db, $fname, $lname, $uname, $email_add) {
- $sql =
- "INSERT INTO zentrack_users (fname, lname, login, initials, access_level, email) VALUES (" .
- {
- }
- {
- }
- {
- }
- //echo $i . " : INSERT effected <b>" .mysql_affected_rows(). " rows</b><br>\n";
- }
- // UpdateUser - Updates record in MySQL Zentrack Users table
- function UpdateUser ($i, $db, $fname, $lname, $uname, $email_add) {
- $sql =
- "UPDATE zentrack_users SET
- {
- }
- {
- }
- {
- return true;
- }
- //echo $i . " : Update effected <b>" .mysql_affected_rows(). " rows</b><br>\n";
- }
- // DeleteUser - Deletes records from the MySQL Zentrack Users table
- function DeleteUser ($i, $db, $uname) {
- $sql = "DELETE FROM zentrack_users WHERE login = '" . $uname . "'";
- {
- }
- {
- }
- {
- }
- //echo $i . " : Delete effected <b>" .mysql_affected_rows(). " rows</b><br>\n";
- }
- ?>
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.