#include <config.h>
#include <stdlib.h>
#include <assert.h>
#include <syslog.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include "cyrusdb.h"
#include "map.h"
#include "util.h"
#include "global.h"
#include "xmalloc.h"
#include "mailbox.h"
#include "imap_err.h"
#include "mboxkey.h"
#define FNAME_MBOXKEYSUFFIX ".mboxkey"
enum {
MBOXKEY_VERSION = 1,
MBOXKEY_DEBUG = 0
};
struct mboxkey {
char *user;
char *fname;
struct db *db;
struct txn *tid;
};
static struct mboxkey *lastmboxkey = NULL;
#define DB (config_mboxkey_db)
static void abortcurrent(struct mboxkey *s)
{
if (s && s->tid) {
int r = DB->abort(s->db, s->tid);
if (r) {
syslog(LOG_ERR, "DBERROR: error aborting txn: %s",
cyrusdb_strerror(r));
}
s->tid = NULL;
}
}
char *mboxkey_getpath(const char *userid)
{
char *fname = xmalloc(strlen(config_dir) + sizeof(FNAME_DOMAINDIR) +
sizeof(FNAME_USERDIR) + strlen(userid) +
sizeof(FNAME_MBOXKEYSUFFIX) + 10);
char c, *domain;
if (config_virtdomains && (domain = strchr(userid, '@'))) {
char d = (char) dir_hash_c(domain+1);
*domain = '\0';
c = (char) dir_hash_c(userid);
sprintf(fname, "%s%s%c/%s%s%c/%s%s", config_dir, FNAME_DOMAINDIR, d,
domain+1, FNAME_USERDIR, c, userid, FNAME_MBOXKEYSUFFIX);
*domain = '@';
}
else {
c = (char) dir_hash_c(userid);
sprintf(fname, "%s%s%c/%s%s", config_dir, FNAME_USERDIR, c, userid,
FNAME_MBOXKEYSUFFIX);
}
return fname;
}
int mboxkey_open(const char *user,
int flags,
struct mboxkey **mboxkeydbptr)
{
struct mboxkey *mboxkeydb;
struct stat sbuf;
char *fname = NULL;
int r;
mboxkeydb = lastmboxkey;
lastmboxkey = NULL;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_open(%s)", user);
}
if (mboxkeydb && !strcmp(mboxkeydb->user, user) &&
!stat(mboxkeydb->fname, &sbuf)) {
abortcurrent(mboxkeydb);
*mboxkeydbptr = mboxkeydb;
return 0;
}
*mboxkeydbptr = NULL;
if (mboxkeydb) {
abortcurrent(mboxkeydb);
r = DB->close(mboxkeydb->db);
if (r) {
syslog(LOG_ERR, "DBERROR: error closing mboxkeydb: %s",
cyrusdb_strerror(r));
}
free(mboxkeydb->user);
free(mboxkeydb->fname);
} else {
mboxkeydb = (struct mboxkey *) xmalloc(sizeof(struct mboxkey));
}
fname = mboxkey_getpath(user);
r = DB->open(fname, (flags & MBOXKEY_CREATE) ? CYRUSDB_CREATE : 0,
&mboxkeydb->db);
if (r != 0) {
int level = (flags & MBOXKEY_CREATE) ? LOG_ERR : LOG_DEBUG;
syslog(level, "DBERROR: opening %s: %s", fname,
cyrusdb_strerror(r));
r = IMAP_IOERROR;
free(mboxkeydb);
free(fname);
return r;
}
syslog(LOG_DEBUG, "mboxkey_db: user %s opened %s", user, fname);
mboxkeydb->tid = NULL;
mboxkeydb->user = xstrdup(user);
mboxkeydb->fname = fname;
*mboxkeydbptr = mboxkeydb;
return r;
}
static int mboxkey_readit(struct mboxkey *mboxkeydb, const char *mailbox,
const char **mboxkey, size_t *mboxkeylen,
int rw)
{
int r;
const char *data;
int datalen;
unsigned short version, s;
assert(mboxkeydb && mailbox);
if (rw || mboxkeydb->tid) {
r = DB->fetchlock(mboxkeydb->db, mailbox, strlen(mailbox),
&data, &datalen, &mboxkeydb->tid);
} else {
r = DB->fetch(mboxkeydb->db, mailbox, strlen(mailbox),
&data, &datalen, NULL);
}
switch (r) {
case 0:
break;
case CYRUSDB_AGAIN:
syslog(LOG_DEBUG, "deadlock in mboxkey database for '%s/%s'",
mboxkeydb->user, mailbox);
return IMAP_AGAIN;
break;
case CYRUSDB_IOERROR:
syslog(LOG_ERR, "DBERROR: error fetching txn %s",
cyrusdb_strerror(r));
return IMAP_IOERROR;
break;
case CYRUSDB_NOTFOUND:
*mboxkey = NULL;
*mboxkeylen = 0;
return 0;
break;
}
memcpy(&s, data, sizeof(s));
version = ntohs(s);
assert(version == MBOXKEY_VERSION);
*mboxkey = data + sizeof(s);
*mboxkeylen = datalen - sizeof(s);
return 0;
}
int mboxkey_read(struct mboxkey *mboxkeydb, const char *mailbox,
const char **mboxkey, size_t *mboxkeylen)
{
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_read(%s, %s)",
mboxkeydb->user, mailbox);
}
return mboxkey_readit(mboxkeydb, mailbox, mboxkey, mboxkeylen, 0);
}
int mboxkey_lockread(struct mboxkey *mboxkeydb, const char *mailbox,
const char **mboxkey, size_t *mboxkeylen)
{
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_lockread(%s, %s)",
mboxkeydb->user, mailbox);
}
return mboxkey_readit(mboxkeydb, mailbox, mboxkey, mboxkeylen, 1);
}
int mboxkey_write(struct mboxkey *mboxkeydb, const char *mailbox,
const char *mboxkey, size_t mboxkeylen)
{
int r;
assert(mboxkeydb && mailbox);
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_write(%s, %s, %s)",
mboxkeydb->user, mailbox, mboxkey ? "KEY" : "NIL");
}
if (!mboxkey) {
r = DB->delete(mboxkeydb->db, mailbox, strlen(mailbox),
&mboxkeydb->tid, 1);
}
else {
unsigned short version = MBOXKEY_VERSION, s;
int datalen = sizeof(s) + mboxkeylen;
char *data = xmalloc(datalen);
s = htons(version);
memcpy(data, &s, sizeof(s));
memcpy(data+sizeof(s), mboxkey, mboxkeylen);
r = DB->store(mboxkeydb->db, mailbox, strlen(mailbox),
data, datalen, &mboxkeydb->tid);
free(data);
}
switch (r) {
case CYRUSDB_OK:
break;
case CYRUSDB_IOERROR:
r = IMAP_AGAIN;
break;
default:
syslog(LOG_ERR, "DBERROR: error updating database: %s",
cyrusdb_strerror(r));
r = IMAP_IOERROR;
break;
}
return r;
}
int mboxkey_close(struct mboxkey *mboxkeydb)
{
int r;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_close(%s)",
mboxkeydb->user);
}
if (mboxkeydb->tid) {
r = DB->commit(mboxkeydb->db, mboxkeydb->tid);
if (r != CYRUSDB_OK) {
syslog(LOG_ERR, "DBERROR: error committing mboxkey txn; "
"mboxkey state lost: %s", cyrusdb_strerror(r));
}
mboxkeydb->tid = NULL;
}
if (lastmboxkey) {
int r;
abortcurrent(lastmboxkey);
r = DB->close(lastmboxkey->db);
if (r != CYRUSDB_OK) {
syslog(LOG_ERR, "DBERROR: error closing lastmboxkey: %s",
cyrusdb_strerror(r));
r = IMAP_IOERROR;
}
if(!r) lastmboxkey->db = NULL;
free(lastmboxkey->user);
free(lastmboxkey->fname);
free(lastmboxkey);
lastmboxkey = NULL;
}
lastmboxkey = mboxkeydb;
return 0;
}
int mboxkey_delete_user(const char *user)
{
char *fname = mboxkey_getpath(user);
int r = 0;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_delete_user(%s)",
user);
}
r = unlink(fname);
if (r < 0 && errno == ENOENT) {
syslog(LOG_DEBUG, "can not unlink %s: %m", fname);
r = 0;
}
else if (r < 0) {
syslog(LOG_ERR, "error unlinking %s: %m", fname);
r = IMAP_IOERROR;
}
free(fname);
if (lastmboxkey) {
free(lastmboxkey->user);
free(lastmboxkey->fname);
free(lastmboxkey);
lastmboxkey = NULL;
}
return r;
}
int mboxkey_unlock(struct mboxkey *mboxkeydb)
{
int r;
assert(mboxkeydb);
if (!mboxkeydb->tid) return 0;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_unlock(%s)",
mboxkeydb->user);
}
r = DB->commit(mboxkeydb->db, mboxkeydb->tid);
if (r != CYRUSDB_OK) {
syslog(LOG_ERR, "DBERROR: error committing mboxkey txn; "
"mboxkey state lost: %s", cyrusdb_strerror(r));
}
mboxkeydb->tid = NULL;
return 0;
}
int mboxkey_done(void)
{
int r = 0;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_done()");
}
if (lastmboxkey) {
abortcurrent(lastmboxkey);
r = DB->close(lastmboxkey->db);
if (r) {
syslog(LOG_ERR, "DBERROR: error closing lastmboxkey: %s",
cyrusdb_strerror(r));
r = IMAP_IOERROR;
}
free(lastmboxkey->user);
free(lastmboxkey->fname);
free(lastmboxkey);
}
return r;
}
struct mboxkey_merge_rock
{
struct db *db;
struct txn *tid;
};
static int mboxkey_merge_cb(void *rockp,
const char *key, int keylen,
const char *tmpdata, int tmpdatalen)
{
int r;
struct mboxkey_merge_rock *rockdata = (struct mboxkey_merge_rock *)rockp;
struct db *tgtdb = rockdata->db;
const char *tgtdata;
int tgtdatalen, dirty = 0;
if(!tgtdb) return IMAP_INTERNAL;
r = DB->fetchlock(tgtdb, key, keylen, &tgtdata, &tgtdatalen,
&(rockdata->tid));
if(!r && tgtdata) {
unsigned short version, s;
const char *tmp = tmpdata, *tgt = tgtdata;
memcpy(&s, tgt, sizeof(s));
version = ntohs(s);
assert(version == MBOXKEY_VERSION);
memcpy(&s, tmp, sizeof(s));
version = ntohs(s);
assert(version == MBOXKEY_VERSION);
dirty = 1;
} else {
dirty = 1;
}
if(dirty) {
return DB->store(tgtdb, key, keylen, tmpdata, tmpdatalen,
&(rockdata->tid));
} else {
return 0;
}
}
int mboxkey_merge(const char *tmpfile, const char *tgtfile)
{
int r = 0;
struct db *tmp = NULL, *tgt = NULL;
struct mboxkey_merge_rock rock;
r = DB->open(tmpfile, CYRUSDB_CREATE, &tmp);
if(r) goto done;
r = DB->open(tgtfile, CYRUSDB_CREATE, &tgt);
if(r) goto done;
rock.db = tgt;
rock.tid = NULL;
r = DB->foreach(tmp, "", 0, NULL, mboxkey_merge_cb, &rock, &rock.tid);
if(r) DB->abort(rock.db, rock.tid);
else DB->commit(rock.db, rock.tid);
done:
if(tgt) DB->close(tgt);
if(tmp) DB->close(tmp);
return r;
}