UTC output option for exim_dumpdb, exim_fixdb
[exim.git] / src / src / exim_dbutil.c
index 5c7b6650ed5726f00160ea63008ff36d217e9877..697b87500ec846f938deee86adeec735092d4297 100644 (file)
@@ -17,11 +17,13 @@ maintaining Exim hints databases.
 In all cases, the first argument is the name of the spool directory. The second
 argument is the name of the database file. The available names are:
 
-  retry:      retry delivery information
-  misc:       miscellaneous hints data
-  wait-<t>:   message waiting information; <t> is a transport name
-  callout:    callout verification cache
-  tls:       TLS session resumption cache
+  callout:     callout verification cache
+  misc:                miscellaneous hints data
+  ratelimit:   record for ACL "ratelimit" condition
+  retry:       etry delivery information
+  seen:                imestamp records for ACL "seen" condition
+  tls:         TLS session resumption cache
+  wait-<t>:    message waiting information; <t> is a transport name
 
 There are a number of common subroutines, followed by three main programs,
 whose inclusion is controlled by -D on the compilation command. */
@@ -38,12 +40,15 @@ whose inclusion is controlled by -D on the compilation command. */
 #define type_callout   4
 #define type_ratelimit 5
 #define type_tls       6
+#define type_seen      7
 
 
 /* This is used by our cut-down dbfn_open(). */
 
 uschar *spool_directory;
 
+BOOL utc = FALSE;
+
 
 /******************************************************************************/
       /* dummies needed by Solaris build */
@@ -69,6 +74,13 @@ struct global_flags  f;
 unsigned int           log_selector[1];
 uschar *               queue_name;
 BOOL                   split_spool_directory;
+
+
+/* These introduced by the taintwarn handling */
+#ifdef ALLOW_INSECURE_TAINTED_DATA
+BOOL    allow_insecure_tainted_data;
+#endif
+
 /******************************************************************************/
 
 
@@ -106,7 +118,6 @@ SIGNAL_BOOL sigalrm_seen;
 void
 sigalrm_handler(int sig)
 {
-sig = sig;            /* Keep picky compilers happy */
 sigalrm_seen = 1;
 }
 
@@ -120,8 +131,8 @@ static void
 usage(uschar *name, uschar *options)
 {
 printf("Usage: exim_%s%s  <spool-directory> <database-name>\n", name, options);
-printf("  <database-name> = retry | misc | wait-<transport-name> | callout | ratelimit | tls\n");
-exit(1);
+printf("  <database-name> = retry | misc | wait-<transport-name> | callout | ratelimit | tls | seen\n");
+exit(EXIT_FAILURE);
 }
 
 
@@ -136,20 +147,38 @@ second of them to be sure it is a known database name. */
 static int
 check_args(int argc, uschar **argv, uschar *name, uschar *options)
 {
-if (argc == 3)
+uschar * aname = argv[optind + 1];
+if (argc - optind == 2)
   {
-  if (Ustrcmp(argv[2], "retry") == 0) return type_retry;
-  if (Ustrcmp(argv[2], "misc") == 0) return type_misc;
-  if (Ustrncmp(argv[2], "wait-", 5) == 0) return type_wait;
-  if (Ustrcmp(argv[2], "callout") == 0) return type_callout;
-  if (Ustrcmp(argv[2], "ratelimit") == 0) return type_ratelimit;
-  if (Ustrcmp(argv[2], "tls") == 0) return type_tls;
+  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, "ratelimit") == 0)        return type_ratelimit;
+  if (Ustrcmp(aname, "tls") == 0)              return type_tls;
+  if (Ustrcmp(aname, "seen") == 0)             return type_seen;
   }
 usage(name, options);
 return -1;              /* Never obeyed */
 }
 
 
+static void
+options(int argc, uschar * argv[], uschar * name)
+{
+int opt;
+
+opterr = 0;
+while ((opt = getopt(argc, (char * const *)argv, "z")) != -1)
+  switch (opt)
+  {
+  case 'z':    utc = TRUE; break;
+  default:     usage(name, US" [-z]");
+  }
+}
+
+
+
 
 /*************************************************
 *         Handle attempts to write the log       *
@@ -177,8 +206,6 @@ va_start(ap, format);
 vfprintf(stderr, format, ap);
 fprintf(stderr, "\n");
 va_end(ap);
-selector = selector;     /* Keep picky compilers happy */
-flags = flags;
 }
 
 
