SPDX: license tags (mostly by guesswork)
[exim.git] / src / src / lookups / cdb.c
index c9a5de1d81554188a8eab4b5c5bc61d2077f7679..696e520194cb736edfd3abe04e9d3e0e7e0dc5a5 100644 (file)
@@ -6,7 +6,9 @@
  * Exim - CDB database lookup module
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  *
+ * Copyright (c) The Exim Maintainers 2020 - 2022
  * Copyright (c) 1998 Nigel Metheringham, Planet Online Ltd
+ * SPDX-License-Identifier: GPL-2.0-or-later
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -156,19 +158,14 @@ void * mapbuf;
 
 if ((fileno = Uopen(filename, O_RDONLY, 0)) < 0)
   {
-  int save_errno = errno;
-  *errmsg = string_open_failed(errno, "%s for cdb lookup", filename);
-  errno = save_errno;
+  *errmsg = string_open_failed("%s for cdb lookup", filename);
   return NULL;
   }
 
 if (fstat(fileno, &statbuf) != 0)
   {
-  int save_errno = errno;
-  *errmsg = string_open_failed(errno,
-                             "fstat(%s) failed - cannot do cdb lookup",
+  *errmsg = string_open_failed("fstat(%s) failed - cannot do cdb lookup",
                              filename);
-  errno = save_errno;
   return NULL;
   }
 
@@ -177,16 +174,12 @@ CDB_HASH_TABLE bytes long */
 
 if (statbuf.st_size < CDB_HASH_TABLE)
   {
-  int save_errno = errno;
-  *errmsg = string_open_failed(errno,
-                             "%s too short for cdb lookup",
-                             filename);
-  errno = save_errno;
+  *errmsg = string_open_failed("%s too short for cdb lookup", filename);
   return NULL;
   }
 
 /* Having got a file open we need the structure to put things in */
-cdbp = store_get(sizeof(struct cdb_state), FALSE);
+cdbp = store_get(sizeof(struct cdb_state), GET_UNTAINTED);
 /* store_get() does not return if memory was not available... */
 /* preload the structure.... */
 cdbp->fileno = fileno;
@@ -221,7 +214,7 @@ DEBUG(D_lookup) debug_printf_indent("cdb mmap failed - %d\n", errno);
 /* get a buffer to stash the basic offsets in - this should speed
 things up a lot - especially on multiple lookups */
 
-cdbp->cdb_offsets = store_get(CDB_HASH_TABLE, FALSE);
+cdbp->cdb_offsets = store_get(CDB_HASH_TABLE, GET_UNTAINTED);
 
 /* now fill the buffer up... */
 
@@ -230,8 +223,7 @@ if (cdb_bread(fileno, cdbp->cdb_offsets, CDB_HASH_TABLE) == -1)
   /* read of hash table failed, oh dear, oh.....  time to give up I think....
   call the close routine (deallocs the memory), and return NULL */
 
-  *errmsg = string_open_failed(errno,
-                             "cannot read header from %s for cdb lookup",
+  *errmsg = string_open_failed("cannot read header from %s for cdb lookup",
                              filename);
   cdb_close(cdbp);
   return NULL;
@@ -264,7 +256,8 @@ return lf_check_file(cdbp->fileno, filename, S_IFREG, modemask,
 
 static int
 cdb_find(void * handle, const uschar * filename, const uschar * keystring,
-  int key_len, uschar ** result, uschar ** errmsg, uint * do_cache)
+  int key_len, uschar ** result, uschar ** errmsg, uint * do_cache,
+  const uschar * opts)
 {
 struct cdb_state * cdbp = handle;
 uint32 item_key_len,
@@ -279,9 +272,6 @@ hash_offset,
 hash_offlen,
 hash_slotnm;
 
-/* Keep picky compilers happy */
-do_cache = do_cache;
-
 key_hash = cdb_hash(keystring, key_len);
 
 hash_offset_entry = CDB_HASH_ENTRY * (key_hash & CDB_HASH_MASK);
@@ -354,7 +344,7 @@ if (cdbp->cdb_map != NULL)
           /* ... and the returned result.  Assume it is not
           tainted, lacking any way of telling.  */
 
-          *result = store_get(item_dat_len + 1, FALSE);
+          *result = store_get(item_dat_len + 1, GET_UNTAINTED);
           memcpy(*result, item_ptr, item_dat_len);
           (*result)[item_dat_len] = 0;
           return OK;
@@ -398,7 +388,7 @@ for (int loop = 0; (loop < hash_offlen); ++loop)
     if (item_key_len == key_len)
       {                                        /* finally check if key matches */
       rmark reset_point = store_mark();
-      uschar * item_key = store_get(key_len, TRUE); /* keys liable to be tainted */
+      uschar * item_key = store_get(key_len, GET_TAINTED); /* keys liable to be tainted */
 
       if (cdb_bread(cdbp->fileno, item_key, key_len) == -1) return DEFER;
       if (Ustrncmp(keystring, item_key, key_len) == 0)
@@ -412,7 +402,7 @@ for (int loop = 0; (loop < hash_offlen); ++loop)
         /* then we build a new result string.  We know we have enough
         memory so disable Coverity errors about the tainted item_dat_ken */
 
-        *result = store_get(item_dat_len + 1, FALSE);
+        *result = store_get(item_dat_len + 1, GET_UNTAINTED);
         /* coverity[tainted_data] */
         if (cdb_bread(cdbp->fileno, *result, item_dat_len) == -1)
          return DEFER;
@@ -469,25 +459,26 @@ if (cdbp->cdb_map)
 
 #include "../version.h"
 
-void
-cdb_version_report(FILE *f)
+gstring *
+cdb_version_report(gstring * g)
 {
 #ifdef DYNLOOKUP
-fprintf(f, "Library version: CDB: Exim version %s\n", EXIM_VERSION_STR);
+g = string_fmt_append(g, "Library version: CDB: Exim version %s\n", EXIM_VERSION_STR);
 #endif
+return g;
 }
 
 
 lookup_info cdb_lookup_info = {
-  US"cdb",                       /* lookup name */
-  lookup_absfile,                /* uses absolute file name */
-  cdb_open,                      /* open function */
-  cdb_check,                     /* check function */
-  cdb_find,                      /* find function */
-  cdb_close,                     /* close function */
-  NULL,                          /* no tidy function */
-  NULL,                          /* no quoting function */
-  cdb_version_report             /* version reporting */
+  .name = US"cdb",                     /* lookup name */
+  .type = lookup_absfile,              /* absolute file name */
+  .open = cdb_open,                    /* open function */
+  .check = cdb_check,                  /* check function */
+  .find = cdb_find,                    /* find function */
+  .close = cdb_close,                  /* close function */
+  .tidy = NULL,                                /* no tidy function */
+  .quote = NULL,                       /* no quoting function */
+  .version_report = cdb_version_report             /* version reporting */
 };
 
 #ifdef DYNLOOKUP