]> git.proxmox.com Git - proxmox-spamassassin.git/blame - upstream/sql/README
update SpamAssassin to 4.0.0
[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
ae52237f 9able to update the white list of addresses (welcomelist_from, previously whitelist_from) without the need
37ef5775
SI
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,
ae52237f 15welcomelist_from(s) (previously whitelist_from), and required_score.
37ef5775
SI
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
ae52237f 78
37ef5775
SI
79Requirements
80------------
81
ae52237f
SI
82In order for SpamAssassin to work with your SQL database, you must have the
83perl DBI module installed, AS WELL AS the DBD driver/module for your
84specific database. For example, if using MySQL/MariaDB as your RDBMS, you
85must have the DBD::mysql or DBD::MariaDB module installed. For PostgreSQL
86use the DBD::Pg module. Check CPAN for the latest versions of DBI and your
87database driver/module.
37ef5775
SI
88
89
90Database Schema
91---------------
92
93The database must contain a table, default name "userpref", with at
94least three fields:
95
96 username varchar(100) # this is the username whose e-mail is being filtered
ae52237f 97 preference varchar(50) # the preference (welcomelist_from (previously whitelist_from), required_score, etc.)
37ef5775
SI
98 value varchar(100) # the value of the named preference
99
100You can add as many other fields you wish as long as the above three fields are
101contained in the table.
102
103Note that you can either use just the mail recipient's username for the
104"username" field, in which case a varchar(8) should suffice. Alternatively,
105you can use the entire recipient's email address, e.g. "user@example.com", and
106use the full varchar(100).
107
108Included is a default table that can be safely used in your own setup. To use
109the default table, you must first create a database, and a username/password
110that can access that database.
111
112If you wish to use a table that differs from the included default you
113should review the user_scores_sql_custom_query config option for
114information on making it work correctly.
115
116To create a database, if one does not already exist, see "Creating A Database"
117below.
118
119To install the table to a mysql database, use the following command:
120
121mysql -h <hostname> -u <adminusername> -p <databasename> < userpref_mysql.sql
122Enter password: <adminpassword>
123
124This will create the following table:
125
126CREATE TABLE userpref (
127 username varchar(100) default NOT NULL,
128 preference varchar(50) default NOT NULL,
129 value varchar(100) default NOT NULL,
130 prefid int(11) NOT NULL auto_increment,
131 PRIMARY KEY (prefid),
132 INDEX (username)
133) TYPE=MyISAM;
134
135For PostgreSQL, use the following command:
136
137psql -U <username> -f userpref_pg.sql <databasename>
138
139This will create a table similar to above.
140
141Once you have created the database and added the table, just add the required
142lines to your global configuration file (local.cf). Note that you must be
143running spamc/spamd in order for this to work, and the current username must
144be passed to spamd. This can be done from spamc using the following
145.procmailrc recipe:
146
147 :0fw
148 | /usr/local/bin/spamc -f
149
150(watch out; spamc could be installed as /usr/bin/spamc instead.)
151If you are using this from /etc/procmailrc, you must include DROPPRIVS=yes
152before spamc. An example /etc/procmailrc:
153
154 DROPPRIVS=yes
155
156 :0fw
157 | /usr/local/bin/spamc -f
158
159Also note that spamd may need the "-q" switch so it knows to look up users in
160the SQL table instead of /etc/passwd. See "man spamd".
161
162
163Creating A Database
164-------------------
165
166Here's the command to create a MySQL database, and user/password pair to access
167it:
168
169mysql -h <hostname> -u <adminusername> -p
170Enter password: <adminpassword>
171mysql> use mysql;
172mysql> insert into user (Host, User, Password) values('localhost','<username>', password('<password>'));
173mysql> insert into db (Host, Db, User, Select_priv) values('localhost','<databasename>','<username>','Y');
174mysql> create database <databasename>;
175mysql> quit
176
177NOTE: If you intend to use this database for Bayes and/or AWL data you
178may need to grant additional privs (ie Insert_priv, Update_priv and
179Delete_priv). Please refer to the MySQL documentation for the proper
180method of adding these privs.
181
182To create the database for PostgreSQL, with a username/password:
183
184psql -U <adminuser> template1
185template1=# CREATE USER <username> PASSWORD '<password>';
186template1=# CREATE DATABASE <databasename> OWNER = <username>;
187
188
189Testing SpamAssassin/SQL
190------------------------
191
192To test your SQL setup, and debug any possible problems, you should start
193spamd with the -D option, which will keep spamd in the foreground, and will
194output debug message to the terminal. You should then test spamd with a
195message by calling spamc. You can use the sample-spam.txt file with the
196following command:
197
198 cat sample-spam.txt | spamc
199
200Watch the debug output from spamd and look for the following debug line:
201
202 retrieving prefs for <username> from SQL server
203
204If you do not see the above text, then the SQL query was not successful, and
205you should see any error messages reported. <username> should be the user
206that was passed to spamd and is usually the user executing spamc.
207
208Note that under the default configuration any prefs stored under the
209username '@GLOBAL' are used as defaults for all users.
210
211This code has only been tested using MySQL as the RDMS, but it has been written
212with the utmost simplicity using DBI, and any database driver that conforms to
213the DBI interface should work without problems.
214
215Web Interfaces
216--------------
217
218Several web interfaces have been created for per user configurations.
219You can find more information about these on the SpamAssassin wiki:
220http://wiki.apache.org/spamassassin/WebUserInterfaces