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