constify
[exim.git] / src / src / lookups / dbmdb.c
index 29a4a3adb60905f2ca342a848721e9db6768dc25..f3bd6d2da2d4fcc3ab0a3742c5f1899be473d6ec 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/lookups/dbmdb.c,v 1.6 2009/11/16 19:50:38 nm4 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 #include "../exim.h"
 /* See local README for interface description */
 
 static void *
-dbmdb_open(uschar *filename, uschar **errmsg)
+dbmdb_open(const uschar * filename, uschar ** errmsg)
 {
-EXIM_DB *yield;
-EXIM_DBOPEN(filename, O_RDONLY, 0, &yield);
-if (yield == NULL)
+uschar * dirname = string_copy(filename);
+uschar * s;
+EXIM_DB *yield = NULL;
+
+if ((s = Ustrrchr(dirname, '/'))) *s = '\0';
+EXIM_DBOPEN(filename, dirname, O_RDONLY, 0, &yield);
+if (!yield)
   {
   int save_errno = errno;
   *errmsg = string_open_failed(errno, "%s as a %s file", filename, EXIM_DBTYPE);
@@ -45,7 +47,7 @@ the same. Otherwise, for safety, we have to check for x.db or x.dir and x.pag.
 */
 
 static BOOL
-dbmdb_check(void *handle, uschar *filename, int modemask, uid_t *owners,
+dbmdb_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
   gid_t *owngroups, uschar **errmsg)
 {
 int rc;
@@ -88,8 +90,8 @@ return rc == 0;
 the keylength in order to include the terminating zero. */
 
 static int
-dbmdb_find(void *handle, uschar *filename, uschar *keystring, int length,
-  uschar **result, uschar **errmsg, BOOL *do_cache)
+dbmdb_find(void * handle, const uschar * filename, const uschar * keystring,
+  int length, uschar ** result, uschar ** errmsg, uint * do_cache)
 {
 EXIM_DB *d = (EXIM_DB *)handle;
 EXIM_DATUM key, data;
@@ -120,9 +122,9 @@ return FAIL;
 
 /* See local README for interface description */
 
-int
-static dbmnz_find(void *handle, uschar *filename, uschar *keystring, int length,
-  uschar **result, uschar **errmsg, BOOL *do_cache)
+static int
+dbmnz_find(void * handle, const uschar * filename, const uschar * keystring,
+  int length, uschar ** result, uschar ** errmsg, uint * do_cache)
 {
 return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
   do_cache);
@@ -130,6 +132,84 @@ return dbmdb_find(handle, filename, keystring, length-1, result, errmsg,
 
 
 
+/*************************************************
+*     Find entry point - zero-joined list key    *
+*************************************************/
+
+/*
+ * The parameter passed as a key is a list in normal Exim list syntax.
+ * The elements of that list are joined together on NUL, with no trailing
+ * NUL, to form the key.
+ */
+
+static int
+dbmjz_find(void * handle, const uschar * filename, const uschar * keystring,
+  int length, uschar ** result, uschar ** errmsg, uint * do_cache)
+{
+uschar *key_item, *key_buffer, *key_p;
+const uschar *key_elems = keystring;
+int buflen, bufleft, key_item_len, sep = 0;
+
+/* To a first approximation, the size of the lookup key needs to be about,
+or less than, the length of the delimited list passed in + 1. */
+
+buflen = length + 3;
+key_buffer = store_get(buflen, is_tainted(keystring));
+
+key_buffer[0] = '\0';
+
+key_p = key_buffer;
+bufleft = buflen;
+
+/* In all cases of an empty list item, we can set 1 and advance by 1 and then
+pick up the trailing NUL from the previous list item, EXCEPT when at the
+beginning of the output string, in which case we need to supply that NUL
+ourselves.  */
+while ((key_item = string_nextinlist(&key_elems, &sep, key_p, bufleft)) != NULL)
+  {
+  key_item_len = Ustrlen(key_item) + 1;
+  if (key_item_len == 1)
+    {
+    key_p[0] = '\0';
+    if (key_p == key_buffer)
+      {
+      key_p[1] = '\0';
+      key_item_len += 1;
+      }
+    }
+
+  bufleft -= key_item_len;
+  if (bufleft <= 0)
+    {
+    /* The string_nextinlist() will stop at buffer size, but we should always
+    have at least 1 character extra, so some assumption has failed. */
+    *errmsg = string_copy(US"Ran out of buffer space for joining elements");
+    return DEFER;
+    }
+  key_p += key_item_len;
+  }
+
+if (key_p == key_buffer)
+  {
+  *errmsg = string_copy(US"empty list key");
+  return FAIL;
+  }
+
+/* We do not pass in the final NULL; if needed, the list should include an
+empty element to put one in. Boundary: key length 1, is a NULL */
+key_item_len = key_p - key_buffer - 1;
+
+DEBUG(D_lookup) debug_printf_indent("NUL-joined key length: %d\n", key_item_len);
+
+/* beware that dbmdb_find() adds 1 to length to get back terminating NUL, so
+because we've calculated the real length, we need to subtract one more here */
+
+return dbmdb_find(handle, filename, key_buffer, key_item_len - 1,
+    result, errmsg, do_cache);
+}
+
+
+
 /*************************************************
 *              Close entry point                 *
 *************************************************/
@@ -142,6 +222,25 @@ static dbmdb_close(void *handle)
 EXIM_DBCLOSE((EXIM_DB *)handle);
 }
 
+
+
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+dbm_version_report(FILE *f)
+{
+#ifdef DYNLOOKUP
+fprintf(f, "Library version: DBM: Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+
 lookup_info dbm_lookup_info = {
   US"dbm",                       /* lookup name */
   lookup_absfile,                /* uses absolute file name */
@@ -150,7 +249,8 @@ lookup_info dbm_lookup_info = {
   dbmdb_find,                    /* find function */
   dbmdb_close,                   /* close function */
   NULL,                          /* no tidy function */
-  NULL                           /* no quoting function */
+  NULL,                          /* no quoting function */
+  dbm_version_report             /* version reporting */
 };
 
 lookup_info dbmz_lookup_info = {
@@ -161,14 +261,27 @@ lookup_info dbmz_lookup_info = {
   dbmnz_find,                    /* find function */
   dbmdb_close,     /* sic */     /* close function */
   NULL,                          /* no tidy function */
-  NULL                           /* no quoting function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
+};
+
+lookup_info dbmjz_lookup_info = {
+  US"dbmjz",                     /* lookup name */
+  lookup_absfile,                /* uses absolute file name */
+  dbmdb_open,      /* sic */     /* open function */
+  dbmdb_check,     /* sic */     /* check function */
+  dbmjz_find,                    /* find function */
+  dbmdb_close,     /* sic */     /* close function */
+  NULL,                          /* no tidy function */
+  NULL,                          /* no quoting function */
+  NULL                           /* no version reporting (redundant) */
 };
 
 #ifdef DYNLOOKUP
 #define dbmdb_lookup_module_info _lookup_module_info
 #endif
 
-static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info };
-lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 2 };
+static lookup_info *_lookup_list[] = { &dbm_lookup_info, &dbmz_lookup_info, &dbmjz_lookup_info };
+lookup_module_info dbmdb_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 3 };
 
 /* End of lookups/dbmdb.c */