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);
145 lookup_info dbm_lookup_info = {
146 US"dbm", /* lookup name */
147 lookup_absfile, /* uses absolute file name */
148 dbmdb_open, /* open function */
149 dbmdb_check, /* check function */
150 dbmdb_find, /* find function */
151 dbmdb_close, /* close function */
152 NULL, /* no tidy function */
153 NULL /* no quoting function */
156 lookup_info dbmz_lookup_info = {
157 US"dbmnz", /* lookup name */
158 lookup_absfile, /* uses absolute file name */
159 dbmdb_open, /* sic */ /* open function */
160 dbmdb_check, /* sic */ /* check function */
161 dbmnz_find, /* find function */
162 dbmdb_close, /* sic */ /* close function */
163 NULL, /* no tidy function */
164 NULL /* no quoting function */
168 #define dbmdb_lookup_module_info _lookup_module_info
171 static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info };
172 lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 2 };
174 /* End of lookups/dbmdb.c */