Hintsbd: fix locking
[exim.git] / src / src / exim_dbutil.c
index 2cee43b8507ad7d3dcbb13e99939535999892108..c466ae380dc7bfdbab63741bfbbab8f849548329 100644 (file)
@@ -2,9 +2,10 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
+/* Copyright (c) The Exim Maintainers 2020 - 2024 */
 /* Copyright (c) University of Cambridge 1995 - 2018 */
-/* Copyright (c) The Exim Maintainers 2020 - 2021 */
 /* See the file NOTICE for conditions of use and distribution. */
+/* SPDX-License-Identifier: GPL-2.0-or-later */
 
 
 /* This single source file is used to compile three utility programs for
@@ -47,6 +48,7 @@ whose inclusion is controlled by -D on the compilation command. */
 
 uschar *spool_directory;
 
+BOOL keyonly = FALSE;
 BOOL utc = FALSE;
 
 
@@ -59,6 +61,9 @@ uschar *
 readconf_printtime(int t)
 { return NULL; }
 gstring *
+string_catn(gstring * g, const uschar * s, int count)
+{ return NULL; }
+gstring *
 string_vformat_trc(gstring * g, const uschar * func, unsigned line,
   unsigned size_limit, unsigned flags, const char *format, va_list ap)
 { return NULL; }
@@ -125,13 +130,13 @@ check_args(int argc, uschar **argv, uschar *name, uschar *options)
 uschar * aname = argv[optind + 1];
 if (argc - optind == 2)
   {
-  if (Ustrcmp(aname, "retry") == 0)            return type_retry;
-  if (Ustrcmp(aname, "misc") == 0)             return type_misc;
+  if (Ustrcmp(aname, "retry") == 0)    return type_retry;
+  if (Ustrcmp(aname, "misc") == 0)     return type_misc;
   if (Ustrncmp(aname, "wait-", 5) == 0)        return type_wait;
-  if (Ustrcmp(aname, "callout") == 0)          return type_callout;
+  if (Ustrcmp(aname, "callout") == 0)  return type_callout;
   if (Ustrcmp(aname, "ratelimit") == 0)        return type_ratelimit;
-  if (Ustrcmp(aname, "tls") == 0)              return type_tls;
-  if (Ustrcmp(aname, "seen") == 0)             return type_seen;
+  if (Ustrcmp(aname, "tls") == 0)      return type_tls;
+  if (Ustrcmp(aname, "seen") == 0)     return type_seen;
   }
 usage(name, options);
 return -1;              /* Never obeyed */
@@ -140,16 +145,17 @@ return -1;              /* Never obeyed */
 
 FUNC_MAYBE_UNUSED
 static void
-options(int argc, uschar * argv[], uschar * name)
+options(int argc, uschar * argv[], uschar * name, const uschar * opts)
 {
 int opt;
 
 opterr = 0;
-while ((opt = getopt(argc, (char * const *)argv, "z")) != -1)
+while ((opt = getopt(argc, (char * const *)argv, CCS opts)) != -1)
   switch (opt)
   {
+  case 'k':    keyonly = TRUE; break;
   case 'z':    utc = TRUE; break;
-  default:     usage(name, US" [-z]");
+  default:     usage(name, US" [-z] [-k]");
   }
 }
 
@@ -281,28 +287,26 @@ Returns:   NULL if the open failed, or the locking failed.
 */
 
 open_db *
