1 /* $Id: storage_driver.h,v 1.22 2011/06/28 00:13:48 sbajic Exp $ */
2 
3 /*
4  DSPAM
5  COPYRIGHT (C) 2002-2012 DSPAM PROJECT
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Affero General Public License as
9  published by the Free Software Foundation, either version 3 of the
10  License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU Affero General Public License for more details.
16 
17  You should have received a copy of the GNU Affero General Public License
18  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 
20 */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <auto-config.h>
24 #endif
25 
26 #ifndef _STORAGE_DRIVER_H
27 #  define _STORAGE_DRIVER_H
28 
29 #include "libdspam_objects.h"
30 #include "diction.h"
31 #include "pref.h"
32 #include "config_shared.h"
33 #ifdef DAEMON
34 #include <pthread.h>
35 #endif
36 
37 /*
38  *  _ds_drv_connection: a storage resource for a server worker thread
39  *  in stateful (daemon) mode, the storage driver may create a series
40  *  of _ds_drv_connections which are then pooled into a series of worker
41  *  threads. depending on the locking paradigm (mutex or rwlock), one
42  *  connection may or may not service multiple threads simultaneously.
43  *  connections usually contain stateful resources such as connections to
44  *  a backend database or in the case of hash_drv, pointers to an mmap'd
45  *  portion of memory.
46  */
47 
48 struct _ds_drv_connection
49 {
50   void *dbh;
51 #ifdef DAEMON
52   pthread_mutex_t lock;
53   pthread_rwlock_t rwlock;
54 #endif
55 };
56 
57 /*
58  *  DRIVER_CTX: storage driver context
59  *  a single storage driver context is used to pool connection resources,
60  *  set driver behavior (e.g. locking paradigm), and most importantly connect
61  *  to the dspam context being used by the worker thread.
62  */
63 
64 typedef struct {
65   DSPAM_CTX *CTX;				/* IN */
66   int status;					/* OUT */
67   int flags;					/* IN */
68   int connection_cache;				/* IN */
69   struct _ds_drv_connection **connections;	/* OUT */
70 } DRIVER_CTX;
71 
72 /*
73  *  _ds_storage_record: dspam-facing storage structure
74  *  the _ds_storage_record structure is a common structure passed between
75  *  libdspam and the storage abstraction layer. each instance represents a
76  *  single token in a diction. once the storage driver has it, it can create
77  *  its own internal structures, but must pass this structure back and forth
78  *  to libdspam.
79  */
80 
81 struct _ds_storage_record
82 {
83   unsigned long long token;
84   long spam_hits;
85   long innocent_hits;
86   time_t last_hit;
87 };
88 
89 /*
90  *  _ds_storage_signature: dspam-facing signature structure
91  *  the _ds_storage_signature structure is a common structure passed between
92  *  libdspam and the storage abstraction layer. the signature represents
93  *  binary training data used later for reclassification of errors. once the
94  *  storage driver has it, it can create its own internal structures, but
95  *  must pass this structure back and forth to libdspam.
96  */
97 
98 struct _ds_storage_signature
99 {
100   char signature[256];
101   void *data;
102   long length;
103   time_t created_on;
104 };
105 
106 int dspam_init_driver     (DRIVER_CTX *DTX);
107 int dspam_shutdown_driver (DRIVER_CTX *DTX);
108 int _ds_init_storage      (DSPAM_CTX * CTX, void *dbh);
109 int _ds_shutdown_storage  (DSPAM_CTX * CTX);
110 void *_ds_connect         (DSPAM_CTX *CTX);
111 
112 int _ds_getall_spamrecords (DSPAM_CTX * CTX, ds_diction_t diction);
113 int _ds_setall_spamrecords (DSPAM_CTX * CTX, ds_diction_t diction);
114 int _ds_delall_spamrecords (DSPAM_CTX * CTX, ds_diction_t diction);
115 
116 int _ds_get_spamrecord(
117   DSPAM_CTX * CTX,
118   unsigned long long token,
119   struct _ds_spam_stat *stat);
120 int _ds_set_spamrecord(
121   DSPAM_CTX * CTX,
122   unsigned long long token,
123   struct _ds_spam_stat *stat);
124 int _ds_del_spamrecord(
125   DSPAM_CTX * CTX,
126   unsigned long long token);
127 
128 struct _ds_storage_record *_ds_get_nexttoken (DSPAM_CTX * CTX);
129 struct _ds_storage_signature *_ds_get_nextsignature (DSPAM_CTX * CTX);
130 char *_ds_get_nextuser (DSPAM_CTX * CTX);
131 
132 int _ds_delete_signature(
133   DSPAM_CTX * CTX,
134   const char *signature);
135 int _ds_get_signature(
136   DSPAM_CTX * CTX,
137   struct _ds_spam_signature *SIG,
138   const char *signature);
139 int _ds_set_signature(
140   DSPAM_CTX * CTX,
141   struct _ds_spam_signature *SIG,
142   const char *signature);
143 int _ds_verify_signature(
144   DSPAM_CTX * CTX,
145   const char *signature);
146 int _ds_create_signature_id(
147   DSPAM_CTX * CTX,
148   char *buf,
149   size_t len);
150 
151 /*
152  *  Storage Driver Preferences Extension
153  *  When defined, the built-in preferences functions are overridden with
154  *  functions specific to the storage driver. This allows preferences to be
155  *  alternatively stored in the storage facility instead of flat files.
156  *  The selected storage driver must support preference extensions.
157  */
158 
159 agent_pref_t _ds_pref_load(
160   config_t config,
161   const char *user,
162   const char *home, void *dbh);
163 int _ds_pref_set(
164   config_t config,
165   const char *user,
166   const char *home,
167   const char *attrib,
168   const char *value,
169    void *dbh);
170 int _ds_pref_del(
171   config_t config,
172   const char *user,
173   const char *home,
174   const char *attrib,
175   void *dbh);
176 
177 /* Driver context flags */
178 
179 #define DRF_STATEFUL	0x01
180 #define DRF_RWLOCK	0x02
181 
182 /* Driver statuses */
183 
184 #define DRS_ONLINE	0x01
185 #define DRS_OFFLINE	0x02
186 #define DRS_UNKNOWN	0xFF
187 
188 #define CONTROL_TOKEN	11624422384514212933llu
189 			/* $$CONTROL$$ */
190 
191 #endif /* _STORAGE_DRIVER_H */
192