Fix dbmjz lookup
[exim.git] / src / src / dbfn.c
index 2b5ec908b92e67a0f084531fe65a623175d9b53d..1044611cca1b276e0fb136c116d015395d83dc68 100644 (file)
@@ -38,8 +38,16 @@ are separate open and close functions. However, the calling modules should
 arrange to hold the locks for the bare minimum of time.
 
 API:
-  dbfn_open
-  dbfn_close
+  exim_lockfile_needed                 facilities predicate
+  dbfn_open_path                       full pathname; no lock taken, readonly
+  dbfn_open                            takes lockfile or opens transaction
+  dbfn_open_multi                      only if transactions supported;
+                                       no lock or transaction taken
+  dbfn_close                           release lockfile or transaction
+  dbfn_close_multi
+  dbfn_transaction_start               only if transactions supported
+  dbfn_transaction_commit
+  dbfn_read_klen                       explicit key length; embedded NUL ok
   dbfn_read_with_length
   dbfn_read_enforce_length
   dbfn_write
@@ -53,6 +61,7 @@ Users:
   TLS session resumption
   peer capability cache
   callout & quota cache
+  DBM lookup type
 */
 
 
@@ -61,6 +70,22 @@ Users:
 *          Open and lock a database file         *
 *************************************************/
 
+/* Used by DBM lookups:
+full pathname for DB file rather than hintsdb name, readonly, no locking. */
+
+open_db *
+dbfn_open_path(const uschar * path, open_db * dbblock)
+{
+uschar * dirname = string_copy(path);
+
+dbblock->readonly = TRUE;
+dbblock->lockfd = -1;
+dbblock->dbptr = !exim_lockfile_needed()
+  ? exim_dbopen_multi(path, dirname, O_RDONLY, 0)
+  : exim_dbopen(path, dirname, O_RDONLY, 0);
+return dbblock->dbptr ? dbblock : NULL;;
+}
+
 /* Ensure the directory for the DB is present */
 
 static inline void
@@ -128,7 +153,7 @@ return TRUE;
 Arguments:
   name     The single-component name of one of Exim's database files.
   flags    Either O_RDONLY or O_RDWR, indicating the type of open required;
-             O_RDWR implies "create if necessary"
+             optionally O_CREAT
   dbblock  Points to an open_db block to be filled in.
   lof      If TRUE, write to the log for actual open failures (locking failures
            are always logged).
@@ -145,7 +170,7 @@ open_db *
 dbfn_open(const uschar * name, int flags, open_db * dbblock,
   BOOL lof, BOOL panic)
 {
-int rc, save_errno;
+int rc, save_errno, dlen, flen;
 flock_t lock_data;
 uschar dirname[PATHLEN], filename[PATHLEN];
 
@@ -164,14 +189,18 @@ make the directory as well, just in case. We won't be doing this many times
 unnecessarily, because usually the lock file will be there. If the directory
 exists, there is no error. */
 
-snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
-snprintf(CS filename, sizeof(filename), "%s/%s.lockfile", dirname, name);
+dlen = snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
 
+dbblock->readonly = (flags & O_ACCMODE) == O_RDONLY;
 dbblock->lockfd = -1;
 if (!exim_lockfile_needed())
   db_dir_make(panic);
 else
   {
+  flen = Ustrlen(name);
+  snprintf(CS filename, sizeof(filename), "%.*s/%.*s.lockfile",
+           (int)sizeof(filename) - dlen - flen - 11, dirname,
+           flen, name);
   if (!lockfile_take(dbblock, filename, flags == O_RDONLY, panic))
     {
     DEBUG(D_hints_lookup) acl_level--;
@@ -188,16 +217,18 @@ databases - often this is caused by non-matching db.h and the library. To make
 it easy to pin this down, there are now debug statements on either side of the
 open call. */
 
-flags &= O_RDONLY | O_RDWR;
-snprintf(CS filename, sizeof(filename), "%s/%s", dirname, name);
+snprintf(CS filename, sizeof(filename), "%.*s/%s", dlen, dirname, name);
 
 priv_drop_temp(exim_uid, exim_gid);
-dbblock->dbptr = exim_dbopen(filename, dirname, flags, EXIMDB_MODE);
-if (!dbblock->dbptr && errno == ENOENT && flags == O_RDWR)
+dbblock->dbptr = dbblock->readonly && !exim_lockfile_needed()
+  ? exim_dbopen_multi(filename, dirname, flags & O_ACCMODE, EXIMDB_MODE)
+  : exim_dbopen(filename, dirname, flags & O_ACCMODE, EXIMDB_MODE);
+
+if (!dbblock->dbptr && errno == ENOENT && flags & O_CREAT)
   {
   DEBUG(D_hints_lookup)
     debug_printf_indent("%s appears not to exist: trying to create\n", filename);
-  dbblock->dbptr = exim_dbopen(filename, dirname, flags|O_CREAT, EXIMDB_MODE);
+  dbblock->dbptr = exim_dbopen(filename, dirname, flags, EXIMDB_MODE);
   }
 save_errno = errno;
 priv_restore();
@@ -218,24 +249,87 @@ if (!dbblock->dbptr)
           filename));
   (void)close(dbblock->lockfd);
   dbblock->lockfd = -1;
-  errno = save_errno;
-  DEBUG(D_hints_lookup) acl_level--;
-  return NULL;
+  dbblock = NULL;
   }
 