@@ -192,7 +219,7 @@ static uschar time_buffer[sizeof("09-xxx-1999 hh:mm:ss  ")];
 uschar *
 print_time(time_t t)
 {
-struct tm *tmstr = localtime(&t);
+struct tm *tmstr = utc ? gmtime(&t) : localtime(&t);
 Ustrftime(time_buffer, sizeof(time_buffer), "%d-%b-%Y %H:%M:%S", tmstr);
 return time_buffer;
 }
@@ -206,8 +233,8 @@ return time_buffer;
 uschar *
 print_cache(int value)
 {
-return (value == ccache_accept)? US"accept" :
-       (value == ccache_reject)? US"reject" :
+return value == ccache_accept ? US"accept" :
+       value == ccache_reject ? US"reject" :
        US"unknown";
 }
 
@@ -391,7 +418,7 @@ pick out the timestamps, etc., do the copying centrally here.
 Arguments:
   dbblock   a pointer to an open database block
   key       the key of the record to be read
-  length    where to put the length (or NULL if length not wanted)
+  length    where to put the length (or NULL if length not wanted). Includes overhead.
 
 Returns: a pointer to the retrieved record, or
          NULL if the record is not found
@@ -419,7 +446,7 @@ we should store the taint status along with the data. */
 
 yield = store_get(EXIM_DATUM_SIZE(result_datum), TRUE);
 memcpy(yield, EXIM_DATUM_DATA(result_datum), EXIM_DATUM_SIZE(result_datum));
-if (length != NULL) *length = EXIM_DATUM_SIZE(result_datum);
+if (length) *length = EXIM_DATUM_SIZE(result_datum);
 
 EXIM_DATUM_FREE(result_datum);    /* Some DBM libs require freeing */
 return yield;
