1 /* $Cambridge: exim/src/src/exim_dbutil.c,v 1.7 2005/06/27 14:29:43 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2005 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 /* This single source file is used to compile three utility programs for
12 maintaining Exim hints databases.
14 exim_dumpdb dumps out the contents
15 exim_fixdb patches the database (really for Exim maintenance/testing)
16 exim_tidydb removed obsolete data
18 In all cases, the first argument is the name of the spool directory. The second
19 argument is the name of the database file. The available names are:
21 retry: retry delivery information
22 misc: miscellaneous hints data
23 wait-<t>: message waiting information; <t> is a transport name
24 callout: callout verification cache
26 There are a number of common subroutines, followed by three main programs,
27 whose inclusion is controlled by -D on the compilation command. */
30 /* Standard C headers and Unix headers */
46 /* These are two values from macros.h which should perhaps be accessible in
47 some better way than just repeating them here. */
49 #define WAIT_NAME_MAX 50
50 #define MESSAGE_ID_LENGTH 16
53 /* This selection of Exim headers contains exactly what we need, and hopefully
54 not too much extra baggage. */
56 #include "config.h" /* Needed to get the DB type */
60 #include "osfunctions.h"
64 /* Identifiers for the different database types. */
69 #define type_callout 4
70 #define type_ratelimit 5
75 /*************************************************
76 * Berkeley DB error callback *
77 *************************************************/
79 /* For Berkeley DB >= 2, we can define a function to be called in case of DB
80 errors. This should help with debugging strange DB problems, e.g. getting "File
81 exists" when you try to open a db file. The API changed at release 4.3. */
83 #if defined(USE_DB) && defined(DB_VERSION_STRING)
85 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
86 dbfn_bdb_error_callback(const DB_ENV *dbenv, const char *pfx, const char *msg)
90 dbfn_bdb_error_callback(const char *pfx, char *msg)
94 printf("Berkeley DB error: %s\n", msg);
100 /*************************************************
102 *************************************************/
104 static int sigalrm_seen;
107 sigalrm_handler(int sig)
109 sig = sig; /* Keep picky compilers happy */
115 /*************************************************
116 * Output usage message and exit *
117 *************************************************/
120 usage(uschar *name, uschar *options)
122 printf("Usage: exim_%s%s <spool-directory> <database-name>\n", name, options);
123 printf(" <database-name> = retry | misc | wait-<transport-name> | callout | ratelimit\n");
129 /*************************************************
130 * Sort out the command arguments *
131 *************************************************/
133 /* This function checks that there are exactly 2 arguments, and checks the
134 second of them to be sure it is a known database name. */
137 check_args(int argc, uschar **argv, uschar *name, uschar *options)
141 if (Ustrcmp(argv[2], "retry") == 0) return type_retry;
142 if (Ustrcmp(argv[2], "misc") == 0) return type_misc;
143 if (Ustrncmp(argv[2], "wait-", 5) == 0) return type_wait;
144 if (Ustrcmp(argv[2], "callout") == 0) return type_callout;
145 if (Ustrcmp(argv[2], "ratelimit") == 0) return type_ratelimit;
147 usage(name, options);
148 return -1; /* Never obeyed */
153 /*************************************************
154 * Handle attempts to write the log *
155 *************************************************/
157 /* The message gets written to stderr when log_write() is called from a
158 utility. The message always gets '\n' added on the end of it. These calls come
159 from modules such as store.c when things go drastically wrong (e.g. malloc()
160 failing). In normal use they won't get obeyed.
163 selector not relevant when running a utility
164 flags not relevant when running a utility
165 format a printf() format
166 ... arguments for format
172 log_write(unsigned int selector, int flags, char *format, ...)
175 va_start(ap, format);
176 vfprintf(stderr, format, ap);
177 fprintf(stderr, "\n");
179 selector = selector; /* Keep picky compilers happy */
185 /*************************************************
186 * Format a time value for printing *
187 *************************************************/
189 static uschar time_buffer[sizeof("09-xxx-1999 hh:mm:ss ")];
194 struct tm *tmstr = localtime(&t);
195 Ustrftime(time_buffer, sizeof(time_buffer), "%d-%b-%Y %H:%M:%S", tmstr);
201 /*************************************************
202 * Format a cache value for printing *
203 *************************************************/
206 print_cache(int value)
208 return (value == ccache_accept)? US"accept" :
209 (value == ccache_reject)? US"reject" :
215 /*************************************************
217 *************************************************/
225 time_t now = time(NULL);
226 struct tm *tm = localtime(&now);
231 for (t = s + Ustrlen(s) - 1; t >= s; t--)
233 if (*t == ':') continue;
234 if (!isdigit((uschar)*t)) return -1;
239 if (!isdigit((uschar)*t)) return -1;
240 value = value + (*t - '0')*10;
245 case 0: tm->tm_min = value; break;
246 case 1: tm->tm_hour = value; break;
247 case 2: tm->tm_mday = value; break;
248 case 3: tm->tm_mon = value - 1; break;
249 case 4: tm->tm_year = (value < 90)? value + 100 : value; break;
256 #endif /* EXIM_FIXDB */
260 /*************************************************
261 * Open and lock a database file *
262 *************************************************/
264 /* This is a cut-down version from the function in dbfn.h that Exim itself
265 uses. We assume the database exists, and therefore give up if we cannot open
269 spool The spool directory
270 name The single-component name of one of Exim's database files.
271 flags O_RDONLY or O_RDWR
272 dbblock Points to an open_db block to be filled in.
274 Returns: NULL if the open failed, or the locking failed.
275 On success, dbblock is returned. This contains the dbm pointer and
276 the fd of the locked lock file.
280 dbfn_open(uschar *spool, uschar *name, int flags, open_db *dbblock)
283 struct flock lock_data;
284 BOOL read_only = flags == O_RDONLY;
287 /* The first thing to do is to open a separate file on which to lock. This
288 ensures that Exim has exclusive use of the database before it even tries to
289 open it. If there is a database, there should be a lock file in existence. */
291 sprintf(CS buffer, "%s/db/%s.lockfile", spool, name);
293 dbblock->lockfd = Uopen(buffer, flags, 0);
294 if (dbblock->lockfd < 0)
296 printf("** Failed to open database lock file %s: %s\n", buffer,
301 /* Now we must get a lock on the opened lock file; do this with a blocking
302 lock that times out. */
304 lock_data.l_type = read_only? F_RDLCK : F_WRLCK;
305 lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
307 sigalrm_seen = FALSE;
308 os_non_restarting_signal(SIGALRM, sigalrm_handler);
309 alarm(EXIMDB_LOCK_TIMEOUT);
310 rc = fcntl(dbblock->lockfd, F_SETLKW, &lock_data);
313 if (sigalrm_seen) errno = ETIMEDOUT;
316 printf("** Failed to get %s lock for %s: %s",
317 ((flags & O_RDONLY) != 0)? "read" : "write", buffer,
318 (errno == ETIMEDOUT)? "timed out" : strerror(errno));
319 (void)close(dbblock->lockfd);
323 /* At this point we have an opened and locked separate lock file, that is,
324 exclusive access to the database, so we can go ahead and open it. */
326 sprintf(CS buffer, "%s/db/%s", spool, name);
327 EXIM_DBOPEN(buffer, flags, 0, &(dbblock->dbptr));
329 if (dbblock->dbptr == NULL)
331 printf("** Failed to open DBM file %s for %s:\n %s%s\n", buffer,
332 read_only? "reading" : "writing", strerror(errno),
334 " (or Berkeley DB error while opening)"
339 (void)close(dbblock->lockfd);
349 /*************************************************
350 * Unlock and close a database file *
351 *************************************************/
353 /* Closing a file automatically unlocks it, so after closing the database, just
356 Argument: a pointer to an open database block
361 dbfn_close(open_db *dbblock)
363 EXIM_DBCLOSE(dbblock->dbptr);
364 (void)close(dbblock->lockfd);
370 /*************************************************
371 * Read from database file *
372 *************************************************/
374 /* Passing back the pointer unchanged is useless, because there is no guarantee
375 of alignment. Since all the records used by Exim need to be properly aligned to
376 pick out the timestamps, etc., do the copying centrally here.
379 dbblock a pointer to an open database block
380 key the key of the record to be read
381 length where to put the length (or NULL if length not wanted)
383 Returns: a pointer to the retrieved record, or
384 NULL if the record is not found
388 dbfn_read_with_length(open_db *dbblock, uschar *key, int *length)
391 EXIM_DATUM key_datum, result_datum;
393 EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */
394 EXIM_DATUM_INIT(result_datum); /* to be cleared before use. */
395 EXIM_DATUM_DATA(key_datum) = CS key;
396 EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
398 if (!EXIM_DBGET(dbblock->dbptr, key_datum, result_datum)) return NULL;
400 yield = store_get(EXIM_DATUM_SIZE(result_datum));
401 memcpy(yield, EXIM_DATUM_DATA(result_datum), EXIM_DATUM_SIZE(result_datum));
402 if (length != NULL) *length = EXIM_DATUM_SIZE(result_datum);
404 EXIM_DATUM_FREE(result_datum); /* Some DBM libs require freeing */
410 #if defined(EXIM_TIDYDB) || defined(EXIM_FIXDB)
412 /*************************************************
413 * Write to database file *
414 *************************************************/
418 dbblock a pointer to an open database block
419 key the key of the record to be written
420 ptr a pointer to the record to be written
421 length the length of the record to be written
423 Returns: the yield of the underlying dbm or db "write" function. If this
424 is dbm, the value is zero for OK.
428 dbfn_write(open_db *dbblock, uschar *key, void *ptr, int length)
430 EXIM_DATUM key_datum, value_datum;
431 dbdata_generic *gptr = (dbdata_generic *)ptr;
432 gptr->time_stamp = time(NULL);
434 EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */
435 EXIM_DATUM_INIT(value_datum); /* to be cleared before use. */
436 EXIM_DATUM_DATA(key_datum) = CS key;
437 EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
438 EXIM_DATUM_DATA(value_datum) = CS ptr;
439 EXIM_DATUM_SIZE(value_datum) = length;
440 return EXIM_DBPUT(dbblock->dbptr, key_datum, value_datum);
445 /*************************************************
446 * Delete record from database file *
447 *************************************************/
451 dbblock a pointer to an open database block
452 key the key of the record to be deleted
454 Returns: the yield of the underlying dbm or db "delete" function.
458 dbfn_delete(open_db *dbblock, uschar *key)
460 EXIM_DATUM key_datum;
461 EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require clearing */
462 EXIM_DATUM_DATA(key_datum) = CS key;
463 EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1;
464 return EXIM_DBDEL(dbblock->dbptr, key_datum);
467 #endif /* EXIM_TIDYDB || EXIM_FIXDB */
471 #if defined(EXIM_DUMPDB) || defined(EXIM_TIDYDB)
472 /*************************************************
473 * Scan the keys of a database file *
474 *************************************************/
478 dbblock a pointer to an open database block
479 start TRUE if starting a new scan
480 FALSE if continuing with the current scan
481 cursor a pointer to a pointer to a cursor anchor, for those dbm libraries
482 that use the notion of a cursor
484 Returns: the next record from the file, or
485 NULL if there are no more
489 dbfn_scan(open_db *dbblock, BOOL start, EXIM_CURSOR **cursor)
491 EXIM_DATUM key_datum, value_datum;
493 value_datum = value_datum; /* dummy; not all db libraries use this */
495 /* Some dbm require an initialization */
497 if (start) EXIM_DBCREATE_CURSOR(dbblock->dbptr, cursor);
499 EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */
500 EXIM_DATUM_INIT(value_datum); /* to be cleared before use. */
502 yield = (EXIM_DBSCAN(dbblock->dbptr, key_datum, value_datum, start, *cursor))?
503 US EXIM_DATUM_DATA(key_datum) : NULL;
505 /* Some dbm require a termination */
507 if (!yield) EXIM_DBDELETE_CURSOR(*cursor);
510 #endif /* EXIM_DUMPDB || EXIM_TIDYDB */
515 /*************************************************
516 * The exim_dumpdb main program *
517 *************************************************/
520 main(int argc, char **cargv)
527 uschar **argv = USS cargv;
529 uschar keybuffer[1024];
531 /* Check the arguments, and open the database */
533 dbdata_type = check_args(argc, argv, US"dumpdb", US"");
534 dbm = dbfn_open(argv[1], argv[2], O_RDONLY, &dbblock);
535 if (dbm == NULL) exit(1);
537 /* Scan the file, formatting the information for each entry. Note
538 that data is returned in a malloc'ed block, in order that it be
539 correctly aligned. */
541 key = dbfn_scan(dbm, TRUE, &cursor);
546 dbdata_callout_cache *callout;
547 dbdata_ratelimit *ratelimit;
551 uschar name[MESSAGE_ID_LENGTH + 1];
554 /* Keep a copy of the key separate, as in some DBM's the pointer is into data
555 which might change. */
557 if (Ustrlen(key) > sizeof(keybuffer) - 1)
559 printf("**** Overlong key encountered: %s\n", key);
562 Ustrcpy(keybuffer, key);
563 value = dbfn_read_with_length(dbm, keybuffer, &length);
566 fprintf(stderr, "**** Entry \"%s\" was in the key scan, but the record "
567 "was not found in the file - something is wrong!\n",
571 /* Note: don't use print_time more than once in one statement, since
572 it uses a single buffer. */
577 retry = (dbdata_retry *)value;
578 printf(" %s %d %d %s\n%s ", keybuffer, retry->basic_errno,
579 retry->more_errno, retry->text,
580 print_time(retry->first_failed));
581 printf("%s ", print_time(retry->last_try));
582 printf("%s %s\n", print_time(retry->next_try),
583 (retry->expired)? "*" : "");
587 wait = (dbdata_wait *)value;
588 printf("%s ", keybuffer);
590 name[MESSAGE_ID_LENGTH] = 0;
592 if (wait->count > WAIT_NAME_MAX)
595 "**** Data for %s corrupted\n count=%d=0x%x max=%d\n",
596 CS keybuffer, wait->count, wait->count, WAIT_NAME_MAX);
597 wait->count = WAIT_NAME_MAX;
598 yield = count_bad = 1;
600 for (i = 1; i <= wait->count; i++)
602 Ustrncpy(name, t, MESSAGE_ID_LENGTH);
603 if (count_bad && name[0] == 0) break;
604 if (Ustrlen(name) != MESSAGE_ID_LENGTH ||
605 Ustrspn(name, "0123456789"
606 "abcdefghijklmnopqrstuvwxyz"
607 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-") != MESSAGE_ID_LENGTH)
611 "**** Data for %s corrupted: bad character in message id\n",
613 for (j = 0; j < MESSAGE_ID_LENGTH; j++)
614 fprintf(stderr, "%02x ", name[j]);
615 fprintf(stderr, "\n");
620 t += MESSAGE_ID_LENGTH;
626 printf("%s %s\n", print_time(((dbdata_generic *)value)->time_stamp),
631 callout = (dbdata_callout_cache *)value;
633 /* New-style address record */
635 if (length == sizeof(dbdata_callout_cache_address))
637 printf("%s %s callout=%s\n",
638 print_time(((dbdata_generic *)value)->time_stamp),
640 print_cache(callout->result));
643 /* New-style domain record */
645 else if (length == sizeof(dbdata_callout_cache))
647 printf("%s %s callout=%s postmaster=%s",
648 print_time(((dbdata_generic *)value)->time_stamp),
650 print_cache(callout->result),
651 print_cache(callout->postmaster_result));
652 if (callout->postmaster_result != ccache_unknown)
653 printf(" (%s)", print_time(callout->postmaster_stamp));
654 printf(" random=%s", print_cache(callout->random_result));
655 if (callout->random_result != ccache_unknown)
656 printf(" (%s)", print_time(callout->random_stamp));
660 /* Old-style domain record, without separate timestamps. This code can
661 eventually be thrown away, say in 5 years' time (it's now Feb 2003). */
665 printf("%s %s callout=%s postmaster=%s random=%s\n",
666 print_time(((dbdata_generic *)value)->time_stamp),
668 print_cache(callout->result),
669 print_cache(callout->postmaster_result),
670 print_cache(callout->random_result));
676 ratelimit = (dbdata_ratelimit *)value;
678 printf("%s.%06d rate: %10.3f key: %s\n",
679 print_time(ratelimit->time_stamp), ratelimit->time_usec,
680 ratelimit->rate, keybuffer);
686 key = dbfn_scan(dbm, FALSE, &cursor);
693 #endif /* EXIM_DUMPDB */
699 /*************************************************
700 * The exim_fixdb main program *
701 *************************************************/
703 /* In order not to hold the database lock any longer than is necessary, each
704 operation on the database uses a separate open/close call. This is expensive,
705 but then using this utility is not expected to be very common. Its main use is
706 to provide a way of patching up hints databases in order to run tests.
711 This causes the data from the given record to be displayed, or "not found"
712 to be output. Note that in the retry database, destination names are
713 preceded by R: or T: for router or transport retry info.
716 This causes the given record to be deleted or "not found" to be output.
718 (3) <record name> <field number> <value>
719 This sets the given value into the given field, identified by a number
720 which is output by the display command. Not all types of record can
724 This exits from exim_fixdb.
726 If the record name is omitted from (2) or (3), the previously used record name
730 int main(int argc, char **cargv)
733 uschar **argv = USS cargv;
736 void *reset_point = store_get(0);
738 name[0] = 0; /* No name set */
740 /* Sort out the database type, verify what we are working on and then process
743 dbdata_type = check_args(argc, argv, US"fixdb", US"");
744 printf("Modifying Exim hints database %s/db/%s\n", argv[1], argv[2]);
753 dbdata_callout_cache *callout;
754 dbdata_ratelimit *ratelimit;
757 uschar field[256], value[256];
759 store_reset(reset_point);
762 if (Ufgets(buffer, 256, stdin) == NULL) break;
764 buffer[Ustrlen(buffer)-1] = 0;
765 field[0] = value[0] = 0;
767 /* If the buffer contains just one digit, or just consists of "d", use the
768 previous name for an update. */
770 if ((isdigit((uschar)buffer[0]) && !isdigit((uschar)buffer[1])) ||
771 Ustrcmp(buffer, "d") == 0)
775 printf("No previous record name is set\n");
778 (void)sscanf(CS buffer, "%s %s", field, value);
783 (void)sscanf(CS buffer, "%s %s %s", name, field, value);
786 /* Handle an update request */
791 dbm = dbfn_open(argv[1], argv[2], O_RDWR, &dbblock);
792 if (dbm == NULL) continue;
794 if (Ustrcmp(field, "d") == 0)
796 if (value[0] != 0) printf("unexpected value after \"d\"\n");
797 else printf("%s\n", (dbfn_delete(dbm, name) < 0)?
798 "not found" : "deleted");
803 else if (isdigit((uschar)field[0]))
805 int fieldno = Uatoi(field);
808 printf("value missing\n");
814 record = dbfn_read_with_length(dbm, name, &oldlength);
815 if (record == NULL) printf("not found\n"); else
818 int length = 0; /* Stops compiler warning */
823 retry = (dbdata_retry *)record;
824 length = sizeof(dbdata_retry) + Ustrlen(retry->text);
829 retry->basic_errno = Uatoi(value);
833 retry->more_errno = Uatoi(value);
837 if ((tt = read_time(value)) > 0) retry->first_failed = tt;
838 else printf("bad time value\n");
842 if ((tt = read_time(value)) > 0) retry->last_try = tt;
843 else printf("bad time value\n");
847 if ((tt = read_time(value)) > 0) retry->next_try = tt;
848 else printf("bad time value\n");
852 if (Ustrcmp(value, "yes") == 0) retry->expired = TRUE;
853 else if (Ustrcmp(value, "no") == 0) retry->expired = FALSE;
854 else printf("\"yes\" or \"no\" expected=n");
858 printf("unknown field number\n");
865 printf("Can't change contents of wait database record\n");
869 printf("Can't change contents of misc database record\n");
873 callout = (dbdata_callout_cache *)record;
874 length = sizeof(dbdata_callout_cache);
878 callout->result = Uatoi(value);
882 callout->postmaster_result = Uatoi(value);
886 callout->random_result = Uatoi(value);
890 printf("unknown field number\n");
897 ratelimit = (dbdata_ratelimit *)value;
901 if ((tt = read_time(value)) > 0) ratelimit->time_stamp = tt;
902 else printf("bad time value\n");
906 ratelimit->time_usec = Uatoi(value);
909 ratelimit->rate = Ustrtod(value, NULL);
913 printf("unknown field number\n");
920 dbfn_write(dbm, name, record, length);
927 printf("field number or d expected\n");
932 if (!verify) continue;
935 /* The "name" q causes an exit */
937 else if (Ustrcmp(name, "q") == 0) return 0;
939 /* Handle a read request, or verify after an update. */
941 dbm = dbfn_open(argv[1], argv[2], O_RDONLY, &dbblock);
942 if (dbm == NULL) continue;
944 record = dbfn_read_with_length(dbm, name, &oldlength);
947 printf("record %s not found\n", name);
953 printf("%s\n", CS print_time(((dbdata_generic *)record)->time_stamp));
957 retry = (dbdata_retry *)record;
958 printf("0 error number: %d %s\n", retry->basic_errno, retry->text);
959 printf("1 extra data: %d\n", retry->more_errno);
960 printf("2 first failed: %s\n", print_time(retry->first_failed));
961 printf("3 last try: %s\n", print_time(retry->last_try));
962 printf("4 next try: %s\n", print_time(retry->next_try));
963 printf("5 expired: %s\n", (retry->expired)? "yes" : "no");
967 wait = (dbdata_wait *)record;
969 printf("Sequence: %d\n", wait->sequence);
970 if (wait->count > WAIT_NAME_MAX)
972 printf("**** Data corrupted: count=%d=0x%x max=%d ****\n", wait->count,
973 wait->count, WAIT_NAME_MAX);
974 wait->count = WAIT_NAME_MAX;
977 for (i = 1; i <= wait->count; i++)
979 Ustrncpy(value, t, MESSAGE_ID_LENGTH);
980 value[MESSAGE_ID_LENGTH] = 0;
981 if (count_bad && value[0] == 0) break;
982 if (Ustrlen(value) != MESSAGE_ID_LENGTH ||
983 Ustrspn(value, "0123456789"
984 "abcdefghijklmnopqrstuvwxyz"
985 "ABCDEFGHIJKLMNOPQRSTUVWXYZ-") != MESSAGE_ID_LENGTH)
988 printf("\n**** Data corrupted: bad character in message id ****\n");
989 for (j = 0; j < MESSAGE_ID_LENGTH; j++)
990 printf("%02x ", value[j]);
994 printf("%s ", value);
995 t += MESSAGE_ID_LENGTH;
1004 callout = (dbdata_callout_cache *)record;
1005 printf("0 callout: %s (%d)\n", print_cache(callout->result),
1007 if (oldlength > sizeof(dbdata_callout_cache_address))
1009 printf("1 postmaster: %s (%d)\n", print_cache(callout->postmaster_result),
1010 callout->postmaster_result);
1011 printf("2 random: %s (%d)\n", print_cache(callout->random_result),
1012 callout->random_result);
1016 case type_ratelimit:
1017 ratelimit = (dbdata_ratelimit *)value;
1018 printf("0 time stamp: %s\n", print_time(ratelimit->time_stamp));
1019 printf("1 fract. time: .%06d\n", ratelimit->time_usec);
1020 printf("2 sender rate: % .3f\n", ratelimit->rate);
1025 /* The database is closed after each request */
1034 #endif /* EXIM_FIXDB */
1039 /*************************************************
1040 * The exim_tidydb main program *
1041 *************************************************/
1044 /* Utility program to tidy the contents of an exim database file. There is one
1047 -t <time> expiry time for old records - default 30 days
1049 For backwards compatibility, an -f option is recognized and ignored. (It used
1050 to request a "full" tidy. This version always does the whole job.) */
1053 typedef struct key_item {
1054 struct key_item *next;
1059 int main(int argc, char **cargv)
1061 struct stat statbuf;
1062 int maxkeep = 30 * 24 * 60 * 60;
1063 int dbdata_type, i, oldest, path_len;
1064 key_item *keychain = NULL;
1068 EXIM_CURSOR *cursor;
1069 uschar **argv = USS cargv;
1073 /* Scan the options */
1075 for (i = 1; i < argc; i++)
1077 if (argv[i][0] != '-') break;
1078 if (Ustrcmp(argv[i], "-f") == 0) continue;
1079 if (Ustrcmp(argv[i], "-t") == 0)
1087 if (!isdigit(*s)) usage(US"tidydb", US" [-t <time>]");
1088 (void)sscanf(CS s, "%d%n", &value, &count);
1092 case 'w': value *= 7;
1093 case 'd': value *= 24;
1094 case 'h': value *= 60;
1095 case 'm': value *= 60;
1098 default: usage(US"tidydb", US" [-t <time>]");
1103 else usage(US"tidydb", US" [-t <time>]");
1106 /* Adjust argument values and process arguments */
1111 dbdata_type = check_args(argc, argv, US"tidydb", US" [-t <time>]");
1113 /* Compute the oldest keep time, verify what we are doing, and open the
1116 oldest = time(NULL) - maxkeep;
1117 printf("Tidying Exim hints database %s/db/%s\n", argv[1], argv[2]);
1119 dbm = dbfn_open(argv[1], argv[2], O_RDWR, &dbblock);
1120 if (dbm == NULL) exit(1);
1122 /* Prepare for building file names */
1124 sprintf(CS buffer, "%s/input/", argv[1]);
1125 path_len = Ustrlen(buffer);
1128 /* It appears, by experiment, that it is a bad idea to make changes
1129 to the file while scanning it. Pity the man page doesn't warn you about that.
1130 Therefore, we scan and build a list of all the keys. Then we use that to
1131 read the records and possibly update them. */
1133 key = dbfn_scan(dbm, TRUE, &cursor);
1136 key_item *k = store_get(sizeof(key_item) + Ustrlen(key));
1139 Ustrcpy(k->key, key);
1140 key = dbfn_scan(dbm, FALSE, &cursor);
1143 /* Now scan the collected keys and operate on the records, resetting
1144 the store each time round. */
1146 reset_point = store_get(0);
1148 while (keychain != NULL)
1150 dbdata_generic *value;
1152 store_reset(reset_point);
1153 key = keychain->key;
1154 keychain = keychain->next;
1155 value = dbfn_read_with_length(dbm, key, NULL);
1157 /* A continuation record may have been deleted or renamed already, so
1158 non-existence is not serious. */
1160 if (value == NULL) continue;
1162 /* Delete if too old */
1164 if (value->time_stamp < oldest)
1166 printf("deleted %s (too old)\n", key);
1167 dbfn_delete(dbm, key);
1171 /* Do database-specific tidying for wait databases, and message-
1172 specific tidying for the retry database. */
1174 if (dbdata_type == type_wait)
1176 dbdata_wait *wait = (dbdata_wait *)value;
1177 BOOL update = FALSE;
1179 /* Leave corrupt records alone */
1181 if (wait->count > WAIT_NAME_MAX)
1183 printf("**** Data for %s corrupted\n count=%d=0x%x max=%d\n",
1184 key, wait->count, wait->count, WAIT_NAME_MAX);
1188 /* Loop for renamed continuation records. For each message id,
1189 check to see if the message exists, and if not, remove its entry
1190 from the record. Because of the possibility of split input directories,
1191 we must look in both possible places for a -D file. */
1196 int length = wait->count * MESSAGE_ID_LENGTH;
1198 for (offset = length - MESSAGE_ID_LENGTH;
1199 offset >= 0; offset -= MESSAGE_ID_LENGTH)
1201 Ustrncpy(buffer+path_len, wait->text + offset, MESSAGE_ID_LENGTH);
1202 sprintf(CS(buffer+path_len + MESSAGE_ID_LENGTH), "-D");
1204 if (Ustat(buffer, &statbuf) != 0)
1206 buffer[path_len] = wait->text[offset+5];
1207 buffer[path_len+1] = '/';
1208 Ustrncpy(buffer+path_len+2, wait->text + offset, MESSAGE_ID_LENGTH);
1209 sprintf(CS(buffer+path_len+2 + MESSAGE_ID_LENGTH), "-D");
1211 if (Ustat(buffer, &statbuf) != 0)
1213 int left = length - offset - MESSAGE_ID_LENGTH;
1214 if (left > 0) Ustrncpy(wait->text + offset,
1215 wait->text + offset + MESSAGE_ID_LENGTH, left);
1217 length -= MESSAGE_ID_LENGTH;
1223 /* If record is empty and the main record, either delete it or rename
1224 the next continuation, repeating if that is also empty. */
1226 if (wait->count == 0 && Ustrchr(key, ':') == NULL)
1228 while (wait->count == 0 && wait->sequence > 0)
1231 dbdata_generic *newvalue;
1232 sprintf(CS newkey, "%s:%d", key, wait->sequence - 1);
1233 newvalue = dbfn_read_with_length(dbm, newkey, NULL);
1234 if (newvalue != NULL)
1237 wait = (dbdata_wait *)newvalue;
1238 dbfn_delete(dbm, newkey);
1239 printf("renamed %s\n", newkey);
1242 else wait->sequence--;
1245 /* If we have ended up with an empty main record, delete it
1246 and break the loop. Otherwise the new record will be scanned. */
1248 if (wait->count == 0 && wait->sequence == 0)
1250 dbfn_delete(dbm, key);
1251 printf("deleted %s (empty)\n", key);
1257 /* If not an empty main record, break the loop */
1262 /* Re-write the record if required */
1266 printf("updated %s\n", key);
1267 dbfn_write(dbm, key, wait, sizeof(dbdata_wait) +
1268 wait->count * MESSAGE_ID_LENGTH);
1272 /* If a retry record's key ends with a message-id, check that that message
1273 still exists; if not, remove this record. */
1275 else if (dbdata_type == type_retry)
1278 int len = Ustrlen(key);
1280 if (len < MESSAGE_ID_LENGTH + 1) continue;
1281 id = key + len - MESSAGE_ID_LENGTH - 1;
1282 if (*id++ != ':') continue;
1284 for (i = 0; i < MESSAGE_ID_LENGTH; i++)
1286 if (i == 6 || i == 13)
1287 { if (id[i] != '-') break; }
1289 { if (!isalnum(id[i])) break; }
1291 if (i < MESSAGE_ID_LENGTH) continue;
1293 Ustrncpy(buffer + path_len, id, MESSAGE_ID_LENGTH);
1294 sprintf(CS(buffer + path_len + MESSAGE_ID_LENGTH), "-D");
1296 if (Ustat(buffer, &statbuf) != 0)
1298 sprintf(CS(buffer + path_len), "%c/%s-D", id[5], id);
1299 if (Ustat(buffer, &statbuf) != 0)
1301 dbfn_delete(dbm, key);
1302 printf("deleted %s (no message)\n", key);
1309 printf("Tidying complete\n");
1313 #endif /* EXIM_TIDYDB */
1315 /* End of exim_dbutil.c */