[Mimedefang] SA 3.0: Loading preferences, etc from LDAP or MySQL

Nate Carlson natecars at real-time.com
Thu Nov 18 18:06:35 EST 2004


On Thu, 18 Nov 2004, Nate Carlson wrote:
> Since SA 3.0 now natively supports loading preferences and such from
> LDAP or MySQL, I've been taking a look to see what would be required to
> integrate this with Mimedefang.
> 
> From what I've found, it looks like it'd be fairly simple - the filter
> would need a way to pass what it thinks the username is (the filter
> portion to figure out the proper username to pass on would probably need
> to be done on a site-by-site basis), and then mimedefang.pl would need
> to run $object->loadscoreonly_ldap($username) and
> $object->signal_user_changed(username => $username, user_dir => undef)  
> before running object->check($mail).
> 
> Does the above look reasonable? Has anyone started down this path yet?
> If not, I'll see what I can do to get it working.
> 
> David, what would be the preferred way of passing the username to
> spam_assassin_check from the filter? Add an additional (optional) field
> that would either be 'ldap:username' or 'mysql:username', or something
> similar?

OK, I hacked something up that works for me, at least. Tested with LDAP
only - I'll test for MySQL Bayes support later, but probably won't ever
test having the prefs in MySQL.

The patch adds the code to the spam assassin checks in mimedefang.pl to go
out and check against the databases; basically, you just add the username
that you want to use to search on as the second option to
spam_assassin_check in your mimedefang-filter. Here's what my SA config
for this looks like:

user_scores_dsn ldap://localhost/dc=testing,dc=localdomain?spamassassin?sub?mail=__USERNAME__
user_scores_ldap_username     cn=user,dc=testing,dc=localdomain
user_scores_ldap_password     password

..and my (very simple) mimedefang-filter entry:

my($hits, $req, $names, $report) = spam_assassin_check("","ldap:$Recipients[0]");

I realize this will need to be improved.  :)  I'm keying on the 'mail='
entry in LDAP right now, which will work on my config, but may not work on
everyone else's - in some cases, it'd probably be better to go through the
work of doing the LDAP queries and such to relate the e-mail address to
the username before passing it off to the check.

Am I doing anything horribly wrong here? Any comments?

-- 
Nate Carlson <natecars at real-time.com>   | Phone : (952)943-8700
http://www.real-time.com                | Fax   : (952)943-8500
-------------- next part --------------
diff -Naur mimedefang-2.48/mimedefang.pl.in mimedefang-2.48.nate/mimedefang.pl.in
--- mimedefang-2.48/mimedefang.pl.in	2004-11-18 16:58:18.000000000 -0600
+++ mimedefang-2.48.nate/mimedefang.pl.in	2004-11-18 17:01:39.000000000 -0600
@@ -5985,7 +5985,7 @@
 # %DESCRIPTION:
 #  Scans message using SpamAssassin (http://www.spamassassin.org)
 #***********************************************************************
-sub spam_assassin_is_spam (;$) {
+sub spam_assassin_is_spam (;$$) {
 
     my($hits, $req, $tests, $report) = spam_assassin_check(@_);
     return undef if (!defined($hits));
@@ -6006,7 +6006,7 @@
 # %DESCRIPTION:
 #  Scans message using SpamAssassin (http://www.spamassassin.org)
 #***********************************************************************
-sub spam_assassin_check (;$) {
+sub spam_assassin_check (;$$) {
 
     my($status) = spam_assassin_status(@_);
     return undef if (!defined($status));
@@ -6031,7 +6031,8 @@
 # %DESCRIPTION:
 #  Scans message using SpamAssassin (http://www.spamassassin.org)
 #***********************************************************************
-sub spam_assassin_status (;$) {
+sub spam_assassin_status (;$$) {
+    my($config,$username) = @_;
 
     my $object = spam_assassin_init(@_);
     return undef unless $object;
@@ -6041,6 +6042,35 @@
 
     my $status;
     push_status_tag("Running SpamAssassin");
+
+    if ($username) {
+	md_syslog('debug', "Got a username: $username");
+    }
+
+    if ("$username" =~ /^sql:/) {
+	$username =~ s/sql://g;
+	md_syslog('debug', "SQL username: $username");
+	if ( $object->load_scoreonly_sql($username) ) {
+	    $object->signal_user_changed(
+		{
+		    username => $username,
+		    user_dir => undef
+		}
+	    );
+	}
+    } elsif ("$username" =~ /^ldap:/) {
+	$username =~ s/ldap://g;
+	md_syslog('debug', "LDAP username: $username");
+	if ( $object->load_scoreonly_ldap($username) ) {
+	    $object->signal_user_changed(
+		{
+		    username => $username,
+		    user_dir => undef
+		}
+	    );
+	}
+    }
+    
     $status = $object->check($mail);
     pop_status_tag();
     return $status;
@@ -6055,8 +6085,8 @@
 # %DESCRIPTION:
 #  Scans message using SpamAssassin (http://www.spamassassin.org)
 #***********************************************************************
-sub spam_assassin_init (;$) {
-    my($config) = @_;
+sub spam_assassin_init (;$$) {
+    my($config,$username) = @_;
     my $LOCAL_RULES_DIR = "@CONFDIR_EVAL@/spamassassin";
 
     unless ($Features{"SpamAssassin"}) {


More information about the MIMEDefang mailing list