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 *
dbmdb_open(const uschar * filename,uschar ** errmsg)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   *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
dbmdb_check(void * handle,const uschar * filename,int modemask,uid_t * owners,gid_t * owngroups,uschar ** errmsg)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
dbmdb_find(void * handle,const uschar * filename,const uschar * keystring,int length,uschar ** result,uschar ** errmsg,uint * do_cache,const uschar * opts)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 EXIM_DATUM_DATA(key) = CS keystring;
99 EXIM_DATUM_SIZE(key) = length + 1;
100 
101 if (EXIM_DBGET(d, key, data))
102   {
103   *result = string_copyn(US EXIM_DATUM_DATA(data), EXIM_DATUM_SIZE(data));
104   EXIM_DATUM_FREE(data);            /* Some DBM libraries need a free() call */
105   return OK;
106   }
107 return FAIL;
108 }
109 
110 
111 
112 /*************************************************
113 *      Find entry point - no zero on key         *
114 *************************************************/
115 
116 /* See local README for interface description */
117 
118 static int
dbmnz_find(void * handle,const uschar * filename,const uschar * keystring,int length,uschar ** result,uschar ** errmsg,uint * do_cache,const uschar * opts)119 dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
120   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
121   const uschar * opts)
122 {
123 return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
124   do_cache, opts);
125 }
126 
127 
128 
129 /*************************************************
130 *     Find entry point - zero-joined list key    *
131 *************************************************/
132 
133 /*
134  * The parameter passed as a key is a list in normal Exim list syntax.
135  * The elements of that list are joined together on NUL, with no trailing
136  * NUL, to form the key.
137  */
138 
139 static int
dbmjz_find(void * handle,const uschar * filename,const uschar * keystring,int length,uschar ** result,uschar ** errmsg,uint * do_cache,const uschar * opts)140 dbmjz_find(void * handle, const uschar * filename, const uschar * keystring,
141   int length, uschar ** result, uschar ** errmsg, uint * do_cache,
142   const uschar * opts)
143 {
144 uschar *key_item, *key_buffer, *key_p;
145 const uschar *key_elems = keystring;
146 int buflen, bufleft, key_item_len, sep = 0;
147 
148 /* To a first approximation, the size of the lookup key needs to be about,
149 or less than, the length of the delimited list passed in + 1. */
150 
151 buflen = length + 3;
152 key_buffer = store_get(buflen, is_tainted(keystring));
153 
154 key_buffer[0] = '\0';
155 
156 key_p = key_buffer;
157 bufleft = buflen;
158 
159 /* In all cases of an empty list item, we can set 1 and advance by 1 and then
160 pick up the trailing NUL from the previous list item, EXCEPT when at the
161 beginning of the output string, in which case we need to supply that NUL
162 ourselves.  */
163 while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
164   {
165   key_item_len = Ustrlen(key_item) + 1;
166   if (key_item_len == 1)
167     {
168     key_p[0] = '\0';
169     if (key_p == key_buffer)
170       {
171       key_p[1] = '\0';
172       key_item_len += 1;
173       }
174     }
175 
176   bufleft -= key_item_len;
177   if (bufleft <= 0)
178     {
179     /* The string_nextinlist() will stop at buffer size, but we should always
180     have at least 1 character extra, so some assumption has failed. */
181     *errmsg = string_copy(US"Ran out of buffer space for joining elements");
182     return DEFER;
183     }
184   key_p += key_item_len;
185   }
186 
187 if (key_p == key_buffer)
188   {
189   *errmsg = string_copy(US"empty list key");
190   return FAIL;
191   }
192 
193 /* We do not pass in the final NULL; if needed, the list should include an
194 empty element to put one in. Boundary: key length 1, is a NULL */
195 key_item_len = key_p - key_buffer - 1;
196 
197 DEBUG(D_lookup) debug_printf_indent("NUL-joined key length: %d\n", key_item_len);
198 
199 /* beware that dbmdb_find() adds 1 to length to get back terminating NUL, so
200 because we've calculated the real length, we need to subtract one more here */
201 
202 return dbmdb_find(handle, filename, key_buffer, key_item_len - 1,
203     result, errmsg, do_cache, opts);
204 }
205 
206 
207 
208 /*************************************************
209 *              Close entry point                 *
210 *************************************************/
211 
212 /* See local README for interface description */
213 
214 void
dbmdb_close(void * handle)215 static dbmdb_close(void *handle)
216 {
217 EXIM_DBCLOSE((EXIM_DB *)handle);
218 }
219 
220 
221 
222 /*************************************************
223 *         Version reporting entry point          *
224 *************************************************/
225 
226 /* See local README for interface description. */
227 
228 #include "../version.h"
229 
230 void
dbm_version_report(FILE * f)231 dbm_version_report(FILE *f)
232 {
233 #ifdef DYNLOOKUP
234 fprintf(f, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
235 #endif
236 }
237 
238 
239 lookup_info dbm_lookup_info = {
240   .name = US"dbm",			/* lookup name */
241   .type = lookup_absfile,		/* uses absolute file name */
242   .open = dbmdb_open,			/* open function */
243   .check = dbmdb_check,			/* check function */
244   .find = dbmdb_find,			/* find function */
245   .close = dbmdb_close,			/* close function */
246   .tidy = NULL,				/* no tidy function */
247   .quote = NULL,			/* no quoting function */
248   .version_report = dbm_version_report             /* version reporting */
249 };
250 
251 lookup_info dbmz_lookup_info = {
252   .name = US"dbmnz",			/* lookup name */
253   .type = lookup_absfile,		/* uses absolute file name */
254   .open = dbmdb_open,			/* sic */     /* open function */
255   .check = dbmdb_check,			/* sic */     /* check function */
256   .find = dbmnz_find,			/* find function */
257   .close = dbmdb_close,			/* sic */     /* close function */
258   .tidy = NULL,				/* no tidy function */
259   .quote = NULL,			/* no quoting function */
260   .version_report = NULL                           /* no version reporting (redundant) */
261 };
262 
263 lookup_info dbmjz_lookup_info = {
264   .name = US"dbmjz",			/* lookup name */
265   .type = lookup_absfile,		/* uses absolute file name */
266   .open = dbmdb_open,			/* sic */     /* open function */
267   .check = dbmdb_check,			/* sic */     /* check function */
268   .find = dbmjz_find,			/* find function */
269   .close = dbmdb_close,			/* sic */     /* close function */
270   .tidy = NULL,				/* no tidy function */
271   .quote = NULL,			/* no quoting function */
272   .version_report = NULL                           /* no version reporting (redundant) */
273 };
274 
275 #ifdef DYNLOOKUP
276 #define dbmdb_lookup_module_info _lookup_module_info
277 #endif
278 
279 static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info, &dbmjz_lookup_info };
280 lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 3 };
281 
282 /* End of lookups/dbmdb.c */
283