Posted by lindsaym on Wed 2nd Dec 12:33 (modification of post by mlindsay view diff)
diff | download | new post
- <?php
- /*******************************************************************************\
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- *
- * Date: $Date: 12/02/2009$
- * Revision: $Rev: 6$
- * Last Edited by: $Author: Mike Lindsay (mike.lindsay@cbc.ca)$
- * ID: $Id:$
- * FileName: LDAP.class.php
- *
- *
- \*******************************************************************************/
- /* required functions
- * ldap_auth_user($username, $pass) returns $userDN
- * ldap_verify_group($userDN, $this->ldap_requiredGroup_dn, $this->ldap_groupMemberSearch) returns TRUE or FALSE
- * ldap_usertable_sync($that, $userDN, $passphrase) returns assoc array $ldapRtnArray
- * ldap_usertable_cleanup($that) removes or marks inactive users from the ZT db if not an ldap/ad group member
- */
- ##INTERNAL VARS
- ##GENERAL LDAP INTERNAL FUNCTIONS
- function ldapConnect($server, $port, $tls){
- $GLOBALS["ldapconn"] = ldap_connect($server, $port)
- //SETUP ANY LDAP OPTIONS
- ldap_set_option($GLOBALS["ldapconn"], LDAP_OPT_REFERRALS, 0);
- if (!ldap_set_option($GLOBALS["ldapconn"], LDAP_OPT_PROTOCOL_VERSION, 3)) {
- }
- if ($GLOBALS["ldapconn"] && $tls) {
- if (!ldap_start_tls($GLOBALS["ldapconn"])) {
- }
- }
- }
- function ldapClose(){
- ldap_unbind($GLOBALS["ldapconn"]);
- }
- function ldapError($errorText){
- $str = "<b><font color=\"#ff0000\">" . $errorText ;
- $str .= "<br>(LDAP ERR: " . ldap_error($GLOBALS["ldapconn"]) . " : " . ldap_errno($GLOBALS["ldapconn"]);
- $str .= ")</font></b>";
- return($str);
- }
- ##EXTERNALLY CALLED FUNCTIONS
- //AUTH THE USER AGAINST LDAP TO CHECK FOR VALID ACCOUNT
- function ldap_auth_user($userDN, $pass, $failover){
- $ldapbind = ldap_bind($GLOBALS["ldapconn"], $userDN, $pass);
- // verify binding
- if ($ldapbind) {
- return 1;
- } else {
- if(ldap_errno($GLOBALS["ldapconn"]) == "81"){
- if($failover){
- //Get the error number
- ldapError("falling over to internal authentication");
- //HAND OVER AUTH TO ZT DB
- return(999); //
- }else{
- }
- }else{
- return 0;
- }
- }
- }
- //VERIFY THAT THE AUTH'ED USER IS A MEMBER OF THE REQUIRED GROUP
- function ldap_verify_group($userDN, $baseDN, $requiredGroup_dn, $groupMemberSearch){
- $sr=ldap_search($GLOBALS["ldapconn"], $requiredGroup_dn, "(&(objectclass=*))", $justThese);
- $info = ldap_get_entries($GLOBALS["ldapconn"], $sr); //retuns a nested array
- /*
- * Array (
- * [count] => 1
- * [0] => Array (
- * [$groupMemberSearch] => Array (
- * [count] => 7
- * [0] => uid=browmb,ou=People,dc=example,dc=com
- * [1] => uid=yellowf,ou=People,dc=example,dc=com
- * [2] => uid=bluel,ou=People,dc=example,dc=com
- * [3] => uid=greym,ou=People,dc=example,dc=com
- * [4] => uid=purplet,ou=People,dc=example,dc=com
- * [5] => uid=greenp,ou=People,dc=example,dc=com
- * [6] => uid=zenadmin,ou=People,dc=example,dc=com
- * )
- * [0] => $groupMemberSearch
- * [count] => 1
- * [dn] => cn=Zentrack,ou=Groups,dc=example,dc=com
- * )
- * )
- */
- foreach($info[0]["$groupMemberSearch"] as $value){
- return TRUE; //User is part of the group
- }
- }
- return FALSE; //If we hit this then the User is not part of the group, or something broke
- }
- function ldap_usertable_sync($that, $userDN, $passphrase){
- //GET THE USER INFO FROM LDAP THAT IS NEEDED FOR ZT
- foreach ($that->ldapToZTuserMapping as $value){
- $objectFilter[] .= $value;
- }
- $sr=ldap_search($GLOBALS["ldapconn"], $userDN, "(&(objectclass=*))", $objectFilter);
- $ldapUserInfo = ldap_get_entries($GLOBALS["ldapconn"], $sr); //returns a nested array
- //FLATTEN THE RETURNED ARRAY, AND INSERT NULL VALUES IF LDAP IS MISSING OBJECTS
- $i = 0;
- while($i < $ldapUserInfo[0]["count"]){
- $x = $ldapUserInfo[0][$i];
- $ldapUserFields[$x] = $ldapUserInfo[0][$x][0];
- $i++;
- }
- foreach($that->ldapToZTuserMapping as $value){
- if(!isset($ldapUserFields["$value"])){ //If the LDAP/AD user is missing fields, set them to a default string
- $ldapUserFields["$value"] = "Missing LDAP OBJ";
- }
- }
- //creates the following array
- /*
- * Array
- * (
- * [uid] => bofh
- * [sn] => Master
- * [givenname] => The
- * [initials] => TM
- * [mail] => bofh@example.ca
- * [description] => man Sysadmin $> Bribe with beer frequently
- */
- //GET THE ZT DB FIELDS FROM THE MAP THAT WE WANT TO WORK WITH
- $ztUserDBfields = "`user_id`,";
- foreach ($that->ldapToZTuserMapping as $key=>$value){
- $ztUserDBfields .= "`" .$key . "`,";
- }
- //LOOK TO SEE IF THE LDAP USER IS IN THE ZT DB
- $query = "SELECT $ztUserDBfields from `" . $that->table_users
- ."` where `login`=\"" . $ldapUserFields[$that->ldap_user_dn_prefix] . "\"";
- $ztUserDB = $that->db_quickIndexed( $query );
- $insert = "";
- foreach($that->ldapToZTuserMapping as $dbk=>$ldapk){ //Create the insert statment in the corect order to match the map
- $insert .= $that->checkSlashes($ldapUserFields[$ldapk]) . ",";
- }
- $query = "INSERT INTO `". $that->table_users . "` ($ztUserDBfields,`access_level`,`homebin`,`passphrase`,`active`) VALUES " .
- "(NULL,$insert, '$that->ldap_default_accessLevel','$that->ldap_default_homeBin', '" . md5($passphrase) ."', '1')";
- $insertionResult = $that->db_result($query);
- if(!$insertionResult){
- echo "<font color=\"ff0000\"><b>User record insertion into ZenTrack Database failed" .
- ", please contact your Systems Administrator</font></b><br>";
- //echo "$that->dbErrMsg <br>";
- return(0);
- }
- return(1);
- }else{ //CHECK TO SEE IF THE ZT DB IS DIFFERENT FROM THE LDAP INFO
- $recordUpdate = "`passphrase`='" . md5($passphrase) . "',"; //Always re-sync the password in the ZT DB to LDAP/AD
- foreach ($that->ldapToZTuserMapping as $dbk=>$ldapk){
- if($ldapUserFields["$ldapk"] != $ztUserDB["$dbk"]){
- $recordUpdate .= "`" . $dbk . "`=" . $that->checkSlashes($ldapUserFields["$ldapk"]) . ",";
- }
- }
- //NOW BUILD THE UPDATE QUERY
- $query = "UPDATE `". $that->table_users . "` SET " .
- $recordUpdate . " WHERE " .
- "`user_id`=" . $ztUserDB["user_id"] . " LIMIT 1";
- $insertionResult = $that->db_result($query);
- if(!$insertionResult){
- echo "<font color=\"ff0000\"><b>User record sync failed" .
- ", please contact your Systems Administrator</font></b><br>";
- //echo "$that->dbErrMsg <br>";
- return(0);
- }else{
- return(1);
- }
- }
- }
- //CLEAN UP THE ZT USER DB, IF A USER IS REMOVED FROM THE ZT LDAP/AD GROUP THEN
- // EITHER DELETE OR MARK INACTIVE THE USER IN THE ZT USER TABLE
- function ldap_usertable_cleanup($that){
- //GET THE LDAP GROUP MEMBERS LIST
- $sr=ldap_search($GLOBALS["ldapconn"], $that->ldap_requiredGroup_dn, "(&(objectclass=*))", $justThese);
- $ldapGroupMembers = ldap_get_entries($GLOBALS["ldapconn"], $sr); //retuns a nested array
- /*
- * Array (
- * [count] => 1
- * [0] => Array (
- * [$groupMemberSearch] => Array (
- * [count] => 7
- * [0] => uid=browmb,ou=People,dc=example,dc=com
- * [1] => uid=yellowf,ou=People,dc=example,dc=com
- * [2] => uid=bluel,ou=People,dc=example,dc=com
- * [3] => uid=greym,ou=People,dc=example,dc=com
- * [4] => uid=purplet,ou=People,dc=example,dc=com
- * [5] => uid=greenp,ou=People,dc=example,dc=com
- * [6] => uid=zenadmin,ou=People,dc=example,dc=com
- * )
- * [0] => $groupMemberSearch
- * [count] => 1
- * [dn] => cn=Zentrack,ou=Groups,dc=example,dc=com
- * )
- * )
- */
- //GET THE user_id AND login INFO FOR ALL USERS FROM ZT DB
- $protectedUsers = "(";
- foreach($that->ldap_to_ZT_protected_users as $value){ //exclude protected users
- $protectedUsers .= "`login` NOT LIKE \"" . $value . "\" AND ";
- }
- $protectedUsers .= ")";
- $query = "SELECT `user_id`,`login` from `" . $that->table_users . "`";
- if($that->ldap_delete_orphined_users){ //if false then we are only marking users inactive
- $query .= " WHERE " . $protectedUsers;
- }else{
- $query .= " WHERE `active` > 0 AND " . $protectedUsers;
- }
- $ztUserDB = $that->db_queryIndexed( $query );
- $i = 0;
- $pattern = "/" . $ztUserDB[$i]["login"] . "/";
- foreach($ldapGroupMembers[0]["$that->ldap_groupMemberSearch"] as $value){
- $tmp[$ztUserDB[$i]["user_id"]] = $ztUserDB[$i]["login"];
- }else{
- break;
- }
- }
- $i++;
- }
- if($that->ldap_delete_orphined_users){ //if $that->ldap_delete_orphined_users is true then delete the ZT DB account
- foreach($tmp as $key=>$value){
- $that->db_delete($that->table_users, "user_id", $key);
- }
- }else{ //Mark the user in ZT DB inactive
- foreach($tmp as $key=>$value){
- $query = "UPDATE `". $that->table_users . "` SET " .
- "`active`='0' WHERE " .
- "`user_id`=" . $key . " AND `login`=\"" . $value . "\" LIMIT 1";
- if(!$that->db_result($query)){
- echo "<font color=\"bbbb00\"><b>The house keeping job has failed" .
- ", this is a non-critical error. Please notify your Systems Administrator</font></b><br>";
- }
- }
- }
- }
- return(1);
- }
- ?>
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.