#include "sys_defs.h"
#ifdef HAS_DBM
#include <sys/stat.h>
#ifdef PATH_NDBM_H
#include PATH_NDBM_H
#else
#include <ndbm.h>
#endif
#ifdef R_FIRST
#error "Error: you are including the Berkeley DB version of ndbm.h"
#error "To build with Postfix NDBM support, delete the Berkeley DB ndbm.h file"
#endif
#include <string.h>
#include <unistd.h>
#include "msg.h"
#include "mymalloc.h"
#include "htable.h"
#include "iostuff.h"
#include "vstring.h"
#include "myflock.h"
#include "stringops.h"
#include "dict.h"
#include "dict_dbm.h"
#include "warn_stat.h"
typedef struct {
DICT dict;
DBM *dbm;
VSTRING *key_buf;
VSTRING *val_buf;
} DICT_DBM;
#define SCOPY(buf, data, size) \
vstring_str(vstring_strncpy(buf ? buf : (buf = vstring_alloc(10)), data, size))
static const char *dict_dbm_lookup(DICT *dict, const char *name)
{
DICT_DBM *dict_dbm = (DICT_DBM *) dict;
datum dbm_key;
datum dbm_value;
const char *result = 0;
dict->error = 0;
if ((dict->flags & (DICT_FLAG_TRY1NULL | DICT_FLAG_TRY0NULL)) == 0)
msg_panic("dict_dbm_lookup: no DICT_FLAG_TRY1NULL | DICT_FLAG_TRY0NULL flag");
if (dict->flags & DICT_FLAG_FOLD_FIX) {
if (dict->fold_buf == 0)
dict->fold_buf = vstring_alloc(10);
vstring_strcpy(dict->fold_buf, name);
name = lowercase(vstring_str(dict->fold_buf));
}
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_SHARED) < 0)
msg_fatal("%s: lock dictionary: %m", dict_dbm->dict.name);
if (dict->flags & DICT_FLAG_TRY1NULL) {
dbm_key.dptr = (void *) name;
dbm_key.dsize = strlen(name) + 1;
dbm_value = dbm_fetch(dict_dbm->dbm, dbm_key);
if (dbm_value.dptr != 0) {
dict->flags &= ~DICT_FLAG_TRY0NULL;
result = SCOPY(dict_dbm->val_buf, dbm_value.dptr, dbm_value.dsize);
}
}
if (result == 0 && (dict->flags & DICT_FLAG_TRY0NULL)) {
dbm_key.dptr = (void *) name;
dbm_key.dsize = strlen(name);
dbm_value = dbm_fetch(dict_dbm->dbm, dbm_key);
if (dbm_value.dptr != 0) {
dict->flags &= ~DICT_FLAG_TRY1NULL;
result = SCOPY(dict_dbm->val_buf, dbm_value.dptr, dbm_value.dsize);
}
}
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_NONE) < 0)
msg_fatal("%s: unlock dictionary: %m", dict_dbm->dict.name);
return (result);
}
static int dict_dbm_update(DICT *dict, const char *name, const char *value)
{
DICT_DBM *dict_dbm = (DICT_DBM *) dict;
datum dbm_key;
datum dbm_value;
int status;
dict->error = 0;
if ((dict->flags & (DICT_FLAG_TRY1NULL | DICT_FLAG_TRY0NULL)) == 0)
msg_panic("dict_dbm_update: no DICT_FLAG_TRY1NULL | DICT_FLAG_TRY0NULL flag");
if (dict->flags & DICT_FLAG_FOLD_FIX) {
if (dict->fold_buf == 0)
dict->fold_buf = vstring_alloc(10);
vstring_strcpy(dict->fold_buf, name);
name = lowercase(vstring_str(dict->fold_buf));
}
dbm_key.dptr = (void *) name;
dbm_value.dptr = (void *) value;
dbm_key.dsize = strlen(name);
dbm_value.dsize = strlen(value);
if ((dict->flags & DICT_FLAG_TRY1NULL)
&& (dict->flags & DICT_FLAG_TRY0NULL)) {
#ifdef DBM_NO_TRAILING_NULL
dict->flags &= ~DICT_FLAG_TRY1NULL;
#else
dict->flags &= ~DICT_FLAG_TRY0NULL;
#endif
}
if (dict->flags & DICT_FLAG_TRY1NULL) {
dbm_key.dsize++;
dbm_value.dsize++;
}
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_EXCLUSIVE) < 0)
msg_fatal("%s: lock dictionary: %m", dict_dbm->dict.name);
if ((status = dbm_store(dict_dbm->dbm, dbm_key, dbm_value,
(dict->flags & DICT_FLAG_DUP_REPLACE) ? DBM_REPLACE : DBM_INSERT)) < 0)
msg_fatal("error writing DBM database %s: %m", dict_dbm->dict.name);
if (status) {
if (dict->flags & DICT_FLAG_DUP_IGNORE)
;
else if (dict->flags & DICT_FLAG_DUP_WARN)
msg_warn("%s: duplicate entry: \"%s\"", dict_dbm->dict.name, name);
else
msg_fatal("%s: duplicate entry: \"%s\"", dict_dbm->dict.name, name);
}
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_NONE) < 0)
msg_fatal("%s: unlock dictionary: %m", dict_dbm->dict.name);
return (status);
}
static int dict_dbm_delete(DICT *dict, const char *name)
{
DICT_DBM *dict_dbm = (DICT_DBM *) dict;
datum dbm_key;
int status = 1;
dict->error = 0;
if ((dict->flags & (DICT_FLAG_TRY1NULL | DICT_FLAG_TRY0NULL)) == 0)
msg_panic("dict_dbm_delete: no DICT_FLAG_TRY1NULL | DICT_FLAG_TRY0NULL flag");
if (dict->flags & DICT_FLAG_FOLD_FIX) {
if (dict->fold_buf == 0)
dict->fold_buf = vstring_alloc(10);
vstring_strcpy(dict->fold_buf, name);
name = lowercase(vstring_str(dict->fold_buf));
}
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_EXCLUSIVE) < 0)
msg_fatal("%s: lock dictionary: %m", dict_dbm->dict.name);
if (dict->flags & DICT_FLAG_TRY1NULL) {
dbm_key.dptr = (void *) name;
dbm_key.dsize = strlen(name) + 1;
dbm_clearerr(dict_dbm->dbm);
if ((status = dbm_delete(dict_dbm->dbm, dbm_key)) < 0) {
if (dbm_error(dict_dbm->dbm) != 0)
msg_fatal("error deleting from %s: %m", dict_dbm->dict.name);
status = 1;
} else {
dict->flags &= ~DICT_FLAG_TRY0NULL;
}
}
if (status > 0 && (dict->flags & DICT_FLAG_TRY0NULL)) {
dbm_key.dptr = (void *) name;
dbm_key.dsize = strlen(name);
dbm_clearerr(dict_dbm->dbm);
if ((status = dbm_delete(dict_dbm->dbm, dbm_key)) < 0) {
if (dbm_error(dict_dbm->dbm) != 0)
msg_fatal("error deleting from %s: %m", dict_dbm->dict.name);
status = 1;
} else {
dict->flags &= ~DICT_FLAG_TRY1NULL;
}
}
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_NONE) < 0)
msg_fatal("%s: unlock dictionary: %m", dict_dbm->dict.name);
return (status);
}
static int dict_dbm_sequence(DICT *dict, int function,
const char **key, const char **value)
{
const char *myname = "dict_dbm_sequence";
DICT_DBM *dict_dbm = (DICT_DBM *) dict;
datum dbm_key;
datum dbm_value;
int status;
dict->error = 0;
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_SHARED) < 0)
msg_fatal("%s: lock dictionary: %m", dict_dbm->dict.name);
switch (function) {
case DICT_SEQ_FUN_FIRST:
dbm_key = dbm_firstkey(dict_dbm->dbm);
break;
case DICT_SEQ_FUN_NEXT:
dbm_key = dbm_nextkey(dict_dbm->dbm);
break;
default:
msg_panic("%s: invalid function: %d", myname, function);
}
if (dbm_key.dptr != 0 && dbm_key.dsize > 0) {
*key = SCOPY(dict_dbm->key_buf, dbm_key.dptr, dbm_key.dsize);
dbm_value = dbm_fetch(dict_dbm->dbm, dbm_key);
if (dbm_value.dptr != 0 && dbm_value.dsize > 0) {
*value = SCOPY(dict_dbm->val_buf, dbm_value.dptr, dbm_value.dsize);
status = 0;
} else {
if (dbm_error(dict_dbm->dbm))
msg_fatal("error seeking %s: %m", dict_dbm->dict.name);
status = 1;
}
} else {
if (dbm_error(dict_dbm->dbm))
msg_fatal("error seeking %s: %m", dict_dbm->dict.name);
status = 1;
}
if ((dict->flags & DICT_FLAG_LOCK)
&& myflock(dict->lock_fd, INTERNAL_LOCK, MYFLOCK_OP_NONE) < 0)
msg_fatal("%s: unlock dictionary: %m", dict_dbm->dict.name);
return (status);
}
static void dict_dbm_close(DICT *dict)
{
DICT_DBM *dict_dbm = (DICT_DBM *) dict;
dbm_close(dict_dbm->dbm);
if (dict_dbm->key_buf)
vstring_free(dict_dbm->key_buf);
if (dict_dbm->val_buf)
vstring_free(dict_dbm->val_buf);
if (dict->fold_buf)
vstring_free(dict->fold_buf);
dict_free(dict);
}
DICT *dict_dbm_open(const char *path, int open_flags, int dict_flags)
{
DICT_DBM *dict_dbm;
struct stat st;
DBM *dbm;
char *dbm_path;
int lock_fd;
if (dict_flags & DICT_FLAG_LOCK) {
dbm_path = concatenate(path, ".dir", (char *) 0);
if ((lock_fd = open(dbm_path, open_flags, 0644)) < 0)
return (dict_surrogate(DICT_TYPE_DBM, path, open_flags, dict_flags,
"open database %s: %m", dbm_path));
if (myflock(lock_fd, INTERNAL_LOCK, MYFLOCK_OP_SHARED) < 0)
msg_fatal("shared-lock database %s for open: %m", dbm_path);
}
if ((dbm = dbm_open((char *) path, open_flags, 0644)) == 0)
return (dict_surrogate(DICT_TYPE_DBM, path, open_flags, dict_flags,
"open database %s.{dir,pag}: %m", path));
if (dict_flags & DICT_FLAG_LOCK) {
if (myflock(lock_fd, INTERNAL_LOCK, MYFLOCK_OP_NONE) < 0)
msg_fatal("unlock database %s for open: %m", dbm_path);
if (close(lock_fd) < 0)
msg_fatal("close database %s: %m", dbm_path);
}
dict_dbm = (DICT_DBM *) dict_alloc(DICT_TYPE_DBM, path, sizeof(*dict_dbm));
dict_dbm->dict.lookup = dict_dbm_lookup;
dict_dbm->dict.update = dict_dbm_update;
dict_dbm->dict.delete = dict_dbm_delete;
dict_dbm->dict.sequence = dict_dbm_sequence;
dict_dbm->dict.close = dict_dbm_close;
dict_dbm->dict.lock_fd = dbm_dirfno(dbm);
dict_dbm->dict.stat_fd = dbm_pagfno(dbm);
if (dict_dbm->dict.lock_fd == dict_dbm->dict.stat_fd)
msg_fatal("open database %s: cannot support GDBM", path);
if (fstat(dict_dbm->dict.stat_fd, &st) < 0)
msg_fatal("dict_dbm_open: fstat: %m");
dict_dbm->dict.mtime = st.st_mtime;
dict_dbm->dict.owner.uid = st.st_uid;
dict_dbm->dict.owner.status = (st.st_uid != 0);
if ((dict_flags & DICT_FLAG_LOCK) != 0
&& stat(path, &st) == 0
&& st.st_mtime > dict_dbm->dict.mtime
&& st.st_mtime < time((time_t *) 0) - 100)
msg_warn("database %s is older than source file %s", dbm_path, path);
close_on_exec(dbm_pagfno(dbm), CLOSE_ON_EXEC);
close_on_exec(dbm_dirfno(dbm), CLOSE_ON_EXEC);
dict_dbm->dict.flags = dict_flags | DICT_FLAG_FIXED;
if ((dict_flags & (DICT_FLAG_TRY0NULL | DICT_FLAG_TRY1NULL)) == 0)
dict_dbm->dict.flags |= (DICT_FLAG_TRY0NULL | DICT_FLAG_TRY1NULL);
if (dict_flags & DICT_FLAG_FOLD_FIX)
dict_dbm->dict.fold_buf = vstring_alloc(10);
dict_dbm->dbm = dbm;
dict_dbm->key_buf = 0;
dict_dbm->val_buf = 0;
if ((dict_flags & DICT_FLAG_LOCK))
myfree(dbm_path);
return (DICT_DEBUG (&dict_dbm->dict));
}
#endif