Using SpamAssassin With An SQL Database --------------------------------------- SpamAssassin can now load users' score files from an SQL database. The concept here is to have a web application (PHP/perl/ASP/etc.) that will allow users to be able to update their local preferences on how SpamAssassin will filter their e-mail. The most common use for a system like this would be for users to be able to update the white list of addresses (whitelist_from) without the need for them to update their $HOME/.spamassassin/user_prefs file. It is also quite common for users listed in /etc/passwd to not have a home directory, therefore, the only way to have their own local settings would be through an RDBMS system. SpamAssassin will check the global configuration file (ie. any file matching /etc/mail/spamassassin/*.cf) for the following settings: user_scores_dsn DBI:driver:database:hostname[:port] user_scores_sql_username dbusername user_scores_sql_password dbpassword The first option, user_scores_dsn, describes the data source name that will be used to create the connection to your SQL server. It MUST be in the format as listed above. should be the DBD driver that you have installed to access your database. must be the name of the database that you created to store the user preference table. is the name of the host that contains the SQL database server. is the optional port number where your database server is listening. user_scores_dsn DBI:mysql:spamassassin:localhost Would tell SpamAssassin to connect to the database named spamassassin using MySQL on the local server, and since is omitted, the driver will use the default port number. The other two required options tells SpamAssassin to use the defined username and password to establish the connection. If the user_scores_dsn option does not exist, SpamAssassin will not attempt to use an SQL for retrieving users' preferences. Note that this will NOT look for test rules, only local scores, whitelist_from(s), required_hits, and auto_report_threshold. Requirements ------------ In order for SpamAssassin to work with your SQL database, you must have the perl DBI module installed, AS WELL AS the DBD driver/module for your specific database. For example, if using MySQL as your RDBMS, you must have the Msql-Mysql module installed. Check CPAN for the latest versions of DBI and your database driver/module. We are currently using: DBI-1.20 Msql-Mysql-modules-1.2219 perl v5.6.1 But older versions should work fine as the SQL code in SpamAssassin is as simple as could be. Database Schema --------------- The database must contain a table named "userpref" with at least three fields: username varchar(8) # this is the username whose e-mail is being filtered preference varchar(30) # the preference (whitelist_from, required_hits, etc.) value varchar(100) # the value of the named preference You can add as many other fields you wish as long as the above three fields are contained in the table. Included is a default table that can be safely used in your own setup. To use the default table, you must first create a database, and a username/password that can access that database. To install the table, use the following command: mysql -h -u -p databasename < spamassasin.sql This will create the following table: CREATE TABLE userpref ( username varchar(8) default NOT NULL, preference varchar(30) default NOT NULL, value varchar(100) default NOT NULL, prefid int(11) NOT NULL auto_increment, PRIMARY KEY (prefid), INDEX (username) ) TYPE=MyISAM; Once you have created the database and added the table, just add the required lines to your global spamassassin.cf file. Note that you must be running spamc/spamd in order for this to work, and the current username must be passed to spamd. This can be done from spamc using the following .procmailrc recipe: :0wf | /usr/local/bin/spamc -f (watch out; spamc could be installed as /usr/bin/spamc instead.) If you are using this from /etc/procmailrc, you must include DROPPRIVS=yes before spamc. An example /etc/procmailrc: DROPPRIVS=yes :0fw | /usr/local/bin/spamc -f Testing SpamAssassin/SQL ------------------------ To test your SQL setup, and debug any possible problems, you should start spamd with the -D option, which will keep spamd in the foreground, and will output debug message to the terminal. You should then test spamd with a message by calling spamc. You can use the sample-spam.txt file with the following command: cat sample-spam.txt | spamc Watch the debug output from spamd and look for the following debug line: retrieving prefs for from SQL server If you do not see the above text, then the SQL query was not successful, and you should see any error messages reported. should be the user that was passed to spamd and is usually the user executing spamc. This has only been tested using MySQL as the RDMS, but it has been written with the utmost simplicity using DBI, and any database driver that conforms to the DBI interface should work without problems. ****** NB: This should be considered BETA, and the interface, schema, or overall operation of SQL support may change at any time with future releases of SA. ****** PHP Interface ------------- This can be downloaded from http://spamassassin.org/devel/ , called "php-sa-mysql-n.n" where n.n is the version number. Please send any comments to jengland /at/ enetis.net . Justin England