]> git.proxmox.com Git - proxmox-spamassassin.git/blame - upstream/sql/README
buildsys: drop upstream tarball and add extracted sources
[proxmox-spamassassin.git] / upstream / sql / README
CommitLineData
37ef5775
SI
1
2Loading SpamAssassin User Preferences From An SQL Database
3----------------------------------------------------------
4
5SpamAssassin can now load users' score files from an SQL database. The concept
6here is to have a web application (PHP/perl/ASP/etc.) that will allow users to
7be able to update their local preferences on how SpamAssassin will filter their
8e-mail. The most common use for a system like this would be for users to be
9able to update the white list of addresses (whitelist_from) without the need
10for them to update their $HOME/.spamassassin/user_prefs file. It is also quite
11common for users listed in /etc/passwd to not have a home directory, therefore,
12the only way to have their own local settings would be through an RDBMS system.
13
14Note that this will NOT look for test rules, only local scores,
15whitelist_from(s), and required_score.
16
17In addition, any config options marked as Admin Only will NOT be parsed from
18SQL preferences.
19
20SpamAssassin will check the global configuration file (ie. any file matching
21/etc/mail/spamassassin/*.cf) for the following settings:
22
23 user_scores_dsn DBI:driver:connection
24 user_scores_sql_username dbusername
25 user_scores_sql_password dbpassword
26
27The first option, user_scores_dsn, describes the data source name that will be
28used to create the connection to your SQL server. It MUST be in the format
29as listed above. <driver> should be the DBD driver that you have installed
30to access your database. <connection> can differ depending on which
31database you are using.
32
33For MySQL, connection should take the format
34
35 database:hostname[:port]
36
37<database> must be the name of the database that you created to store the user
38preference table. <hostname> is the name of the host that contains the SQL
39database server. <port> is the optional port number where your database server
40is listening.
41
42 user_scores_dsn DBI:mysql:spamassassin:localhost
43
44Would tell SpamAssassin to connect to the database named spamassassin using
45MySQL on the local server, and since <port> is omitted, the driver will use the
46default port number.
47
48For PostgreSQL, connection should take the following format:
49
50 dbname=database;[host=hostname;[port=port;]
51
52 user_scores_dsn DBI:Pg:dbname=spamassassin;host=localhost
53
54would do the same as the previous example.
55
56For additional information, please refer to the DBD::* documentation
57for your particular driver.
58
59The spamd server will not pay attention to SQL preferences by default,
60even with user_scores_dsn set in the config files. You must startup
61spamd with the proper options (ie -q or -Q, see perldoc spamd for more
62information). If the user_scores_dsn option does not exist,
63SpamAssassin will not attempt to use SQL for retrieving users'
64preferences.
65
66While scanning a message if spamd is unable to connect to the server
67specified in user_scores_dsn or an error occurs when querying the SQL
68server then spam checking will not be performed on that message.
69
70The user_scores_sql_username and user_scores_sql_password options are
71required if your database server requires a username and password to
72be sent on connect.
73
74If you have a table layout that differs from the default, please
75review the documentation for user_scores_sql_custom_query for
76information on how deal with a custom layout.
77
78Requirements
79------------
80
81In order for SpamAssassin to work with your SQL database, you must have
82the perl DBI module installed, AS WELL AS the DBD driver/module for your
83specific database. For example, if using MySQL as your RDBMS, you must have
84the Msql-Mysql module installed. Check CPAN for the latest versions of DBI
85and your database driver/module.
86
87We are currently using:
88
89 DBI-1.20
90 Msql-Mysql-modules-1.2219
91 perl v5.6.1
92
93But older and newer versions should work fine as the SQL code in SpamAssassin
94is as simple as could be.
95
96
97Database Schema
98---------------
99
100The database must contain a table, default name "userpref", with at
101least three fields:
102
103 username varchar(100) # this is the username whose e-mail is being filtered
104 preference varchar(50) # the preference (whitelist_from, required_score, etc.)
105 value varchar(100) # the value of the named preference
106
107You can add as many other fields you wish as long as the above three fields are
108contained in the table.
109
110Note that you can either use just the mail recipient's username for the
111"username" field, in which case a varchar(8) should suffice. Alternatively,
112you can use the entire recipient's email address, e.g. "user@example.com", and
113use the full varchar(100).
114
115Included is a default table that can be safely used in your own setup. To use
116the default table, you must first create a database, and a username/password
117that can access that database.
118
119If you wish to use a table that differs from the included default you
120should review the user_scores_sql_custom_query config option for
121information on making it work correctly.
122
123To create a database, if one does not already exist, see "Creating A Database"
124below.
125
126To install the table to a mysql database, use the following command:
127
128mysql -h <hostname> -u <adminusername> -p <databasename> < userpref_mysql.sql
129Enter password: <adminpassword>
130
131This will create the following table:
132
133CREATE TABLE userpref (
134 username varchar(100) default NOT NULL,
135 preference varchar(50) default NOT NULL,
136 value varchar(100) default NOT NULL,
137 prefid int(11) NOT NULL auto_increment,
138 PRIMARY KEY (prefid),
139 INDEX (username)
140) TYPE=MyISAM;
141
142For PostgreSQL, use the following command:
143
144psql -U <username> -f userpref_pg.sql <databasename>
145
146This will create a table similar to above.
147
148Once you have created the database and added the table, just add the required
149lines to your global configuration file (local.cf). Note that you must be
150running spamc/spamd in order for this to work, and the current username must
151be passed to spamd. This can be done from spamc using the following
152.procmailrc recipe:
153
154 :0fw
155 | /usr/local/bin/spamc -f
156
157(watch out; spamc could be installed as /usr/bin/spamc instead.)
158If you are using this from /etc/procmailrc, you must include DROPPRIVS=yes
159before spamc. An example /etc/procmailrc:
160
161 DROPPRIVS=yes
162
163 :0fw
164 | /usr/local/bin/spamc -f
165
166Also note that spamd may need the "-q" switch so it knows to look up users in
167the SQL table instead of /etc/passwd. See "man spamd".
168
169
170Creating A Database
171-------------------
172
173Here's the command to create a MySQL database, and user/password pair to access
174it:
175
176mysql -h <hostname> -u <adminusername> -p
177Enter password: <adminpassword>
178mysql> use mysql;
179mysql> insert into user (Host, User, Password) values('localhost','<username>', password('<password>'));
180mysql> insert into db (Host, Db, User, Select_priv) values('localhost','<databasename>','<username>','Y');
181mysql> create database <databasename>;
182mysql> quit
183
184NOTE: If you intend to use this database for Bayes and/or AWL data you
185may need to grant additional privs (ie Insert_priv, Update_priv and
186Delete_priv). Please refer to the MySQL documentation for the proper
187method of adding these privs.
188
189To create the database for PostgreSQL, with a username/password:
190
191psql -U <adminuser> template1
192template1=# CREATE USER <username> PASSWORD '<password>';
193template1=# CREATE DATABASE <databasename> OWNER = <username>;
194
195
196Testing SpamAssassin/SQL
197------------------------
198
199To test your SQL setup, and debug any possible problems, you should start
200spamd with the -D option, which will keep spamd in the foreground, and will
201output debug message to the terminal. You should then test spamd with a
202message by calling spamc. You can use the sample-spam.txt file with the
203following command:
204
205 cat sample-spam.txt | spamc
206
207Watch the debug output from spamd and look for the following debug line:
208
209 retrieving prefs for <username> from SQL server
210
211If you do not see the above text, then the SQL query was not successful, and
212you should see any error messages reported. <username> should be the user
213that was passed to spamd and is usually the user executing spamc.
214
215Note that under the default configuration any prefs stored under the
216username '@GLOBAL' are used as defaults for all users.
217
218This code has only been tested using MySQL as the RDMS, but it has been written
219with the utmost simplicity using DBI, and any database driver that conforms to
220the DBI interface should work without problems.
221
222Web Interfaces
223--------------
224
225Several web interfaces have been created for per user configurations.
226You can find more information about these on the SpamAssassin wiki:
227http://wiki.apache.org/spamassassin/WebUserInterfaces