-DEBUG(D_hints_lookup)
-  debug_printf_indent("opened hints database %s: flags=%s\n", filename,
-    flags == O_RDONLY ? "O_RDONLY"
-    : flags == O_RDWR ? "O_RDWR"
-    : "??");
-
 /* Pass back the block containing the opened database handle and the open fd
 for the lock. */
 
+DEBUG(D_hints_lookup) acl_level--;
+return dbblock;
+}
+
+
+
+/* Only for transaction-capable DB types.  Open without locking or
+starting a transaction.  "lof" and "panic" always true; read/write mode.
+*/
+
+open_db *
+dbfn_open_multi(const uschar * name, int flags, open_db * dbblock)
+{
+int rc, save_errno, dlen;
+flock_t lock_data;
+uschar dirname[PATHLEN], filename[PATHLEN];
+
+DEBUG(D_hints_lookup) acl_level++;
+
+dbblock->lockfd = -1;
+dbblock->readonly = (flags & O_ACCMODE) == O_RDONLY;
+db_dir_make(TRUE);
+
+dlen = snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
+snprintf(CS filename, sizeof(filename), "%.*s/%s", dlen, dirname, name);
+
+priv_drop_temp(exim_uid, exim_gid);
+dbblock->dbptr = exim_dbopen_multi(filename, dirname, flags & O_ACCMODE, EXIMDB_MODE);
+if (!dbblock->dbptr && errno == ENOENT && flags & O_CREAT)
+  {
+  DEBUG(D_hints_lookup)
+    debug_printf_indent("%s appears not to exist: trying to create\n", filename);
+  dbblock->dbptr = exim_dbopen_multi(filename, dirname, flags, EXIMDB_MODE);
+  }
+save_errno = errno;
+priv_restore();
+
+/* If the open has failed, return NULL, leaving errno set. If lof is TRUE,
+log the event - also for debugging - but debug only if the file just doesn't
+exist. */
+
+if (!dbblock->dbptr)
+  {
+  errno = save_errno;
+  if (save_errno != ENOENT)
+    log_write(0, LOG_MAIN, "%s", string_open_failed("DB file %s",
+        filename));
+  else
+    DEBUG(D_hints_lookup)
+      debug_printf_indent("%s\n", CS string_open_failed("DB file %s",
+          filename));
+  dbblock =  NULL;
+  }
+
+/* Pass back the block containing the opened database handle */
+DEBUG(D_hints_lookup) acl_level--;
 return dbblock;
 }
 
 
+/* Return: boolean success */
+BOOL
+dbfn_transaction_start(open_db * dbp)
+{
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_transaction_start\n");
+if (!dbp->readonly) return exim_dbtransaction_start(dbp->dbptr);
+return FALSE;
+}
+void
+dbfn_transaction_commit(open_db * dbp)
+{
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_transaction_commit\n");
+if (!dbp->readonly) exim_dbtransaction_commit(dbp->dbptr);
+}
+
 
 
 /*************************************************
@@ -250,39 +344,49 @@ Returns:  nothing
 */
 
 void
