From fbb90d6adda76ff7939e86596ecb3bbd1a380ee4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Thu, 24 Oct 2019 10:28:13 +0200
Subject: [PATCH] :sparkles: feat(ldap) Add LDAP trace messages to LDAP debug
 level

This will allow to easily inspect what FD is doing at the LDAP level

issue #6042
---
 include/class_ldap.inc | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/include/class_ldap.inc b/include/class_ldap.inc
index 0af3d2ede..e8a002656 100644
--- a/include/class_ldap.inc
+++ b/include/class_ldap.inc
@@ -220,6 +220,8 @@ class LDAP
     } else {
       $this->error = 'Could not connect to LDAP server';
     }
+
+    @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'connect');
   }
 
   /*!
@@ -232,9 +234,11 @@ class LDAP
       $this->error      = "Success";
       $this->hascon     = TRUE;
       $this->reconnect  = TRUE;
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rebind');
       return 0;
     } else {
       $this->error = "Could not bind to " . $credentials['ADMINDN'];
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rebind');
       return NULL;
     }
   }
@@ -256,6 +260,7 @@ class LDAP
   {
     @ldap_unbind($this->cid);
     $this->cid = NULL;
+    @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, '', 'unbind');
   }
 
   /*!
@@ -267,6 +272,7 @@ class LDAP
       @ldap_close($this->cid);
       $this->hascon = FALSE;
     }
+    @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, '', 'disconnect');
   }
 
   /*!
@@ -353,9 +359,11 @@ class LDAP
       }
 
       $this->log("LDAP operation: time=".$diff." operation=search('".$this->basedn."', '$filter')");
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'search(base="'.$this->basedn.'",scope="'.$scope.'",filter="'.$filter.'")');
       return $this->sr[$srp];
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'search(base="'.$this->basedn.'",scope="'.$scope.'",filter="'.$filter.'")');
       return "";
     }
   }
@@ -420,9 +428,11 @@ class LDAP
       $this->error    = @ldap_error($this->cid);
       $this->resetResult($srp);
       $this->hasres[$srp] = TRUE;
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'cat(dn="'.$dn.'",filter="'.$filter.'")');
       return $this->sr[$srp];
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'cat(dn="'.$dn.'",filter="'.$filter.'")');
       return "";
     }
   }
@@ -441,9 +451,11 @@ class LDAP
         $this->connect();
       }
       $res  = @ldap_read($this->cid, $dn, $filter, ["objectClass"]);
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'object_match_filter(dn="'.$dn.'",filter="'.$filter.'")');
       return @ldap_count_entries($this->cid, $res);
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'object_match_filter(dn="'.$dn.'",filter="'.$filter.'")');
       return FALSE;
     }
   }
@@ -464,6 +476,7 @@ class LDAP
     } else {
       $this->error = "Could not connect to LDAP server";
     }
+    @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $size, 'set_size_limit');
   }
 
   /*!
@@ -494,13 +507,16 @@ class LDAP
         if (!isset($att)) {
           $att = [];
         }
+        @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'fetch()');
         return $att;
       } else {
         $this->error = "Perform a fetch with no search";
+        @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'fetch()');
         return "";
       }
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'fetch()');
       return "";
     }
   }
@@ -566,13 +582,16 @@ class LDAP
       if ($this->hasres[$srp]) {
         $rv = @ldap_count_entries($this->cid, $this->sr[$srp]);
         $this->error = @ldap_error($this->cid);
+        @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'count()');
         return $rv;
       } else {
         $this->error = "Perform a Fetch with no Search";
+        @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'count()');
         return "";
       }
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'count()');
       return "";
     }
   }
@@ -597,9 +616,11 @@ class LDAP
 
       $r = ldap_mod_del($this->cid, $dn, $attrs);
       $this->error = @ldap_error($this->cid);
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rm('.$dn.')');
       return $r;
     } else {
       $this->error = 'Could not connect to LDAP server';
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rm('.$dn.')');
       return '';
     }
   }
@@ -616,9 +637,11 @@ class LDAP
 
       $r = @ldap_mod_add($this->cid, $dn, $attrs);
       $this->error = @ldap_error($this->cid);
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'mod_add('.$dn.')');
       return $r;
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'mod_add('.$dn.')');
       return "";
     }
   }
@@ -636,9 +659,11 @@ class LDAP
       }
       $r = @ldap_delete($this->cid, $deletedn);
       $this->error = @ldap_error($this->cid);
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rmdir('.$dn.')');
       return ($r ? $r : 0);
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rmdir('.$dn.')');
       return "";
     }
   }
@@ -688,9 +713,11 @@ class LDAP
       /* Check if destination dn exists, if not the
           server may not support this operation */
       $r &= is_resource($this->dn_exists($dest));
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rename("'.$source.'","'.$dest.'")');
       return $r;
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rename("'.$source.'","'.$dest.'")');
       return FALSE;
     }
   }
@@ -732,9 +759,11 @@ class LDAP
         }
       }
       $this->error = @ldap_error($this->cid);
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rmdir_recursive("'.$deletedn.'")');
       return ($r ? $r : 0);
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rmdir_recursive("'.$deletedn.'")');
       return "";
     }
   }
@@ -784,9 +813,11 @@ class LDAP
       if (!$this->success()) {
         $this->error .= $this->makeReadableErrors($this->error, $attrs);
       }
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'modify('.$this->basedn.')');
       return ($r ? $r : 0);
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'modify('.$this->basedn.')');
       return "";
     }
   }
@@ -807,9 +838,11 @@ class LDAP
       }
       $r            = @ldap_modify_batch($this->cid, $this->basedn, $changes);
       $this->error  = @ldap_error($this->cid);
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'modify_batch('.$this->basedn.')');
       return $r;
     } else {
       $this->error = 'Could not connect to LDAP server';
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'modify_batch('.$this->basedn.')');
       return FALSE;
     }
   }
@@ -830,9 +863,11 @@ class LDAP
       if (!$this->success()) {
         $this->error .= $this->makeReadableErrors($this->error, $attrs);
       }
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'add('.$this->basedn.')');
       return ($r ? $r : 0);
     } else {
       $this->error = "Could not connect to LDAP server";
+      @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'add('.$this->basedn.')');
       return "";
     }
   }
@@ -1129,6 +1164,7 @@ class LDAP
 
   function dn_exists ($dn)
   {
+    @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, '', 'dn_exists('.$dn.')');
     return @ldap_read($this->cid, $dn, "(objectClass=*)", ["objectClass"]);
   }
 
@@ -1485,6 +1521,7 @@ class LDAP
     $res    = @ldap_read($ds, '', 'objectClass=*', ['namingContexts']);
     $attrs  = @ldap_get_entries($ds, $res);
 
+    @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $attrs[0]['namingcontexts'], 'get_naming_contexts');
     return $attrs[0]['namingcontexts'];
   }
 }
-- 
GitLab