1 /* $Cambridge: exim/src/src/lookups/dbmdb.c,v 1.6 2009/11/16 19:50:38 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "lf_functions.h"
14 /*************************************************
16 *************************************************/
18 /* See local README for interface description */
21 dbmdb_open(uschar *filename, uschar **errmsg)
24 EXIM_DBOPEN(filename, O_RDONLY, 0, &yield);
27 int save_errno = errno;
28 *errmsg = string_open_failed(errno, "%s as a %s file", filename, EXIM_DBTYPE);
36 /*************************************************
38 *************************************************/
40 /* This needs to know more about the underlying files than is good for it!
41 We need to know what the real file names are in order to check the owners and
42 modes. If USE_DB is set, we know it is Berkeley DB, which uses an unmodified
43 file name. If USE_TDB or USE_GDBM is set, we know it is tdb or gdbm, which do
44 the same. Otherwise, for safety, we have to check for x.db or x.dir and x.pag.
48 dbmdb_check(void *handle, uschar *filename, int modemask, uid_t *owners,
49 gid_t *owngroups, uschar **errmsg)
52 handle = handle; /* Keep picky compilers happy */
54 #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
55 rc = lf_check_file(-1, filename, S_IFREG, modemask, owners, owngroups,
59 uschar filebuffer[256];
60 (void)sprintf(CS filebuffer, "%.250s.db", filename);
61 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
63 if (rc < 0) /* stat() failed */
65 (void)sprintf(CS filebuffer, "%.250s.dir", filename);
66 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
68 if (rc == 0) /* x.dir was OK */
70 (void)sprintf(CS filebuffer, "%.250s.pag", filename);
71 rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
83 /*************************************************
85 *************************************************/
87 /* See local README for interface description. This function adds 1 to
88 the keylength in order to include the terminating zero. */
91 dbmdb_find(void *handle, uschar *filename, uschar *keystring, int length,
92 uschar **result, uschar **errmsg, BOOL *do_cache)
94 EXIM_DB *d = (EXIM_DB *)handle;
97 filename = filename; /* Keep picky compilers happy */
101 EXIM_DATUM_INIT(key); /* Some DBM libraries require datums to */
102 EXIM_DATUM_INIT(data); /* be cleared before use. */
103 EXIM_DATUM_DATA(key) = CS keystring;
104 EXIM_DATUM_SIZE(key) = length + 1;
106 if (EXIM_DBGET(d, key, data))
108 *result = string_copyn(US EXIM_DATUM_DATA(data), EXIM_DATUM_SIZE(data));
109 EXIM_DATUM_FREE(data); /* Some DBM libraries need a free() call */
117 /*************************************************
118 * Find entry point - no zero on key *
119 *************************************************/
121 /* See local README for interface description */
124 static dbmnz_find(void *handle, uschar *filename, uschar *keystring, int length,
125 uschar **result, uschar **errmsg, BOOL *do_cache)
127 return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
133 /*************************************************
134 * Close entry point *
135 *************************************************/
137 /* See local README for interface description */
140 static dbmdb_close(void *handle)
142 EXIM_DBCLOSE((EXIM_DB *)handle);
147 /*************************************************
148 * Version reporting entry point *
149 *************************************************/
151 /* See local README for interface description. */
153 #include "../version.h"
156 dbm_version_report(FILE *f)
159 fprintf(f, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
164 lookup_info dbm_lookup_info = {
165 US"dbm", /* lookup name */
166 lookup_absfile, /* uses absolute file name */
167 dbmdb_open, /* open function */
168 dbmdb_check, /* check function */
169 dbmdb_find, /* find function */
170 dbmdb_close, /* close function */
171 NULL, /* no tidy function */
172 NULL, /* no quoting function */
173 dbm_version_report /* version reporting */
176 lookup_info dbmz_lookup_info = {
177 US"dbmnz", /* lookup name */
178 lookup_absfile, /* uses absolute file name */
179 dbmdb_open, /* sic */ /* open function */
180 dbmdb_check, /* sic */ /* check function */
181 dbmnz_find, /* find function */
182 dbmdb_close, /* sic */ /* close function */
183 NULL, /* no tidy function */
184 NULL, /* no quoting function */
185 NULL /* no version reporting (redundant) */
189 #define dbmdb_lookup_module_info _lookup_module_info
192 static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info };
193 lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 2 };
195 /* End of lookups/dbmdb.c */