1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* Dynamic loading of the sort backend.
* We use GLib's multiplatform dl() wrapper
* to open up libsortsieve and
* populate the global 'sort' structure.
*
* (c) 2005 Aaron Stone <aaron@serendipity.cx>
*/
#include <gmodule.h>
#include "config.h"
#include "dbmailtypes.h"
#include "debug.h"
#include "sort.h"
#include "sortmodule.h"
static sort_func_t *sort = NULL;
extern db_param_t _db_params;
/* Returns:
* 1 on modules unsupported
* 0 on success
* -1 on failure to load module
* -2 on missing symbols
* -3 on memory error
*/
int sort_load_driver(void)
{
GModule *module;
char *lib = NULL;
char *driver = NULL;
if (!g_module_supported()) {
trace(TRACE_FATAL, "%s,%s: loadable modules unsupported on this platform",
__FILE__, __func__);
return 1;
}
sort = g_new0(sort_func_t,1);
if (!sort) {
trace(TRACE_FATAL, "%s,%s: cannot allocate memory",
__FILE__, __func__);
return -3;
}
/* The only supported driver is Sieve. */
driver = "sort_sieve";
/* Try local build area, then dbmail lib paths, then system lib path. */
int i;
char *lib_path[] = {
"modules/.libs",
"/usr/lib/dbmail",
"/usr/local/lib/dbmail",
NULL };
for (i = 0; i < 4; i++) {
lib = g_module_build_path(lib_path[i], driver);
module = g_module_open(lib, 0); // non-lazy bind.
if (module)
break;
}
/* If the list is exhausted without opening a module, we'll catch it,
* but we don't bomb out as we do for db and auth; just deliver normally. */
if (!module) {
trace(TRACE_ERROR, "%s,%s: cannot load %s: %s",
__FILE__, __func__,
lib, g_module_error());
return -1;
}
if (!g_module_symbol(module, "sort_process", (gpointer)&sort->process )
|| !g_module_symbol(module, "sort_validate", (gpointer)&sort->validate )
|| !g_module_symbol(module, "sort_free_result", (gpointer)&sort->free_result )
|| !g_module_symbol(module, "sort_listextensions", (gpointer)&sort->listextensions )
|| !g_module_symbol(module, "sort_get_cancelkeep", (gpointer)&sort->get_cancelkeep )
|| !g_module_symbol(module, "sort_get_reject", (gpointer)&sort->get_reject )
|| !g_module_symbol(module, "sort_get_errormsg", (gpointer)&sort->get_errormsg )
|| !g_module_symbol(module, "sort_get_error", (gpointer)&sort->get_error )
|| !g_module_symbol(module, "sort_get_mailbox", (gpointer)&sort->get_mailbox )) {
trace(TRACE_ERROR, "%s,%s: cannot find function: %s: Did you enable SIEVE"
" sorting in the DELIVERY section of dbmail.conf but forget"
" to build the Sieve loadable module?",
__FILE__, __func__, g_module_error());
return -2;
}
return 0;
}
sort_result_t *sort_process(u64_t user_idnr, struct DbmailMessage *message)
{
if (!sort)
sort_load_driver();
if (!sort->process) {
trace(TRACE_ERROR, "%s, %s: Error loading sort driver",
__FILE__, __func__);
return NULL;
}
return sort->process(user_idnr, message);
}
sort_result_t *sort_validate(u64_t user_idnr, char *scriptname)
{
if (!sort)
sort_load_driver();
if (!sort->validate) {
trace(TRACE_ERROR, "%s, %s: Error loading sort driver",
__FILE__, __func__);
return NULL;
}
return sort->validate(user_idnr, scriptname);
}
const char *sort_listextensions(void)
{
if (!sort)
sort_load_driver();
if (!sort->listextensions) {
trace(TRACE_ERROR, "%s, %s: Error loading sort driver",
__FILE__, __func__);
return NULL;
}
return sort->listextensions();
}
void sort_free_result(sort_result_t *result)
{
if (!sort->free_result)
return;
return sort->free_result(result);
}
int sort_get_cancelkeep(sort_result_t *result)
{
if (!sort->get_cancelkeep)
return 0;
return sort->get_cancelkeep(result);
}
int sort_get_reject(sort_result_t *result)
{
if (!sort->get_reject)
return 0;
return sort->get_reject(result);
}
const char * sort_get_mailbox(sort_result_t *result)
{
if (!sort->get_mailbox)
return "";
return sort->get_mailbox(result);
}
const char * sort_get_errormsg(sort_result_t *result)
{
if (!sort->get_errormsg)
return "";
return sort->get_errormsg(result);
}
int sort_get_error(sort_result_t *result)
{
if (!sort->get_error)
return 0;
return sort->get_error(result);
}
|