-dbfn_close(open_db *dbblock)
+dbfn_close(open_db * dbp)
 {
-int * fdp = &dbblock->lockfd;
+int * fdp = &dbp->lockfd;
+
+if (dbp->readonly && !exim_lockfile_needed())
+  exim_dbclose_multi(dbp->dbptr);
+else
+  exim_dbclose(dbp->dbptr);
 
-exim_dbclose(dbblock->dbptr);
 if (*fdp >= 0) (void)close(*fdp);
 DEBUG(D_hints_lookup)
-  {
   debug_printf_indent("closed hints database%s\n",
                      *fdp < 0 ? "" : " and lockfile");
-  acl_level--;
-  }
 *fdp = -1;
 }
 
 
+void
+dbfn_close_multi(open_db * dbp)
+{
+exim_dbclose_multi(dbp->dbptr);
+DEBUG(D_hints_lookup)
+  debug_printf_indent("closed hints database\n");
+}
+
+
 
 
 /*************************************************
 *             Read from database file            *
 *************************************************/
 
-/* Passing back the pointer unchanged is useless, because there is
+/* Read, using a defined-length key (permitting embedded NULs).
+
+Passing back the pointer unchanged is useless, because there is
 no guarantee of alignment. Since all the records used by Exim need
 to be properly aligned to pick out the timestamps, etc., we might as
 well do the copying centrally here.
 
-Most calls don't need the length, so there is a macro called dbfn_read which
-has two arguments; it calls this function adding NULL as the third.
-
 Arguments:
   dbblock   a pointer to an open database block
   key       the key of the record to be read
+  klen     length of key including a terminating NUL (if present)
   length    a pointer to an int into which to return the length, if not NULL
 
 Returns: a pointer to the retrieved record, or
@@ -290,17 +394,16 @@ Returns: a pointer to the retrieved record, or
 */
 
 void *
-dbfn_read_with_length(open_db * dbblock, const uschar * key, int * length)
+dbfn_read_klen(open_db * dbblock, const uschar * key, int klen, int * length)
 {
 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);
 
-DEBUG(D_hints_lookup) debug_printf_indent("dbfn_read: key=%s\n", key);
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_read: key=%.*s\n", klen, key);
 
 exim_datum_init(&key_datum);         /* Some DBM libraries require the datum */
 exim_datum_init(&result_datum);      /* to be cleared before use. */
@@ -317,9 +420,11 @@ if (!exim_dbget(dbblock->dbptr, &key_datum, &result_datum))
 store the taint status with the data. */
 
 dlen = exim_datum_size_get(&result_datum);
-yield = store_get(dlen, GET_TAINTED);
-memcpy(yield, exim_datum_data_get(&result_datum), dlen);
 DEBUG(D_hints_lookup) debug_printf_indent("dbfn_read: size %u return\n", dlen);
+
+yield = store_get(dlen+1, GET_TAINTED);
+memcpy(yield, exim_datum_data_get(&result_datum), dlen);
+((uschar *)yield)[dlen] = '\0';
 if (length) *length = dlen;
 
 exim_datum_free(&result_datum);    /* Some DBM libs require freeing */
@@ -327,6 +432,29 @@ return yield;
 }
 
 
+/* Read, using a NUL-terminated key.
+
+Most calls don't need the length, so there is a macro called dbfn_read which
+has two arguments; it calls this function adding NULL as the third.
+
+Arguments:
+  dbblock   a pointer to an open database block
+  key       the key of the record to be read (NUL-terminated)
+  lenp      a pointer to an int into which to return the data length,
+           if not NULL
+
+Returns: a pointer to the retrieved record, or
+         NULL if the record is not found
+*/
+
+void *
+dbfn_read_with_length(open_db * dbblock, const uschar * key, int * lenp)
+{
+return dbfn_read_klen(dbblock, key, Ustrlen(key)+1, lenp);
+}
+
+
+
 /* Read a record.  If the length is not as expected then delete it, write
 an error log line, delete the record and return NULL.
 Use this for fixed-size records (so not retry or wait records).
@@ -355,7 +483,6 @@ if (yield)
 return NULL;
 }
 
-
 /*************************************************
 *             Write to database file             *
 *************************************************/
@@ -573,7 +700,7 @@ while (Ufgets(buffer, 256, stdin) != NULL)
       }
 
     start = clock();
-    odb = dbfn_open(s, O_RDWR, dbblock + i, TRUE, TRUE);
+    odb = dbfn_open(s, O_RDWR|O_CREAT, dbblock + i, TRUE, TRUE);
     stop = clock();
 
     if (odb)