Docs: clarify helo_allow_chars option
[exim.git] / src / src / lookups / dbmdb.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
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 */
9
10 #include "../exim.h"
11 #include "lf_functions.h"
12
13
14 /*************************************************
15 *              Open entry point                  *
16 *************************************************/
17
18 /* See local README for interface description */
19
20 static void *
21 dbmdb_open(const uschar * filename, uschar ** errmsg)
22 {
23 uschar * dirname = string_copy(filename);
24 uschar * s;
25 EXIM_DB * yield = NULL;
26
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);
30 return yield;
31 }
32
33
34
35 /*************************************************
36 *             Check entry point                  *
37 *************************************************/
38
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.
44 */
45
46 static BOOL
47 dbmdb_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
48   gid_t *owngroups, uschar **errmsg)
49 {
50 int rc;
51
52 #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM)
53 rc = lf_check_file(-1, filename, S_IFREG, modemask, owners, owngroups,
54   "dbm", errmsg);
55 #else
56   {
57   uschar filebuffer[256];
58   (void)sprintf(CS filebuffer, "%.250s.db", filename);
59   rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
60     "dbm", errmsg);
61   if (rc < 0)        /* stat() failed */
62     {
63     (void)sprintf(CS filebuffer, "%.250s.dir", filename);
64     rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
65       "dbm", errmsg);
66     if (rc == 0)     /* x.dir was OK */
67       {
68       (void)sprintf(CS filebuffer, "%.250s.pag", filename);
69       rc = lf_check_file(-1, filebuffer, S_IFREG, modemask, owners, owngroups,
70         "dbm", errmsg);
71       }
72     }
73   }
74 #endif
75
76 return rc == 0;
77 }
78
79
80
81 /*************************************************
82 *              Find entry point                  *
83 *************************************************/
84
85 /* See local README for interface description. This function adds 1 to
86 the keylength in order to include the terminating zero. */
87
88 static int
89 dbmdb_find(void * handle, const uschar * filename, const uschar * keystring,
90   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
91   const uschar * opts)
92 {
93 EXIM_DB *d = (EXIM_DB *)handle;
94 EXIM_DATUM key, data;
95
96 exim_datum_init(&key);               /* Some DBM libraries require datums to */
97 exim_datum_init(&data);              /* be cleared before use. */
98 length++;
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);
102
103 if (exim_dbget(d, &key, &data))
104   {
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 */
108   return OK;
109   }
110 return FAIL;
111 }
112
113
114
115 /*************************************************
116 *      Find entry point - no zero on key         *
117 *************************************************/
118
119 /* See local README for interface description */
120
121 static int
122 dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
123   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
124   const uschar * opts)
125 {
126 return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
127   do_cache, opts);
128 }
129
130
131
132 /*************************************************
133 *     Find entry point - zero-joined list key    *
134 *************************************************/
135
136 /*
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.
140  */
141
142 static int
143 dbmjz_find(void * handle, const uschar * filename, const uschar * keystring,
144   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
145   const uschar * opts)
146 {
147 uschar *key_item, *key_buffer, *key_p;
148 const uschar *key_elems = keystring;
149 int buflen, bufleft, key_item_len, sep = 0;
150
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. */
153
154 buflen = length + 3;
155 key_buffer = store_get(buflen, keystring);
156
157 key_buffer[0] = '\0';
158
159 key_p = key_buffer;
160 bufleft = buflen;
161
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
165 ourselves.  */
166 while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
167   {
168   key_item_len = Ustrlen(key_item) + 1;
169   if (key_item_len == 1)
170     {
171     key_p[0] = '\0';
172     if (key_p == key_buffer)
173       {
174       key_p[1] = '\0';
175       key_item_len += 1;
176       }
177     }
178
179   bufleft -= key_item_len;
180   if (bufleft <= 0)
181     {
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");
185     return DEFER;
186     }
187   key_p += key_item_len;
188   }
189
190 if (key_p == key_buffer)
191   {
192   *errmsg = string_copy(US"empty list key");
193   return FAIL;
194   }
195
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;
199
200 DEBUG(D_lookup) debug_printf_indent("NUL-joined key length: %d\n", key_item_len);
201
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 */
204
205 return dbmdb_find(handle, filename, key_buffer, key_item_len - 1,
206     result, errmsg, do_cache, opts);
207 }
208
209
210
211 /*************************************************
212 *              Close entry point                 *
213 *************************************************/
214
215 /* See local README for interface description */
216
217 void
218 static dbmdb_close(void *handle)
219 {
220 exim_dbclose((EXIM_DB *)handle);
221 }
222
223
224
225 /*************************************************
226 *         Version reporting entry point          *
227 *************************************************/
228
229 /* See local README for interface description. */
230
231 #include "../version.h"
232
233 gstring *
234 dbm_version_report(gstring * g)
235 {
236 #ifdef DYNLOOKUP
237 g = string_fmt_append(g, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
238 #endif
239 return g;
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 */
287 /* vi: aw ai sw=2
288 */