CVE-2020-28008: Assorted attacks in Exim's spool directory
[exim.git] / src / src / dbfn.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9
10 #include "exim.h"
11
12 /* We have buffers holding path names for database files.
13 PATH_MAX could be used here, but would be wasting memory, as we deal
14 with database files like $spooldirectory/db/<name> */
15 #define PATHLEN 256
16
17
18 /* Functions for accessing Exim's hints database, which consists of a number of
19 different DBM files. This module does not contain code for reading DBM files
20 for (e.g.) alias expansion. That is all contained within the general search
21 functions. As Exim now has support for several DBM interfaces, all the relevant
22 functions are called as macros.
23
24 All the data in Exim's database is in the nature of *hints*. Therefore it
25 doesn't matter if it gets destroyed by accident. These functions are not
26 supposed to implement a "safe" database.
27
28 Keys are passed in as C strings, and the terminating zero *is* used when
29 building the dbm files. This just makes life easier when scanning the files
30 sequentially.
31
32 Synchronization is required on the database files, and this is achieved by
33 means of locking on independent lock files. (Earlier attempts to lock on the
34 DBM files themselves were never completely successful.) Since callers may in
35 general want to do more than one read or write while holding the lock, there
36 are separate open and close functions. However, the calling modules should
37 arrange to hold the locks for the bare minimum of time. */
38
39
40
41 /*************************************************
42 *         Berkeley DB error callback             *
43 *************************************************/
44
45 /* For Berkeley DB >= 2, we can define a function to be called in case of DB
46 errors. This should help with debugging strange DB problems, e.g. getting "File
47 exists" when you try to open a db file. The API for this function was changed
48 at DB release 4.3. */
49
50 #if defined(USE_DB) && defined(DB_VERSION_STRING)
51 void
52 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
53 dbfn_bdb_error_callback(const DB_ENV *dbenv, const char *pfx, const char *msg)
54 {
55 dbenv = dbenv;
56 #else
57 dbfn_bdb_error_callback(const char *pfx, char *msg)
58 {
59 #endif
60 pfx = pfx;
61 log_write(0, LOG_MAIN, "Berkeley DB error: %s", msg);
62 }
63 #endif
64
65
66
67
68 static enum {
69   PRIV_DROPPING, PRIV_DROPPED,
70   PRIV_RESTORING, PRIV_RESTORED
71 } priv_state = PRIV_RESTORED;
72
73 static uid_t priv_euid;
74 static gid_t priv_egid;
75 static gid_t priv_groups[EXIM_GROUPLIST_SIZE + 1];
76 static int priv_ngroups;
77
78 /* Inspired by OpenSSH's temporarily_use_uid(). Thanks! */
79
80 static void
81 priv_drop_temp(const uid_t temp_uid, const gid_t temp_gid)
82 {
83 if (priv_state != PRIV_RESTORED) _exit(EXIT_FAILURE);
84 priv_state = PRIV_DROPPING;
85
86 priv_euid = geteuid();
87 if (priv_euid == root_uid)
88   {
89   priv_egid = getegid();
90   priv_ngroups = getgroups(nelem(priv_groups), priv_groups);
91   if (priv_ngroups < 0) _exit(EXIT_FAILURE);
92
93   if (priv_ngroups > 0 && setgroups(1, &temp_gid) != 0) _exit(EXIT_FAILURE);
94   if (setegid(temp_gid) != 0) _exit(EXIT_FAILURE);
95   if (seteuid(temp_uid) != 0) _exit(EXIT_FAILURE);
96
97   if (geteuid() != temp_uid) _exit(EXIT_FAILURE);
98   if (getegid() != temp_gid) _exit(EXIT_FAILURE);
99   }
100
101 priv_state = PRIV_DROPPED;
102 }
103
104 /* Inspired by OpenSSH's restore_uid(). Thanks! */
105
106 static void
107 priv_restore(void)
108 {
109 if (priv_state != PRIV_DROPPED) _exit(EXIT_FAILURE);
110 priv_state = PRIV_RESTORING;
111
112 if (priv_euid == root_uid)
113   {
114   if (seteuid(priv_euid) != 0) _exit(EXIT_FAILURE);
115   if (setegid(priv_egid) != 0) _exit(EXIT_FAILURE);
116   if (priv_ngroups > 0 && setgroups(priv_ngroups, priv_groups) != 0) _exit(EXIT_FAILURE);
117
118   if (geteuid() != priv_euid) _exit(EXIT_FAILURE);
119   if (getegid() != priv_egid) _exit(EXIT_FAILURE);
120   }
121
122 priv_state = PRIV_RESTORED;
123 }
124
125
126
127
128 /*************************************************
129 *          Open and lock a database file         *
130 *************************************************/
131
132 /* Used for accessing Exim's hints databases.
133
134 Arguments:
135   name     The single-component name of one of Exim's database files.
136   flags    Either O_RDONLY or O_RDWR, indicating the type of open required;
137              O_RDWR implies "create if necessary"
138   dbblock  Points to an open_db block to be filled in.
139   lof      If TRUE, write to the log for actual open failures (locking failures
140            are always logged).
141   panic    If TRUE, panic on failure to create the db directory
142
143 Returns:   NULL if the open failed, or the locking failed. After locking
144            failures, errno is zero.
145
146            On success, dbblock is returned. This contains the dbm pointer and
147            the fd of the locked lock file.
148
149 There are some calls that use O_RDWR|O_CREAT for the flags. Having discovered
150 this in December 2005, I'm not sure if this is correct or not, but for the
151 moment I haven't changed them.
152 */
153
154 open_db *
155 dbfn_open(uschar *name, int flags, open_db *dbblock, BOOL lof, BOOL panic)
156 {
157 int rc, save_errno;
158 BOOL read_only = flags == O_RDONLY;
159 flock_t lock_data;
160 uschar dirname[PATHLEN], filename[PATHLEN];
161
162 DEBUG(D_hints_lookup) acl_level++;
163
164 /* The first thing to do is to open a separate file on which to lock. This
165 ensures that Exim has exclusive use of the database before it even tries to
166 open it. Early versions tried to lock on the open database itself, but that
167 gave rise to mysterious problems from time to time - it was suspected that some
168 DB libraries "do things" on their open() calls which break the interlocking.
169 The lock file is never written to, but we open it for writing so we can get a
170 write lock if required. If it does not exist, we create it. This is done
171 separately so we know when we have done it, because when running as root we
172 need to change the ownership - see the bottom of this function. We also try to
173 make the directory as well, just in case. We won't be doing this many times
174 unnecessarily, because usually the lock file will be there. If the directory
175 exists, there is no error. */
176
177 snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
178 snprintf(CS filename, sizeof(filename), "%s/%s.lockfile", dirname, name);
179
180 priv_drop_temp(exim_uid, exim_gid);
181 if ((dbblock->lockfd = Uopen(filename, O_RDWR, EXIMDB_LOCKFILE_MODE)) < 0)
182   {
183   (void)directory_make(spool_directory, US"db", EXIMDB_DIRECTORY_MODE, panic);
184   dbblock->lockfd = Uopen(filename, O_RDWR|O_CREAT, EXIMDB_LOCKFILE_MODE);
185   }
186 priv_restore();
187
188 if (dbblock->lockfd < 0)
189   {
190   log_write(0, LOG_MAIN, "%s",
191     string_open_failed("database lock file %s", filename));
192   errno = 0;      /* Indicates locking failure */
193   DEBUG(D_hints_lookup) acl_level--;
194   return NULL;
195   }
196
197 /* Now we must get a lock on the opened lock file; do this with a blocking
198 lock that times out. */
199
200 lock_data.l_type = read_only? F_RDLCK : F_WRLCK;
201 lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0;
202
203 DEBUG(D_hints_lookup|D_retry|D_route|D_deliver)
204   debug_printf_indent("locking %s\n", filename);
205
206 sigalrm_seen = FALSE;
207 ALARM(EXIMDB_LOCK_TIMEOUT);
208 rc = fcntl(dbblock->lockfd, F_SETLKW, &lock_data);
209 ALARM_CLR(0);
210
211 if (sigalrm_seen) errno = ETIMEDOUT;
212 if (rc < 0)
213   {
214   log_write(0, LOG_MAIN|LOG_PANIC, "Failed to get %s lock for %s: %s",
215     read_only ? "read" : "write", filename,
216     errno == ETIMEDOUT ? "timed out" : strerror(errno));
217   (void)close(dbblock->lockfd);
218   errno = 0;       /* Indicates locking failure */
219   DEBUG(D_hints_lookup) acl_level--;
220   return NULL;
221   }
222
223 DEBUG(D_hints_lookup) debug_printf_indent("locked  %s\n", filename);
224
225 /* At this point we have an opened and locked separate lock file, that is,
226 exclusive access to the database, so we can go ahead and open it. If we are
227 expected to create it, don't do so at first, again so that we can detect
228 whether we need to change its ownership (see comments about the lock file
229 above.) There have been regular reports of crashes while opening hints
230 databases - often this is caused by non-matching db.h and the library. To make
231 it easy to pin this down, there are now debug statements on either side of the
232 open call. */
233
234 snprintf(CS filename, sizeof(filename), "%s/%s", dirname, name);
235
236 priv_drop_temp(exim_uid, exim_gid);
237 EXIM_DBOPEN(filename, dirname, flags, EXIMDB_MODE, &(dbblock->dbptr));
238 if (!dbblock->dbptr && errno == ENOENT && flags == O_RDWR)
239   {
240   DEBUG(D_hints_lookup)
241     debug_printf_indent("%s appears not to exist: trying to create\n", filename);
242   EXIM_DBOPEN(filename, dirname, flags|O_CREAT, EXIMDB_MODE, &(dbblock->dbptr));
243   }
244 save_errno = errno;
245 priv_restore();
246
247 /* If the open has failed, return NULL, leaving errno set. If lof is TRUE,
248 log the event - also for debugging - but debug only if the file just doesn't
249 exist. */
250
251 if (!dbblock->dbptr)
252   {
253   errno = save_errno;
254   if (lof && save_errno != ENOENT)
255     log_write(0, LOG_MAIN, "%s", string_open_failed("DB file %s",
256         filename));
257   else
258     DEBUG(D_hints_lookup)
259       debug_printf_indent("%s\n", CS string_open_failed("DB file %s",
260           filename));
261   (void)close(dbblock->lockfd);
262   errno = save_errno;
263   DEBUG(D_hints_lookup) acl_level--;
264   return NULL;
265   }
266
267 DEBUG(D_hints_lookup)
268   debug_printf_indent("opened hints database %s: flags=%s\n", filename,
269     flags == O_RDONLY ? "O_RDONLY"
270     : flags == O_RDWR ? "O_RDWR"
271     : flags == (O_RDWR|O_CREAT) ? "O_RDWR|O_CREAT"
272     : "??");
273
274 /* Pass back the block containing the opened database handle and the open fd
275 for the lock. */
276
277 return dbblock;
278 }
279
280
281
282
283 /*************************************************
284 *         Unlock and close a database file       *
285 *************************************************/
286
287 /* Closing a file automatically unlocks it, so after closing the database, just
288 close the lock file.
289
290 Argument: a pointer to an open database block
291 Returns:  nothing
292 */
293
294 void
295 dbfn_close(open_db *dbblock)
296 {
297 EXIM_DBCLOSE(dbblock->dbptr);
298 (void)close(dbblock->lockfd);
299 DEBUG(D_hints_lookup)
300   { debug_printf_indent("closed hints database and lockfile\n"); acl_level--; }
301 }
302
303
304
305
306 /*************************************************
307 *             Read from database file            *
308 *************************************************/
309
310 /* Passing back the pointer unchanged is useless, because there is
311 no guarantee of alignment. Since all the records used by Exim need
312 to be properly aligned to pick out the timestamps, etc., we might as
313 well do the copying centrally here.
314
315 Most calls don't need the length, so there is a macro called dbfn_read which
316 has two arguments; it calls this function adding NULL as the third.
317
318 Arguments:
319   dbblock   a pointer to an open database block
320   key       the key of the record to be read
321   length    a pointer to an int into which to return the length, if not NULL
322
323 Returns: a pointer to the retrieved record, or
324          NULL if the record is not found
325 */
326
327 void *
328 dbfn_read_with_length(open_db *dbblock, const uschar *key, int *length)
329 {
330 void *yield;
331 EXIM_DATUM key_datum, result_datum;
332 int klen = Ustrlen(key) + 1;
333 uschar * key_copy = store_get(klen, is_tainted(key));
334
335 memcpy(key_copy, key, klen);
336
337 DEBUG(D_hints_lookup) debug_printf_indent("dbfn_read: key=%s\n", key);
338
339 EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
340 EXIM_DATUM_INIT(result_datum);      /* to be cleared before use. */
341 EXIM_DATUM_DATA(key_datum) = CS key_copy;
342 EXIM_DATUM_SIZE(key_datum) = klen;
343
344 if (!EXIM_DBGET(dbblock->dbptr, key_datum, result_datum)) return NULL;
345
346 /* Assume the data store could have been tainted.  Properly, we should
347 store the taint status with the data. */
348
349 yield = store_get(EXIM_DATUM_SIZE(result_datum), TRUE);
350 memcpy(yield, EXIM_DATUM_DATA(result_datum), EXIM_DATUM_SIZE(result_datum));
351 if (length != NULL) *length = EXIM_DATUM_SIZE(result_datum);
352
353 EXIM_DATUM_FREE(result_datum);    /* Some DBM libs require freeing */
354 return yield;
355 }
356
357
358 /* Read a record.  If the length is not as expected then delete it, write
359 an error log line, delete the record and return NULL.
360 Use this for fixed-size records (so not retry or wait records).
361
362 Arguments:
363   dbblock   a pointer to an open database block
364   key       the key of the record to be read
365   length    the expected record length
366
367 Returns: a pointer to the retrieved record, or
368          NULL if the record is not found/bad
369 */
370
371 void *
372 dbfn_read_enforce_length(open_db * dbblock, const uschar * key, size_t length)
373 {
374 int rlen;
375 void * yield = dbfn_read_with_length(dbblock, key, &rlen);
376
377 if (yield)
378   {
379   if (rlen == length) return yield;
380   log_write(0, LOG_MAIN|LOG_PANIC, "Bad db record size for '%s'", key);
381   dbfn_delete(dbblock, key);
382   }
383 return NULL;
384 }
385
386
387 /*************************************************
388 *             Write to database file             *
389 *************************************************/
390
391 /*
392 Arguments:
393   dbblock   a pointer to an open database block
394   key       the key of the record to be written
395   ptr       a pointer to the record to be written
396   length    the length of the record to be written
397
398 Returns:    the yield of the underlying dbm or db "write" function. If this
399             is dbm, the value is zero for OK.
400 */
401
402 int
403 dbfn_write(open_db *dbblock, const uschar *key, void *ptr, int length)
404 {
405 EXIM_DATUM key_datum, value_datum;
406 dbdata_generic *gptr = (dbdata_generic *)ptr;
407 int klen = Ustrlen(key) + 1;
408 uschar * key_copy = store_get(klen, is_tainted(key));
409
410 memcpy(key_copy, key, klen);
411 gptr->time_stamp = time(NULL);
412
413 DEBUG(D_hints_lookup) debug_printf_indent("dbfn_write: key=%s\n", key);
414
415 EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
416 EXIM_DATUM_INIT(value_datum);       /* to be cleared before use. */
417 EXIM_DATUM_DATA(key_datum) = CS key_copy;
418 EXIM_DATUM_SIZE(key_datum) = klen;
419 EXIM_DATUM_DATA(value_datum) = CS ptr;
420 EXIM_DATUM_SIZE(value_datum) = length;
421 return EXIM_DBPUT(dbblock->dbptr, key_datum, value_datum);
422 }
423
424
425
426 /*************************************************
427 *           Delete record from database file     *
428 *************************************************/
429
430 /*
431 Arguments:
432   dbblock    a pointer to an open database block
433   key        the key of the record to be deleted
434
435 Returns: the yield of the underlying dbm or db "delete" function.
436 */
437
438 int
439 dbfn_delete(open_db *dbblock, const uschar *key)
440 {
441 int klen = Ustrlen(key) + 1;
442 uschar * key_copy = store_get(klen, is_tainted(key));
443
444 DEBUG(D_hints_lookup) debug_printf_indent("dbfn_delete: key=%s\n", key);
445
446 memcpy(key_copy, key, klen);
447 EXIM_DATUM key_datum;
448 EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require clearing */
449 EXIM_DATUM_DATA(key_datum) = CS key_copy;
450 EXIM_DATUM_SIZE(key_datum) = klen;
451 return EXIM_DBDEL(dbblock->dbptr, key_datum);
452 }
453
454
455
456 /*************************************************
457 *         Scan the keys of a database file       *
458 *************************************************/
459
460 /*
461 Arguments:
462   dbblock  a pointer to an open database block
463   start    TRUE if starting a new scan
464            FALSE if continuing with the current scan
465   cursor   a pointer to a pointer to a cursor anchor, for those dbm libraries
466            that use the notion of a cursor
467
468 Returns:   the next record from the file, or
469            NULL if there are no more
470 */
471
472 uschar *
473 dbfn_scan(open_db *dbblock, BOOL start, EXIM_CURSOR **cursor)
474 {
475 EXIM_DATUM key_datum, value_datum;
476 uschar *yield;
477
478 DEBUG(D_hints_lookup) debug_printf_indent("dbfn_scan\n");
479
480 /* Some dbm require an initialization */
481
482 if (start) EXIM_DBCREATE_CURSOR(dbblock->dbptr, cursor);
483
484 EXIM_DATUM_INIT(key_datum);         /* Some DBM libraries require the datum */
485 EXIM_DATUM_INIT(value_datum);       /* to be cleared before use. */
486
487 yield = (EXIM_DBSCAN(dbblock->dbptr, key_datum, value_datum, start, *cursor))?
488   US EXIM_DATUM_DATA(key_datum) : NULL;
489
490 /* Some dbm require a termination */
491
492 if (!yield) EXIM_DBDELETE_CURSOR(*cursor);
493 return yield;
494 }
495
496
497
498 /*************************************************
499 **************************************************
500 *             Stand-alone test program           *
501 **************************************************
502 *************************************************/
503
504 #ifdef STAND_ALONE
505
506 int
507 main(int argc, char **cargv)
508 {
509 open_db dbblock[8];
510 int max_db = sizeof(dbblock)/sizeof(open_db);
511 int current = -1;
512 int showtime = 0;
513 int i;
514 dbdata_wait *dbwait = NULL;
515 uschar **argv = USS cargv;
516 uschar buffer[256];
517 uschar structbuffer[1024];
518
519 if (argc != 2)
520   {
521   printf("Usage: test_dbfn directory\n");
522   printf("The subdirectory called \"db\" in the given directory is used for\n");
523   printf("the files used in this test program.\n");
524   return 1;
525   }
526
527 /* Initialize */
528
529 spool_directory = argv[1];
530 debug_selector = D_all - D_memory;
531 debug_file = stderr;
532 big_buffer = malloc(big_buffer_size);
533
534 for (i = 0; i < max_db; i++) dbblock[i].dbptr = NULL;
535
536 printf("\nExim's db functions tester: interface type is %s\n", EXIM_DBTYPE);
537 printf("DBM library: ");
538
539 #ifdef DB_VERSION_STRING
540 printf("Berkeley DB: %s\n", DB_VERSION_STRING);
541 #elif defined(BTREEVERSION) && defined(HASHVERSION)
542   #ifdef USE_DB
543   printf("probably Berkeley DB version 1.8x (native mode)\n");
544   #else
545   printf("probably Berkeley DB version 1.8x (compatibility mode)\n");
546   #endif
547 #elif defined(_DBM_RDONLY) || defined(dbm_dirfno)
548 printf("probably ndbm\n");
549 #elif defined(USE_TDB)
550 printf("using tdb\n");
551 #else
552   #ifdef USE_GDBM
553   printf("probably GDBM (native mode)\n");
554   #else
555   printf("probably GDBM (compatibility mode)\n");
556   #endif
557 #endif
558
559 /* Test the functions */
560
561 printf("\nTest the functions\n> ");
562
563 while (Ufgets(buffer, 256, stdin) != NULL)
564   {
565   int len = Ustrlen(buffer);
566   int count = 1;
567   clock_t start = 1;
568   clock_t stop = 0;
569   uschar *cmd = buffer;
570   while (len > 0 && isspace((uschar)buffer[len-1])) len--;
571   buffer[len] = 0;
572
573   if (isdigit((uschar)*cmd))
574     {
575     count = Uatoi(cmd);
576     while (isdigit((uschar)*cmd)) cmd++;
577     while (isspace((uschar)*cmd)) cmd++;
578     }
579
580   if (Ustrncmp(cmd, "open", 4) == 0)
581     {
582     int i;
583     open_db *odb;
584     uschar *s = cmd + 4;
585     while (isspace((uschar)*s)) s++;
586
587     for (i = 0; i < max_db; i++)
588       if (dbblock[i].dbptr == NULL) break;
589
590     if (i >= max_db)
591       {
592       printf("Too many open databases\n> ");
593       continue;
594       }
595
596     start = clock();
597     odb = dbfn_open(s, O_RDWR, dbblock + i, TRUE, TRUE);
598     stop = clock();
599
600     if (odb)
601       {
602       current = i;
603       printf("opened %d\n", current);
604       }
605     /* Other error cases will have written messages */
606     else if (errno == ENOENT)
607       {
608       printf("open failed: %s%s\n", strerror(errno),
609         #ifdef USE_DB
610         " (or other Berkeley DB error)"
611         #else
612         ""
613         #endif
614         );
615       }
616     }
617
618   else if (Ustrncmp(cmd, "write", 5) == 0)
619     {
620     int rc = 0;
621     uschar *key = cmd + 5;
622     uschar *data;
623
624     if (current < 0)
625       {
626       printf("No current database\n");
627       continue;
628       }
629
630     while (isspace((uschar)*key)) key++;
631     data = key;
632     while (*data != 0 && !isspace((uschar)*data)) data++;
633     *data++ = 0;
634     while (isspace((uschar)*data)) data++;
635
636     dbwait = (dbdata_wait *)(&structbuffer);
637     Ustrcpy(dbwait->text, data);
638
639     start = clock();
640     while (count-- > 0)
641       rc = dbfn_write(dbblock + current, key, dbwait,
642         Ustrlen(data) + sizeof(dbdata_wait));
643     stop = clock();
644     if (rc != 0) printf("Failed: %s\n", strerror(errno));
645     }
646
647   else if (Ustrncmp(cmd, "read", 4) == 0)
648     {
649     uschar *key = cmd + 4;
650     if (current < 0)
651       {
652       printf("No current database\n");
653       continue;
654       }
655     while (isspace((uschar)*key)) key++;
656     start = clock();
657     while (count-- > 0)
658       dbwait = (dbdata_wait *)dbfn_read_with_length(dbblock+ current, key, NULL);
659     stop = clock();
660     printf("%s\n", (dbwait == NULL)? "<not found>" : CS dbwait->text);
661     }
662
663   else if (Ustrncmp(cmd, "delete", 6) == 0)
664     {
665     uschar *key = cmd + 6;
666     if (current < 0)
667       {
668       printf("No current database\n");
669       continue;
670       }
671     while (isspace((uschar)*key)) key++;
672     dbfn_delete(dbblock + current, key);
673     }
674
675   else if (Ustrncmp(cmd, "scan", 4) == 0)
676     {
677     EXIM_CURSOR *cursor;
678     BOOL startflag = TRUE;
679     uschar *key;
680     uschar keybuffer[256];
681     if (current < 0)
682       {
683       printf("No current database\n");
684       continue;
685       }
686     start = clock();
687     while ((key = dbfn_scan(dbblock + current, startflag, &cursor)) != NULL)
688       {
689       startflag = FALSE;
690       Ustrcpy(keybuffer, key);
691       dbwait = (dbdata_wait *)dbfn_read_with_length(dbblock + current,
692         keybuffer, NULL);
693       printf("%s: %s\n", keybuffer, dbwait->text);
694       }
695     stop = clock();
696     printf("End of scan\n");
697     }
698
699   else if (Ustrncmp(cmd, "close", 5) == 0)
700     {
701     uschar *s = cmd + 5;
702     while (isspace((uschar)*s)) s++;
703     i = Uatoi(s);
704     if (i >= max_db || dbblock[i].dbptr == NULL) printf("Not open\n"); else
705       {
706       start = clock();
707       dbfn_close(dbblock + i);
708       stop = clock();
709       dbblock[i].dbptr = NULL;
710       if (i == current) current = -1;
711       }
712     }
713
714   else if (Ustrncmp(cmd, "file", 4) == 0)
715     {
716     uschar *s = cmd + 4;
717     while (isspace((uschar)*s)) s++;
718     i = Uatoi(s);
719     if (i >= max_db || dbblock[i].dbptr == NULL) printf("Not open\n");
720       else current = i;
721     }
722
723   else if (Ustrncmp(cmd, "time", 4) == 0)
724     {
725     showtime = ~showtime;
726     printf("Timing %s\n", showtime? "on" : "off");
727     }
728
729   else if (Ustrcmp(cmd, "q") == 0 || Ustrncmp(cmd, "quit", 4) == 0) break;
730
731   else if (Ustrncmp(cmd, "help", 4) == 0)
732     {
733     printf("close  [<number>]              close file [<number>]\n");
734     printf("delete <key>                   remove record from current file\n");
735     printf("file   <number>                make file <number> current\n");
736     printf("open   <name>                  open db file\n");
737     printf("q[uit]                         exit program\n");
738     printf("read   <key>                   read record from current file\n");
739     printf("scan                           scan current file\n");
740     printf("time                           time display on/off\n");
741     printf("write  <key> <rest-of-line>    write record to current file\n");
742     }
743
744   else printf("Eh?\n");
745
746   if (showtime && stop >= start)
747     printf("start=%d stop=%d difference=%d\n", (int)start, (int)stop,
748      (int)(stop - start));
749
750   printf("> ");
751   }
752
753 for (i = 0; i < max_db; i++)
754   {
755   if (dbblock[i].dbptr != NULL)
756     {
757     printf("\nClosing %d", i);
758     dbfn_close(dbblock + i);
759     }
760   }
761
762 printf("\n");
763 return 0;
764 }
765
766 #endif
767
768 /* End of dbfn.c */