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