Remove attempts to quieten compiler static-checking
[exim.git] / src / src / lookups / dbmdb.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 #include "../exim.h"
10 #include "lf_functions.h"
11
12
13 /*************************************************
14 *              Open entry point                  *
15 *************************************************/
16
17 /* See local README for interface description */
18
19 static void *
20 dbmdb_open(const uschar * filename, uschar ** errmsg)
21 {
22 uschar * dirname = string_copy(filename);
23 uschar * s;
24 EXIM_DB *yield = NULL;
25
26 if ((s = Ustrrchr(dirname, '/'))) *s = '\0';
27 EXIM_DBOPEN(filename, dirname, O_RDONLY, 0, &yield);
28 if (!yield)
29   {
30   int save_errno = errno;
31   *errmsg = string_open_failed(errno, "%s as a %s file", filename, EXIM_DBTYPE);
32   errno = save_errno;
33   }
34 return yield;
35 }
36
37
38
39 /*************************************************
40 *             Check entry point                  *
41 *************************************************/
42
43 /* This needs to know more about the underlying files than is good for it!
44 We need to know what the real file names are in order to check the owners and
45 modes. If USE_DB is set, we know it is Berkeley DB, which uses an unmodified
46 file name. If USE_TDB or USE_GDBM is set, we know it is tdb or gdbm, which do
47 the same. Otherwise, for safety, we have to check for x.db or x.dir and x.pag.
48 */
49
50 static BOOL
51 dbmdb_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
52   gid_t *owngroups, uschar **errmsg)
53 {
54 int rc;
55
56 #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
57 rc = lf_check_file(-1, filename, S_IFREG, modemask, owners, owngroups,
58   "dbm", errmsg);
59 #else
60   {
61   uschar filebuffer[256];
62   (void)sprintf(CS filebuffer, "%.250s.db", filename);
63   rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
64     "dbm", errmsg);
65   if (rc < 0)        /* stat() failed */
66     {
67     (void)sprintf(CS filebuffer, "%.250s.dir", filename);
68     rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
69       "dbm", errmsg);
70     if (rc == 0)     /* x.dir was OK */
71       {
72       (void)sprintf(CS filebuffer, "%.250s.pag", filename);
73       rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
74         "dbm", errmsg);
75       }
76     }
77   }
78 #endif
79
80 return rc == 0;
81 }
82
83
84
85 /*************************************************
86 *              Find entry point                  *
87 *************************************************/
88
89 /* See local README for interface description. This function adds 1 to
90 the keylength in order to include the terminating zero. */
91
92 static int
93 dbmdb_find(void * handle, const uschar * filename, const uschar * keystring,
94   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
95   const uschar * opts)
96 {
97 EXIM_DB *d = (EXIM_DB *)handle;
98 EXIM_DATUM key, data;
99
100 EXIM_DATUM_INIT(key);               /* Some DBM libraries require datums to */
101 EXIM_DATUM_INIT(data);              /* be cleared before use. */
102 EXIM_DATUM_DATA(key) = CS keystring;
103 EXIM_DATUM_SIZE(key) = length + 1;
104
105 if (EXIM_DBGET(d, key, data))
106   {
107   *result = string_copyn(US EXIM_DATUM_DATA(data), EXIM_DATUM_SIZE(data));
108   EXIM_DATUM_FREE(data);            /* Some DBM libraries need a free() call */
109   return OK;
110   }
111 return FAIL;
112 }
113
114
115
116 /*************************************************
117 *      Find entry point - no zero on key         *
118 *************************************************/
119
120 /* See local README for interface description */
121
122 static int
123 dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
124   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
125   const uschar * opts)
126 {
127 return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
128   do_cache, opts);
129 }
130
131
132
133 /*************************************************
134 *     Find entry point - zero-joined list key    *
135 *************************************************/
136
137 /*
138  * The parameter passed as a key is a list in normal Exim list syntax.
139  * The elements of that list are joined together on NUL, with no trailing
140  * NUL, to form the key.
141  */
142
143 static int
144 dbmjz_find(void * handle, const uschar * filename, const uschar * keystring,
145   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
146   const uschar * opts)
147 {
148 uschar *key_item, *key_buffer, *key_p;
149 const uschar *key_elems = keystring;
150 int buflen, bufleft, key_item_len, sep = 0;
151
152 /* To a first approximation, the size of the lookup key needs to be about,
153 or less than, the length of the delimited list passed in + 1. */
154
155 buflen = length + 3;
156 key_buffer = store_get(buflen, is_tainted(keystring));
157
158 key_buffer[0] = '\0';
159
160 key_p = key_buffer;
161 bufleft = buflen;
162
163 /* In all cases of an empty list item, we can set 1 and advance by 1 and then
164 pick up the trailing NUL from the previous list item, EXCEPT when at the
165 beginning of the output string, in which case we need to supply that NUL
166 ourselves.  */
167 while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
168   {
169   key_item_len = Ustrlen(key_item) + 1;
170   if (key_item_len == 1)
171     {
172     key_p[0] = '\0';
173     if (key_p == key_buffer)
174       {
175       key_p[1] = '\0';
176       key_item_len += 1;
177       }
178     }
179
180   bufleft -= key_item_len;
181   if (bufleft <= 0)
182     {
183     /* The string_nextinlist() will stop at buffer size, but we should always
184     have at least 1 character extra, so some assumption has failed. */
185     *errmsg = string_copy(US"Ran out of buffer space for joining elements");
186     return DEFER;
187     }
188   key_p += key_item_len;
189   }
190
191 if (key_p == key_buffer)
192   {
193   *errmsg = string_copy(US"empty list key");
194   return FAIL;
195   }
196
197 /* We do not pass in the final NULL; if needed, the list should include an
198 empty element to put one in. Boundary: key length 1, is a NULL */
199 key_item_len = key_p - key_buffer - 1;
200
201 DEBUG(D_lookup) debug_printf_indent("NUL-joined key length: %d\n", key_item_len);
202
203 /* beware that dbmdb_find() adds 1 to length to get back terminating NUL, so
204 because we've calculated the real length, we need to subtract one more here */
205
206 return dbmdb_find(handle, filename, key_buffer, key_item_len - 1,
207     result, errmsg, do_cache, opts);
208 }
209
210
211
212 /*************************************************
213 *              Close entry point                 *
214 *************************************************/
215
216 /* See local README for interface description */
217
218 void
219 static dbmdb_close(void *handle)
220 {
221 EXIM_DBCLOSE((EXIM_DB *)handle);
222 }
223
224
225
226 /*************************************************
227 *         Version reporting entry point          *
228 *************************************************/
229
230 /* See local README for interface description. */
231
232 #include "../version.h"
233
234 void
235 dbm_version_report(FILE *f)
236 {
237 #ifdef DYNLOOKUP
238 fprintf(f, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
239 #endif
240 }
241
242
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 */
253 };
254
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) */
265 };
266
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) */
277 };
278
279 #ifdef DYNLOOKUP
280 #define dbmdb_lookup_module_info _lookup_module_info
281 #endif
282
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 };
285
286 /* End of lookups/dbmdb.c */