Updated spamd_ldap patch

Neil Sequeira <[email protected]> Wed, 1 Oct 2003 11:41:18 -0400
Newsgroups gmane.comp.horde.sam
Organization NCS Consulting Inc.
Message-ID <[email protected]>
Here's an update to the spamd_ldap module for sam. This is a patch 
against the CVS version of sam as of the end of last week.  

Max set me straight on the capabilities setup, so i've moved that back 
into the library file.  I also added in some basic error logging - it 
still hides errors from the user but now a message with the ldap 
error gets logged to the horde log.

Some other minor fixes are included - mainly just fixing spelling 
errors and the comments.


	-neil


-- 
sam mailing list
Frequently Asked Questions: http://horde.org/faq/
To unsubscribe, mail: [email protected]
sam.ldap.patch (text/x-diff, 6.2 KB)
diff -ru -N sam.old/config/backends.php.dist sam/config/backends.php.dist
--- sam.old/config/backends.php.dist	2003-09-19 12:16:45.000000000 -0400
+++ sam/config/backends.php.dist	2003-09-29 11:49:48.000000000 -0400
@@ -90,19 +90,6 @@
 	    'defang_mime' => 1,
 	    'skip_rbl' => 1,
 	),
-	'capabilities' => array(
-		'hit_level',
-		'report_header',
-		'rewrite_sub',
-		'subject_tag',
-		'level_stars',
-		'defang_mime',
-		'whitelist_to',
-		'whitelist_from',
-		'blacklist_to',
-		'blacklist_from',
-		'skip_rbl'
-	),
     ),
 );
 
diff -ru -N sam.old/lib/Driver/spamd_ldap.php sam/lib/Driver/spamd_ldap.php
--- sam.old/lib/Driver/spamd_ldap.php	2003-09-19 12:16:46.000000000 -0400
+++ sam/lib/Driver/spamd_ldap.php	2003-09-29 11:50:00.000000000 -0400
@@ -7,9 +7,9 @@
  * Required parameters:
  * ====================
  *   'ldapserver'   --  The hostname of the ldap server.
- *   'basedn'       --  The password associated with 'username'.
- *   'attribute'    --  The database type (ie. 'pgsql', 'mysql, etc.).
- *   'uid'          --  The communication protocol ('tcp', 'unix', etc.).
+ *   'basedn'       --  The basedn for user entries.
+ *   'attribute'    --  The spamAssassin attribute to use.
+ *   'uid'          --  The uid attribute for building userDNs.
  *
  * $Horde: sam/lib/Driver/spamd_ldap.php,v 1.1 2003/09/19 16:16:46 jan Exp $
  *
@@ -44,12 +44,9 @@
 
     /**
      * List of the capabilities supported by this driver.
-     * This has been moved into the backend configuration
-     * to make disabling capabilities easier.
      *
      * @var array $_capabilities
      */
-/*
     var $_capabilities = array('hit_level',
                                'report_header',
                                'rewrite_sub',
@@ -61,7 +58,6 @@
                                'blacklist_to',
                                'blacklist_from',
                                'skip_rbl');
-*/
     /**
      * Constructs a new LDAP storage object.
      *
@@ -75,7 +71,6 @@
     {
         $this->_user = $user;
         $this->_params = $params;
-        $this->_capabilities = $params['capabilities'];
     }
 
     /**
@@ -118,12 +113,17 @@
     /**
      * Retrieve an option set from the storage backend.
      *
-     * @access private
+     * @access public
+     *
+     * @return boolean     true on success, false on failure
+     *
      */
     function retrieve()
     {
-        /* Make sure we have a valid database connection. */
-        $this->_connect();
+        /* Make sure we have a valid ldap connection. */
+        if (!$this->_connect()) { 
+            return false; 
+        }
 
         /* set default values */
         $this->_setDefaults();
@@ -131,10 +131,14 @@
         $user = $this->_user;
         $attrib = strtolower($this->_params['attribute']);
         $filter = "(".$this->_params['uid']."=$user)";
-        $res = ldap_search($this->_linkid, $this->_params['basedn'], 
+        $res = @ldap_search($this->_linkid, $this->_params['basedn'], 
                                                 $filter, array($attrib));
         if ($res) {
-            $eres = ldap_get_entries($this->_linkid, $res);
+            $eres = @ldap_get_entries($this->_linkid, $res);
+	}
+	if (!$res || !$eres) {
+            $this->_handleError("ldap search failed: ".$this->_lastErrstr()); 
+            return false;
 	}
         if ($eres && isset($eres[0][$attrib])) {
             for ($i = 0; $i < $eres[0][$attrib]['count']; $i++) {
@@ -163,12 +167,14 @@
                 }
             }
         }
+        return true;
     }
 
     /**
      * Set default values
      *
      * @access private
+     *
      */
     function _setDefaults()
     {
@@ -181,13 +187,13 @@
     /**
      * Store an option set in the storage backend.
      *
-     * @access private
+     * @access public
      *
      * @return boolean    True on success or false on failure.
      */
     function store()
     {
-        /* Make sure we have a valid database connection. */
+        /* Make sure we have a valid ldap connection. */
         if (!$this->_connect()) { 
             return false; 
         }
@@ -205,7 +211,11 @@
             }
         }
 
-        return (ldap_modify($this->_linkid, $userdn, $entry));
+        $lret=@ldap_modify($this->_linkid, $userdn, $entry);
+        if (!$lret) {
+            $this->_handleError("ldap modify failed: ". $this->_lastErrstr());
+        }
+        return $lret;
     }
     
     /**
@@ -229,34 +239,66 @@
             'SAM backend', 'backends.php', '$backends');
 
         for ($tries = 3; $tries > 0; $tries--) {   /* try three times */
-            $lc = ldap_connect($this->_params['ldapserver']);
+            $lc = @ldap_connect($this->_params['ldapserver']);
             if ($lc) {
-                $lb = ldap_bind($lc, $binddn, $bindpass);
+                $lb = @ldap_bind($lc, $binddn, $bindpass);
                 if ($lb) {
                     $this->_linkid = $lc;
                     $this->connected = true;
                     return true;
                 } else  {
-                    ldap_unbind($lc);
+                    $em=ldap_error($lc);
+                    @ldap_unbind($lc);
                 }
+            } else {
+                $em="ldap_connect failed.";
             }
         }
+        /* if we're here, connection or bind failed */
+        $this->_handleError("ldap error: ".$em);
         return false;
     }
 
     /**
      * Disconnect from the LDAP server and clean up the connection.
      *
+     * @access private
+     *
      * @return boolean  True on success, false on failure.
      */
     function _disconnect()
     {
         if ($this->_connected) {
             $this->_connected = false;
-            return ldap_unbind($this->_linkid);
+            return @ldap_unbind($this->_linkid);
         }
 
         return true;
     }
 
+    /**
+     * dump the last error on current link as a string
+     *
+     * @access private
+     *
+     * @return string
+     *
+     */
+    function _lastErrstr()
+    {
+        return ldap_error($this->_linkid);
+    }
+
+    /**
+     * Handle an error.
+     * For now just log an error message.
+     *
+     * @access private
+     *
+     */
+   function _handleError($msg)
+   {
+       Horde::logMessage($msg, __FILE__, __LINE__, PEAR_LOG_ERR);
+   }
+
 }