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. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 #include "lf_functions.h"
14 /*************************************************
16 *************************************************/
18 /* See local README for interface description */
21 dbmdb_open(const uschar * filename, uschar ** errmsg)
23 uschar * dirname = string_copy(filename);
25 EXIM_DB * yield = NULL;
27 if ((s = Ustrrchr(dirname, '/'))) *s = '\0';
28 if (!(yield = exim_dbopen(filename, dirname, O_RDONLY, 0)))
29 *errmsg = string_open_failed("%s as a %s file", filename, EXIM_DBTYPE);
35 /*************************************************
37 *************************************************/
39 /* This needs to know more about the underlying files than is good for it!
40 We need to know what the real file names are in order to check the owners and
41 modes. If USE_DB is set, we know it is Berkeley DB, which uses an unmodified
42 file name. If USE_TDB or USE_GDBM is set, we know it is tdb or gdbm, which do
43 the same. Otherwise, for safety, we have to check for x.db or x.dir and x.pag.
47 dbmdb_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
48 gid_t *owngroups, uschar **errmsg)
52 #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
53 rc = lf_check_file(-1, filename, S_IFREG, modemask, owners, owngroups,
57 uschar filebuffer[256];
58 (void)sprintf(CS filebuffer, "%.250s.db", filename);
59 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
61 if (rc < 0) /* stat() failed */
63 (void)sprintf(CS filebuffer, "%.250s.dir", filename);
64 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
66 if (rc == 0) /* x.dir was OK */
68 (void)sprintf(CS filebuffer, "%.250s.pag", filename);
69 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
81 /*************************************************
83 *************************************************/
85 /* See local README for interface description. This function adds 1 to
86 the keylength in order to include the terminating zero. */
89 dbmdb_find(void * handle, const uschar * filename, const uschar * keystring,
90 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
93 EXIM_DB *d = (EXIM_DB *)handle;
96 exim_datum_init(&key); /* Some DBM libraries require datums to */
97 exim_datum_init(&data); /* be cleared before use. */
99 exim_datum_data_set(&key,
100 memcpy(store_get(length, keystring), keystring, length)); /* key can have embedded NUL */
101 exim_datum_size_set(&key, length);
103 if (exim_dbget(d, &key, &data))
105 unsigned len = exim_datum_size_get(&data);
106 *result = len > 0 ? string_copyn(exim_datum_data_get(&data), len) : US"";
107 exim_datum_free(&data); /* Some DBM libraries need a free() call */
115 /*************************************************
116 * Find entry point - no zero on key *
117 *************************************************/
119 /* See local README for interface description */
122 dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
123 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
126 return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
132 /*************************************************
133 * Find entry point - zero-joined list key *
134 *************************************************/
137 * The parameter passed as a key is a list in normal Exim list syntax.
138 * The elements of that list are joined together on NUL, with no trailing
139 * NUL, to form the key.
143 dbmjz_find(void * handle, const uschar * filename, const uschar * keystring,
144 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
147 uschar *key_item, *key_buffer, *key_p;
148 const uschar *key_elems = keystring;
149 int buflen, bufleft, key_item_len, sep = 0;
151 /* To a first approximation, the size of the lookup key needs to be about,
152 or less than, the length of the delimited list passed in + 1. */
155 key_buffer = store_get(buflen, keystring);
157 key_buffer[0] = '\0';
162 /* In all cases of an empty list item, we can set 1 and advance by 1 and then
163 pick up the trailing NUL from the previous list item, EXCEPT when at the
164 beginning of the output string, in which case we need to supply that NUL
166 while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
168 key_item_len = Ustrlen(key_item) + 1;
169 if (key_item_len == 1)
172 if (key_p == key_buffer)
179 bufleft -= key_item_len;
182 /* The string_nextinlist() will stop at buffer size, but we should always
183 have at least 1 character extra, so some assumption has failed. */
184 *errmsg = string_copy(US"Ran out of buffer space for joining elements");
187 key_p += key_item_len;
190 if (key_p == key_buffer)
192 *errmsg = string_copy(US"empty list key");
196 /* We do not pass in the final NULL; if needed, the list should include an
197 empty element to put one in. Boundary: key length 1, is a NULL */
198 key_item_len = key_p - key_buffer - 1;
200 DEBUG(D_lookup) debug_printf_indent("NUL-joined key length: %d\n", key_item_len);
202 /* beware that dbmdb_find() adds 1 to length to get back terminating NUL, so
203 because we've calculated the real length, we need to subtract one more here */
205 return dbmdb_find(handle, filename, key_buffer, key_item_len - 1,
206 result, errmsg, do_cache, opts);
211 /*************************************************
212 * Close entry point *
213 *************************************************/
215 /* See local README for interface description */
218 static dbmdb_close(void *handle)
220 exim_dbclose((EXIM_DB *)handle);
225 /*************************************************
226 * Version reporting entry point *
227 *************************************************/
229 /* See local README for interface description. */
231 #include "../version.h"
234 dbm_version_report(gstring * g)
237 g = string_fmt_append(g, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
243 lookup_info dbm_lookup_info = {
244 .name = US"dbm", /* lookup name */
245 .type = lookup_absfile, /* uses absolute file name */
246 .open = dbmdb_open, /* open function */
247 .check = dbmdb_check, /* check function */
248 .find = dbmdb_find, /* find function */
249 .close = dbmdb_close, /* close function */
250 .tidy = NULL, /* no tidy function */
251 .quote = NULL, /* no quoting function */
252 .version_report = dbm_version_report /* version reporting */
255 lookup_info dbmz_lookup_info = {
256 .name = US"dbmnz", /* lookup name */
257 .type = lookup_absfile, /* uses absolute file name */
258 .open = dbmdb_open, /* sic */ /* open function */
259 .check = dbmdb_check, /* sic */ /* check function */
260 .find = dbmnz_find, /* find function */
261 .close = dbmdb_close, /* sic */ /* close function */
262 .tidy = NULL, /* no tidy function */
263 .quote = NULL, /* no quoting function */
264 .version_report = NULL /* no version reporting (redundant) */
267 lookup_info dbmjz_lookup_info = {
268 .name = US"dbmjz", /* lookup name */
269 .type = lookup_absfile, /* uses absolute file name */
270 .open = dbmdb_open, /* sic */ /* open function */
271 .check = dbmdb_check, /* sic */ /* check function */
272 .find = dbmjz_find, /* find function */
273 .close = dbmdb_close, /* sic */ /* close function */
274 .tidy = NULL, /* no tidy function */
275 .quote = NULL, /* no quoting function */
276 .version_report = NULL /* no version reporting (redundant) */
280 #define dbmdb_lookup_module_info _lookup_module_info
283 static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info, &dbmjz_lookup_info };
284 lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 3 };
286 /* End of lookups/dbmdb.c */