@@ -518,7 +545,6 @@ dbfn_scan(open_db *dbblock, BOOL start, EXIM_CURSOR **cursor)
 {
 EXIM_DATUM key_datum, value_datum;
 uschar *yield;
-value_datum = value_datum;    /* dummy; not all db libraries use this */
 
 /* Some dbm require an initialization */
 
@@ -555,11 +581,16 @@ EXIM_CURSOR *cursor;
 uschar **argv = USS cargv;
 uschar keybuffer[1024];
 
+store_init();
+options(argc, argv, US"dumpdb");
+
 /* Check the arguments, and open the database */
 
-dbdata_type = check_args(argc, argv, US"dumpdb", US"");
-spool_directory = argv[1];
-if (!(dbm = dbfn_open(argv[2], O_RDONLY, &dbblock, FALSE, TRUE)))
+dbdata_type = check_args(argc, argv, US"dumpdb", US" [-z]");
+argc -= optind; argv += optind;
+spool_directory = argv[0];
+
+if (!(dbm = dbfn_open(argv[1], O_RDONLY, &dbblock, FALSE, TRUE)))
   exit(1);
 
 /* Scan the file, formatting the information for each entry. Note
@@ -576,6 +607,7 @@ for (uschar * key = dbfn_scan(dbm, TRUE, &cursor);
   dbdata_ratelimit *ratelimit;
   dbdata_ratelimit_unique *rate_unique;
   dbdata_tls_session *session;
+  dbdata_seen *seen;
   int count_bad = 0;
   int length;
   uschar *t;
@@ -620,6 +652,7 @@ for (uschar * key = dbfn_scan(dbm, TRUE, &cursor);
        t = wait->text;
        name[MESSAGE_ID_LENGTH] = 0;
 
+    /* Leave corrupt records alone */
        if (wait->count > WAIT_NAME_MAX)
          {
          fprintf(stderr,
@@ -714,6 +747,11 @@ for (uschar * key = dbfn_scan(dbm, TRUE, &cursor);
        session = (dbdata_tls_session *)value;
        printf("  %s %.*s\n", keybuffer, length, session->session);
        break;
+
+      case type_seen:
+       seen = (dbdata_seen *)value;
+       printf("%s\t%s\n", keybuffer, print_time(seen->time_stamp));
+       break;
       }
     }
   store_reset(reset_point);
@@ -760,21 +798,29 @@ If the record name is omitted from (2) or (3), the previously used record name
 is re-used. */
 
 
-int main(int argc, char **cargv)
+int
+main(int argc, char **cargv)
 {
 int dbdata_type;
 uschar **argv = USS cargv;
 uschar buffer[256];
 uschar name[256];
 rmark reset_point;
+uschar * aname;
 
+store_init();
+options(argc, argv, US"fixdb");
 name[0] = 0;  /* No name set */
 
 /* Sort out the database type, verify what we are working on and then process
 user requests */
 
-dbdata_type = check_args(argc, argv, US"fixdb", US"");
-printf("Modifying Exim hints database %s/db/%s\n", argv[1], argv[2]);
+dbdata_type = check_args(argc, argv, US"fixdb", US" [-z]");
+argc -= optind; argv += optind;
+spool_directory = argv[0];
+aname = argv[1];
+
+printf("Modifying Exim hints database %s/db/%s\n", spool_directory, aname);
 
 for(; (reset_point = store_mark()); store_reset(reset_point))
   {
@@ -821,9 +867,8 @@ for(; (reset_point = store_mark()); store_reset(reset_point))
   if (field[0] != 0)
     {
     int verify = 1;
-    spool_directory = argv[1];
 
-    if (!(dbm = dbfn_open(argv[2], O_RDWR, &dbblock, FALSE, TRUE)))
+    if (!(dbm = dbfn_open(aname, O_RDWR, &dbblock, FALSE, TRUE)))
       continue;
 
     if (Ustrcmp(field, "d") == 0)
@@ -992,8 +1037,7 @@ for(; (reset_point = store_mark()); store_reset(reset_point))
 
   /* Handle a read request, or verify after an update. */
 
-  spool_directory = argv[1];
-  if (!(dbm = dbfn_open(argv[2], O_RDONLY, &dbblock, FALSE, TRUE)))
+  if (!(dbm = dbfn_open(aname, O_RDONLY, &dbblock, FALSE, TRUE)))
     continue;
 
   if (!(record = dbfn_read_with_length(dbm, name, &oldlength)))
@@ -1123,7 +1167,8 @@ typedef struct key_item {
 } key_item;
 
 
-int main(int argc, char **cargv)
+int
+main(int argc, char **cargv)
 {
 struct stat statbuf;
 int maxkeep = 30 * 24 * 60 * 60;
@@ -1137,6 +1182,8 @@ uschar **argv = USS cargv;
 uschar buffer[256];
 uschar *key;
 
+store_init();
+
 /* Scan the options */
 
 for (i = 1; i < argc; i++)
@@ -1222,7 +1269,7 @@ for (; keychain && (reset_point = store_mark()); store_reset(reset_point))
   /* A continuation record may have been deleted or renamed already, so
   non-existence is not serious. */
 
-  if (value == NULL) continue;
+  if (!value) continue;
 
   /* Delete if too old */
 
@@ -1243,12 +1290,33 @@ for (; keychain && (reset_point = store_mark()); store_reset(reset_point))
 
     /* Leave corrupt records alone */
 
+    if (wait->time_stamp > time(NULL))
+      {
+      printf("**** Data for '%s' corrupted\n  time in future: %s\n",
+        key, print_time(((dbdata_generic *)value)->time_stamp));
+      continue;
+      }
     if (wait->count > WAIT_NAME_MAX)
       {
-      printf("**** Data for %s corrupted\n  count=%d=0x%x max=%d\n",
+      printf("**** Data for '%s' corrupted\n  count=%d=0x%x max=%d\n",
         key, wait->count, wait->count, WAIT_NAME_MAX);
       continue;
       }
+    if (wait->sequence > WAIT_CONT_MAX)
+      {
+      printf("**** Data for '%s' corrupted\n  sequence=%d=0x%x max=%d\n",
+        key, wait->sequence, wait->sequence, WAIT_CONT_MAX);
+      continue;
+      }
+
+    /* Record over 1 year old; just remove it */
+
+    if (wait->time_stamp < time(NULL) - 365*24*60*60)
+      {
+      dbfn_delete(dbm, key);
+      printf("deleted %s (too old)\n", key);
+      continue;
+      }
 
     /* Loop for renamed continuation records. For each message id,
     check to see if the message exists, and if not, remove its entry