1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
10 #include "lf_functions.h"
13 /*************************************************
15 *************************************************/
17 /* See local README for interface description */
20 dbmdb_open(const uschar * filename, uschar ** errmsg)
22 uschar * dirname = string_copy(filename);
24 EXIM_DB * yield = NULL;
26 if ((s = Ustrrchr(dirname, '/'))) *s = '\0';
27 if (!(yield = exim_dbopen(filename, dirname, O_RDONLY, 0)))
28 *errmsg = string_open_failed("%s as a %s file", filename, EXIM_DBTYPE);
34 /*************************************************
36 *************************************************/
38 /* This needs to know more about the underlying files than is good for it!
39 We need to know what the real file names are in order to check the owners and
40 modes. If USE_DB is set, we know it is Berkeley DB, which uses an unmodified
41 file name. If USE_TDB or USE_GDBM is set, we know it is tdb or gdbm, which do
42 the same. Otherwise, for safety, we have to check for x.db or x.dir and x.pag.
46 dbmdb_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
47 gid_t *owngroups, uschar **errmsg)
51 #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
52 rc = lf_check_file(-1, filename, S_IFREG, modemask, owners, owngroups,
56 uschar filebuffer[256];
57 (void)sprintf(CS filebuffer, "%.250s.db", filename);
58 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
60 if (rc < 0) /* stat() failed */
62 (void)sprintf(CS filebuffer, "%.250s.dir", filename);
63 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
65 if (rc == 0) /* x.dir was OK */
67 (void)sprintf(CS filebuffer, "%.250s.pag", filename);
68 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
80 /*************************************************
82 *************************************************/
84 /* See local README for interface description. This function adds 1 to
85 the keylength in order to include the terminating zero. */
88 dbmdb_find(void * handle, const uschar * filename, const uschar * keystring,
89 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
92 EXIM_DB *d = (EXIM_DB *)handle;
95 exim_datum_init(&key); /* Some DBM libraries require datums to */
96 exim_datum_init(&data); /* be cleared before use. */
98 exim_datum_data_set(&key,
99 memcpy(store_get(length, keystring), keystring, length)); /* key can have embedded NUL */
100 exim_datum_size_set(&key, length);
102 if (exim_dbget(d, &key, &data))
104 *result = string_copyn(exim_datum_data_get(&data), exim_datum_size_get(&data));
105 exim_datum_free(&data); /* Some DBM libraries need a free() call */
113 /*************************************************
114 * Find entry point - no zero on key *
115 *************************************************/
117 /* See local README for interface description */
120 dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
121 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
124 return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
130 /*************************************************
131 * Find entry point - zero-joined list key *
132 *************************************************/
135 * The parameter passed as a key is a list in normal Exim list syntax.
136 * The elements of that list are joined together on NUL, with no trailing
137 * NUL, to form the key.
141 dbmjz_find(void * handle, const uschar * filename, const uschar * keystring,
142 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
145 uschar *key_item, *key_buffer, *key_p;
146 const uschar *key_elems = keystring;
147 int buflen, bufleft, key_item_len, sep = 0;
149 /* To a first approximation, the size of the lookup key needs to be about,
150 or less than, the length of the delimited list passed in + 1. */
153 key_buffer = store_get(buflen, keystring);
155 key_buffer[0] = '\0';
160 /* In all cases of an empty list item, we can set 1 and advance by 1 and then
161 pick up the trailing NUL from the previous list item, EXCEPT when at the
162 beginning of the output string, in which case we need to supply that NUL
164 while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
166 key_item_len = Ustrlen(key_item) + 1;
167 if (key_item_len == 1)
170 if (key_p == key_buffer)
177 bufleft -= key_item_len;
180 /* The string_nextinlist() will stop at buffer size, but we should always
181 have at least 1 character extra, so some assumption has failed. */
182 *errmsg = string_copy(US"Ran out of buffer space for joining elements");
185 key_p += key_item_len;
188 if (key_p == key_buffer)
190 *errmsg = string_copy(US"empty list key");
194 /* We do not pass in the final NULL; if needed, the list should include an
195 empty element to put one in. Boundary: key length 1, is a NULL */
196 key_item_len = key_p - key_buffer - 1;
198 DEBUG(D_lookup) debug_printf_indent("NUL-joined key length: %d\n", key_item_len);
200 /* beware that dbmdb_find() adds 1 to length to get back terminating NUL, so
201 because we've calculated the real length, we need to subtract one more here */
203 return dbmdb_find(handle, filename, key_buffer, key_item_len - 1,
204 result, errmsg, do_cache, opts);
209 /*************************************************
210 * Close entry point *
211 *************************************************/
213 /* See local README for interface description */
216 static dbmdb_close(void *handle)
218 exim_dbclose((EXIM_DB *)handle);
223 /*************************************************
224 * Version reporting entry point *
225 *************************************************/
227 /* See local README for interface description. */
229 #include "../version.h"
232 dbm_version_report(gstring * g)
235 g = string_fmt_append(g, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
241 lookup_info dbm_lookup_info = {
242 .name = US"dbm", /* lookup name */
243 .type = lookup_absfile, /* uses absolute file name */
244 .open = dbmdb_open, /* open function */
245 .check = dbmdb_check, /* check function */
246 .find = dbmdb_find, /* find function */
247 .close = dbmdb_close, /* close function */
248 .tidy = NULL, /* no tidy function */
249 .quote = NULL, /* no quoting function */
250 .version_report = dbm_version_report /* version reporting */
253 lookup_info dbmz_lookup_info = {
254 .name = US"dbmnz", /* lookup name */
255 .type = lookup_absfile, /* uses absolute file name */
256 .open = dbmdb_open, /* sic */ /* open function */
257 .check = dbmdb_check, /* sic */ /* check function */
258 .find = dbmnz_find, /* find function */
259 .close = dbmdb_close, /* sic */ /* close function */
260 .tidy = NULL, /* no tidy function */
261 .quote = NULL, /* no quoting function */
262 .version_report = NULL /* no version reporting (redundant) */
265 lookup_info dbmjz_lookup_info = {
266 .name = US"dbmjz", /* lookup name */
267 .type = lookup_absfile, /* uses absolute file name */
268 .open = dbmdb_open, /* sic */ /* open function */
269 .check = dbmdb_check, /* sic */ /* check function */
270 .find = dbmjz_find, /* find function */
271 .close = dbmdb_close, /* sic */ /* close function */
272 .tidy = NULL, /* no tidy function */
273 .quote = NULL, /* no quoting function */
274 .version_report = NULL /* no version reporting (redundant) */
278 #define dbmdb_lookup_module_info _lookup_module_info
281 static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info, &dbmjz_lookup_info };
282 lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 3 };
284 /* End of lookups/dbmdb.c */