-dbfn_open(uschar *name, int flags, open_db *dbblock, BOOL lof, BOOL panic)
+dbfn_open(const uschar * name, int flags, open_db * dbblock,
+  BOOL lof, BOOL panic)
 {
 int rc;
 struct flock lock_data;
-BOOL read_only = flags == O_RDONLY;
+BOOL read_only = (flags & (O_WRONLY|O_RDWR)) == O_RDONLY;
 uschar * dirname, * filename;
 
 /* The first thing to do is to open a separate file on which to lock. This
 ensures that Exim has exclusive use of the database before it even tries to
-open it. If there is a database, there should be a lock file in existence. */
+open it. If there is a database, there should be a lock file in existence;
+if no lockfile we infer there is no database and error out.  We open the
+lockfile using the r/w mode requested for the DB, users lacking permission
+for the DB access mode will error out here. */
 
-#ifdef COMPILE_UTILITY
 if (  asprintf(CSS &dirname, "%s/db", spool_directory) < 0
    || asprintf(CSS &filename, "%s/%s.lockfile", dirname, name) < 0)
   return NULL;
-#else
-dirname = string_sprintf("%s/db", spool_directory);
-filename = string_sprintf("%s/%s.lockfile", dirname, name);
-#endif
 
-dbblock->lockfd = Uopen(filename, flags, 0);
-if (dbblock->lockfd < 0)
+if ((dbblock->lockfd = Uopen(filename, flags, 0)) < 0)
   {
   printf("** Failed to open database lock file %s: %s\n", filename,
     strerror(errno));
@@ -325,7 +329,7 @@ if (sigalrm_seen) errno = ETIMEDOUT;
 if (rc < 0)
   {
   printf("** Failed to get %s lock for %s: %s",
-    flags & O_WRONLY ? "write" : "read",
+    read_only ? "read" : "write",
     filename,
     errno == ETIMEDOUT ? "timed out" : strerror(errno));
   (void)close(dbblock->lockfd);
@@ -335,14 +339,11 @@ if (rc < 0)
 /* At this point we have an opened and locked separate lock file, that is,
 exclusive access to the database, so we can go ahead and open it. */
 
-#ifdef COMPILE_UTILITY
 if (asprintf(CSS &filename, "%s/%s", dirname, name) < 0) return NULL;
-#else
-filename = string_sprintf("%s/%s", dirname, name);
-#endif
-dbblock->dbptr = exim_dbopen(filename, dirname, flags, 0);
 
-if (!dbblock->dbptr)
+if (flags & O_RDWR) flags |= O_CREAT;
+
+if (!(dbblock->dbptr = exim_dbopen(filename, dirname, flags, 0)))
   {
   printf("** Failed to open DBM file %s for %s:\n   %s%s\n", filename,
     read_only? "reading" : "writing", strerror(errno),
@@ -401,12 +402,13 @@ Returns: a pointer to the retrieved record, or
 */
 
 void *
-dbfn_read_with_length(open_db *dbblock, const uschar *key, int *length)
+dbfn_read_with_length(open_db * dbblock, const uschar * key, int * length)
 {
-void *yield;
+void * yield;
 EXIM_DATUM key_datum, result_datum;
 int klen = Ustrlen(key) + 1;
 uschar * key_copy = store_get(klen, key);
+unsigned dlen;
 
 memcpy(key_copy, key, klen);
 
@@ -420,9 +422,10 @@ if (!exim_dbget(dbblock->dbptr, &key_datum, &result_datum)) return NULL;
 /* Assume for now that anything stored could have been tainted. Properly
 we should store the taint status along with the data. */
 
-yield = store_get(exim_datum_size_get(&result_datum), GET_TAINTED);
-memcpy(yield, exim_datum_data_get(&result_datum), exim_datum_size_get(&result_datum));
-if (length) *length = exim_datum_size_get(&result_datum);
+dlen = exim_datum_size_get(&result_datum);
+yield = store_get(dlen, GET_TAINTED);
+memcpy(yield, exim_datum_data_get(&result_datum), dlen);
+if (length) *length = dlen;
 
 exim_datum_free(&result_datum);    /* Some DBM libs require freeing */
 return yield;
@@ -512,7 +515,7 @@ Arguments:
   cursor   a pointer to a pointer to a cursor anchor, for those dbm libraries
            that use the notion of a cursor
 
-Returns:   the next record from the file, or
+Returns:   the next *key* (nul-terminated) from the file, or
            NULL if there are no more
 */
 
@@ -558,11 +561,11 @@ uschar **argv = USS cargv;
 uschar keybuffer[1024];
 
 store_init();
-options(argc, argv, US"dumpdb");
+options(argc, argv, US"dumpdb", US"kz");
 
 /* Check the arguments, and open the database */
 
-dbdata_type = check_args(argc, argv, US"dumpdb", US" [-z]");
+dbdata_type = check_args(argc, argv, US"dumpdb", US" [-z] [-k]");
 argc -= optind; argv += optind;
 spool_directory = argv[0];
 
@@ -601,12 +604,13 @@ for (uschar * key = dbfn_scan(dbm, TRUE, &cursor);
     }
   Ustrcpy(keybuffer, key);
 
-  if (!(value = dbfn_read_with_length(dbm, keybuffer, &length)))
+  if (keyonly)
+    printf("  %s\n", keybuffer);
+  else if (!(value = dbfn_read_with_length(dbm, keybuffer, &length)))
     fprintf(stderr, "**** Entry \"%s\" was in the key scan, but the record "
                     "was not found in the file - something is wrong!\n",
       CS keybuffer);
   else
-    {
     /* Note: don't use print_time more than once in one statement, since
     it uses a single buffer. */
 
@@ -619,7 +623,7 @@ for (uschar * key = dbfn_scan(dbm, TRUE, &cursor);
          print_time(retry->first_failed));
        printf("%s  ", print_time(retry->last_try));
        printf("%s %s\n", print_time(retry->next_try),
-         (retry->expired)? "*" : "");
+         retry->expired ? "*" : "");
        break;
 
       case type_wait:
@@ -729,7 +733,6 @@ for (uschar * key = dbfn_scan(dbm, TRUE, &cursor);
        printf("%s\t%s\n", keybuffer, print_time(seen->time_stamp));
        break;
       }
-    }
   store_reset(reset_point);
   }
 
@@ -785,7 +788,7 @@ rmark reset_point;
 uschar * aname;
 
 store_init();
-options(argc, argv, US"fixdb");
+options(argc, argv, US"fixdb", US"z");
 name[0] = 0;  /* No name set */
 
 /* Sort out the database type, verify what we are working on and then process
@@ -1419,3 +1422,5 @@ return 0;
 #endif  /* EXIM_TIDYDB */
 
 /* End of exim_dbutil.c */
+/* vi: aw ai sw=2
+*/