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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*************************************************
* Exim - an Internet mail transport agent *
*************************************************/
/* Copyright (c) University of Cambridge 1995 - 2018 */
/* Copyright (c) The Exim Maintainers 2020 */
/* See the file NOTICE for conditions of use and distribution. */
#include "../exim.h"
#include "lf_functions.h"
/*************************************************
* Open entry point *
*************************************************/
/* See local README for interface description */
static void *
dbmdb_open(const uschar * filename, uschar ** errmsg)
{
uschar * dirname = string_copy(filename);
uschar * s;
EXIM_DB *yield = NULL;
if ((s = Ustrrchr(dirname, '/'))) *s = '\0';
EXIM_DBOPEN(filename, dirname, O_RDONLY, 0, &yield);
if (!yield)
{
int save_errno = errno;
*errmsg = string_open_failed(errno, "%s as a %s file", filename, EXIM_DBTYPE);
errno = save_errno;
}
return yield;
}
/*************************************************
* Check entry point *
*************************************************/
/* This needs to know more about the underlying files than is good for it!
We need to know what the real file names are in order to check the owners and
modes. If USE_DB is set, we know it is Berkeley DB, which uses an unmodified
file name. If USE_TDB or USE_GDBM is set, we know it is tdb or gdbm, which do
the same. Otherwise, for safety, we have to check for x.db or x.dir and x.pag.
*/
static BOOL
dbmdb_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
gid_t *owngroups, uschar **errmsg)
{
int rc;
handle = handle; /* Keep picky compilers happy */
#if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
rc = lf_check_file(-1, filename, S_IFREG, modemask, owners, owngroups,
"dbm", errmsg);
#else
{
uschar filebuffer[256];
(void)sprintf(CS filebuffer, "%.250s.db", filename);
rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
"dbm", errmsg);
if (rc < 0) /* stat() failed */
{
(void)sprintf(CS filebuffer, "%.250s.dir", filename);
rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
"dbm", errmsg);
if (rc == 0) /* x.dir was OK */
{
(void)sprintf(CS filebuffer, "%.250s.pag", filename);
rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
"dbm", errmsg);
}
}
}
#endif
return rc == 0;
}
/*************************************************
* Find entry point *
*************************************************/
/* See local README for interface description. This function adds 1 to
the keylength in order to include the terminating zero. */
static int
dbmdb_find(void * handle, const uschar * filename, const uschar * keystring,
int length, uschar ** result, uschar ** errmsg, uint * do_cache,
const uschar * opts)
{
EXIM_DB *d = (EXIM_DB *)handle;
EXIM_DATUM key, data;
filename = filename; /* Keep picky compilers happy */
errmsg = errmsg;
do_cache = do_cache;
EXIM_DATUM_INIT(key); /* Some DBM libraries require datums to */
EXIM_DATUM_INIT(data); /* be cleared before use. */
EXIM_DATUM_DATA(key) = CS keystring;
EXIM_DATUM_SIZE(key) = length + 1;
if (EXIM_DBGET(d, key, data))
{
*result = string_copyn(US EXIM_DATUM_DATA(data), EXIM_DATUM_SIZE(data));
EXIM_DATUM_FREE(data); /* Some DBM libraries need a free() call */
return OK;
}
return FAIL;
}
/*************************************************
* Find entry point - no zero on key *
*************************************************/
/* See local README for interface description */
static int
dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
int length, uschar ** result, uschar ** errmsg, uint * do_cache,
const uschar * opts)
{
return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
do_cache, opts);
}
/*************************************************
* Find entry point - zero-joined list key *
*************************************************/
/*
* The parameter passed as a key is a list in normal Exim list syntax.
* The elements of that list are joined together on NUL, with no trailing
* NUL, to form the key.
*/
static int
dbmjz_find(void * handle, const uschar * filename, const uschar * keystring,
int length, uschar ** result, uschar ** errmsg, uint * do_cache,
const uschar * opts)
{
uschar *key_item, *key_buffer, *key_p;
const uschar *key_elems = keystring;
int buflen, bufleft, key_item_len, sep = 0;
/* To a first approximation, the size of the lookup key needs to be about,
or less than, the length of the delimited list passed in + 1. */
buflen = length + 3;
key_buffer = store_get(buflen, is_tainted(keystring));
key_buffer[0] = '\0';
key_p = key_buffer;
bufleft = buflen;
/* In all cases of an empty list item, we can set 1 and advance by 1 and then
pick up the trailing NUL from the previous list item, EXCEPT when at the
beginning of the output string, in which case we need to supply that NUL
ourselves. */
while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
{
key_item_len = Ustrlen(key_item) + 1;
if (key_item_len == 1)
{
key_p[0] = '\0';
if (key_p == key_buffer)
{
key_p[1] = '\0';
key_item_len += 1;
}
}
bufleft -= key_item_len;
if (bufleft <= 0)
{
/* The string_nextinlist() will stop at buffer size, but we should always
have at least 1 character extra, so some assumption has failed. */
*errmsg = string_copy(US"Ran out of buffer space for joining elements");
return DEFER;
}
key_p += key_item_len;
}
if (key_p == key_buffer)
{
*errmsg = string_copy(US"empty list key");
return FAIL;
}
/* We do not pass in the final NULL; if needed, the list should include an
empty element to put one in. Boundary: key length 1, is a NULL */
key_item_len = key_p - key_buffer - 1;
DEBUG(D_lookup) debug_printf_indent("NUL-joined key length: %d\n", key_item_len);
/* beware that dbmdb_find() adds 1 to length to get back terminating NUL, so
because we've calculated the real length, we need to subtract one more here */
return dbmdb_find(handle, filename, key_buffer, key_item_len - 1,
result, errmsg, do_cache, opts);
}
/*************************************************
* Close entry point *
*************************************************/
/* See local README for interface description */
void
static dbmdb_close(void *handle)
{
EXIM_DBCLOSE((EXIM_DB *)handle);
}
/*************************************************
* Version reporting entry point *
*************************************************/
/* See local README for interface description. */
#include "../version.h"
void
dbm_version_report(FILE *f)
{
#ifdef DYNLOOKUP
fprintf(f, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
#endif
}
lookup_info dbm_lookup_info = {
.name = US"dbm", /* lookup name */
.type = lookup_absfile, /* uses absolute file name */
.open = dbmdb_open, /* open function */
.check = dbmdb_check, /* check function */
.find = dbmdb_find, /* find function */
.close = dbmdb_close, /* close function */
.tidy = NULL, /* no tidy function */
.quote = NULL, /* no quoting function */
.version_report = dbm_version_report /* version reporting */
};
lookup_info dbmz_lookup_info = {
.name = US"dbmnz", /* lookup name */
.type = lookup_absfile, /* uses absolute file name */
.open = dbmdb_open, /* sic */ /* open function */
.check = dbmdb_check, /* sic */ /* check function */
.find = dbmnz_find, /* find function */
.close = dbmdb_close, /* sic */ /* close function */
.tidy = NULL, /* no tidy function */
.quote = NULL, /* no quoting function */
.version_report = NULL /* no version reporting (redundant) */
};
lookup_info dbmjz_lookup_info = {
.name = US"dbmjz", /* lookup name */
.type = lookup_absfile, /* uses absolute file name */
.open = dbmdb_open, /* sic */ /* open function */
.check = dbmdb_check, /* sic */ /* check function */
.find = dbmjz_find, /* find function */
.close = dbmdb_close, /* sic */ /* close function */
.tidy = NULL, /* no tidy function */
.quote = NULL, /* no quoting function */
.version_report = NULL /* no version reporting (redundant) */
};
#ifdef DYNLOOKUP
#define dbmdb_lookup_module_info _lookup_module_info
#endif
static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info, &dbmjz_lookup_info };
lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 3 };
/* End of lookups/dbmdb.c */
|