fix pop3 Aggregator
[citadel.git] / citadel / database.c
index 97e6c4cf045aa0f6b45b5ae8afaacf314ebf83e7..dbcccf5b16b1a51ce3b7c17136d08f3f849022f7 100644 (file)
@@ -1,22 +1,16 @@
 /*
  * This is a data store backend for the Citadel server which uses Berkeley DB.
  *
- * Copyright (c) 1987-2011 by the citadel.org team
+ * Copyright (c) 1987-2012 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as published
- * by the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
+ * modify it under the terms of the GNU General Public License version 3.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
  */
 
 /*****************************************************************************
 /*****************************************************************************/
 
 #include "sysdep.h"
-#include <stdlib.h>
-#include <unistd.h>
 #include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <dirent.h>
-#include <syslog.h>
+#include <zlib.h>
 
 #ifdef HAVE_DB_H
 #include <db.h>
 
 
 #include <libcitadel.h>
-#include "citadel.h"
-#include "server.h"
-#include "citserver.h"
-#include "database.h"
-#include "msgbase.h"
-#include "sysdep_decls.h"
-#include "threads.h"
-#include "config.h"
-#include "control.h"
 
 #include "ctdl_module.h"
+#include "control.h"
+#include "citserver.h"
 
 
 static DB *dbp[MAXCDB];                /* One DB handle for each Citadel database */
 static DB_ENV *dbenv;          /* The DB environment (global) */
 
 
-#ifdef HAVE_ZLIB
-#include <zlib.h>
-#endif
+void cdb_abort(void) {
+       syslog(LOG_DEBUG,
+               "citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
+               getuid(),
+               getgid(),
+               geteuid(),
+               getegid()
+       );
+       cit_backtrace();
+       exit(CTDLEXIT_DB);
+}
 
 
 /* Verbose logging callback */
@@ -86,6 +74,7 @@ void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
 {
        if (!IsEmptyStr(msg)) {
                syslog(LOG_DEBUG, "DB: %s", msg);
+               cit_backtrace();
        }
 }
 
@@ -93,7 +82,10 @@ void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
 /* Verbose logging callback */
 void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg)
 {
+       int *FOO = NULL;
        syslog(LOG_ALERT, "DB: %s", msg);
+       cit_backtrace();
+       *FOO = 1;
 }
 
 
@@ -106,7 +98,7 @@ static void txabort(DB_TXN * tid)
 
        if (ret) {
                syslog(LOG_EMERG, "bdb(): txn_abort: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -119,7 +111,7 @@ static void txcommit(DB_TXN * tid)
 
        if (ret) {
                syslog(LOG_EMERG, "bdb(): txn_commit: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -132,13 +124,14 @@ static void txbegin(DB_TXN ** tid)
 
        if (ret) {
                syslog(LOG_EMERG, "bdb(): txn_begin: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
 static void dbpanic(DB_ENV * env, int errval)
 {
        syslog(LOG_EMERG, "bdb(): PANIC: %s", db_strerror(errval));
+       cit_backtrace();
 }
 
 static void cclose(DBC * cursor)
@@ -147,7 +140,7 @@ static void cclose(DBC * cursor)
 
        if ((ret = cursor->c_close(cursor))) {
                syslog(LOG_EMERG, "bdb(): c_close: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -158,7 +151,7 @@ static void bailIfCursor(DBC ** cursors, const char *msg)
        for (i = 0; i < MAXCDB; i++)
                if (cursors[i] != NULL) {
                        syslog(LOG_EMERG, "bdb(): cursor still in progress on cdb %02x: %s", i, msg);
-                       abort();
+                       cdb_abort();
                }
 }
 
@@ -169,7 +162,7 @@ void cdb_check_handles(void)
 
        if (TSD->tid != NULL) {
                syslog(LOG_EMERG, "bdb(): transaction still in progress!");
-               abort();
+               cdb_abort();
        }
 }
 
@@ -177,7 +170,7 @@ void cdb_check_handles(void)
 /*
  * Cull the database logs
  */
-static void cdb_cull_logs(void)
+void cdb_cull_logs(void)
 {
        u_int32_t flags;
        int ret;
@@ -213,15 +206,6 @@ static void cdb_cull_logs(void)
        }
 }
 
-/*
- * Manually initiate log file cull.
- */
-void cmd_cull(char *argbuf) {
-       if (CtdlAccessCheck(ac_internal)) return;
-       cdb_cull_logs();
-       cprintf("%d Database log file cull completed.", CIT_OK);
-}
-
 
 /*
  * Request a checkpoint of the database.  Called once per minute by the thread manager.
@@ -235,7 +219,7 @@ void cdb_checkpoint(void)
 
        if (ret != 0) {
                syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 
        /* After a successful checkpoint, we can cull the unused logs */
@@ -283,9 +267,7 @@ void open_databases(void)
        CitControl.MMdbversion = current_dbversion;
        put_control();
 
-#ifdef HAVE_ZLIB
        syslog(LOG_INFO, "Linked zlib: %s\n", zlibVersion());
-#endif
 
        /*
         * Silently try to create the database subdirectory.  If it's
@@ -395,7 +377,7 @@ void open_databases(void)
                if (ret) {
                        syslog(LOG_EMERG, "db_open[%02x]: %s\n", i, db_strerror(ret));
                        if (ret == ENOMEM) {
-                               syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php/faq:troubleshooting:out_of_lock_entries for more information.\n");
+                               syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information.");
                        }
                        syslog(LOG_EMERG, "exit code %d\n", ret);
                        exit(CTDLEXIT_DB);
@@ -431,7 +413,6 @@ void cdb_chmod_data(void) {
        }
 
        syslog(LOG_DEBUG, "open_databases() finished\n");
-       CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
 }
 
 
@@ -473,7 +454,7 @@ void close_databases(void)
 
 
 /*
- * Compression functions only used if we have zlib
+ * Decompress a database item if it was compressed on disk
  */
 void cdb_decompress_if_necessary(struct cdbdata *cdb)
 {
@@ -485,7 +466,6 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb)
            (memcmp(cdb->ptr, &magic, sizeof(magic))))
            return;
 
-#ifdef HAVE_ZLIB
        /* At this point we know we're looking at a compressed item. */
 
        struct CtdlCompressHeader zheader;
@@ -512,16 +492,12 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb)
                       (const Bytef *) compressed_data,
                       (uLong) sourceLen) != Z_OK) {
                syslog(LOG_EMERG, "uncompress() error\n");
-               abort();
+               cdb_abort();
        }
 
        free(cdb->ptr);
        cdb->len = (size_t) destLen;
        cdb->ptr = uncompressed_data;
-#else                          /* HAVE_ZLIB */
-       syslog(LOG_EMERG, "Database contains compressed data, but this citserver was built without compression support.");
-       exit(CTDLEXIT_DB);
-#endif                         /* HAVE_ZLIB */
 }
 
 
@@ -537,22 +513,21 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
        DB_TXN *tid;
        int ret = 0;
 
-#ifdef HAVE_ZLIB
        struct CtdlCompressHeader zheader;
        char *compressed_data = NULL;
        int compressing = 0;
        size_t buffer_len = 0;
        uLongf destLen = 0;
-#endif
 
        memset(&dkey, 0, sizeof(DBT));
        memset(&ddata, 0, sizeof(DBT));
        dkey.size = ckeylen;
+       /* no, we don't care for this error. */
        dkey.data = ckey;
+
        ddata.size = cdatalen;
        ddata.data = cdata;
 
-#ifdef HAVE_ZLIB
        /* Only compress Visit records.  Everything else is uncompressed. */
        if (cdb == CDB_VISIT) {
                compressing = 1;
@@ -566,14 +541,13 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                        &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK)
                {
                        syslog(LOG_EMERG, "compress2() error\n");
-                       abort();
+                       cdb_abort();
                }
                zheader.compressed_len = (size_t) destLen;
                memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader));
                ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
                ddata.data = compressed_data;
        }
-#endif
 
        if (TSD->tid != NULL) {
                ret = dbp[cdb]->put(dbp[cdb],   /* db */
@@ -583,12 +557,10 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                                    0); /* flags */
                if (ret) {
                        syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
-                       abort();
+                       cdb_abort();
                }
-#ifdef HAVE_ZLIB
                if (compressing)
                        free(compressed_data);
-#endif
                return ret;
 
        } else {
@@ -607,18 +579,17 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                                goto retry;
                        } else {
                                syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
-                               abort();
+                               cdb_abort();
                        }
                } else {
                        txcommit(tid);
-#ifdef HAVE_ZLIB
                        if (compressing) {
                                free(compressed_data);
                        }
-#endif
                        return ret;
                }
        }
+       return ret;
 }
 
 
@@ -641,7 +612,7 @@ int cdb_delete(int cdb, void *key, int keylen)
                if (ret) {
                        syslog(LOG_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret));
                        if (ret != DB_NOTFOUND) {
-                               abort();
+                               cdb_abort();
                        }
                }
        } else {
@@ -658,7 +629,7 @@ int cdb_delete(int cdb, void *key, int keylen)
                        } else {
                                syslog(LOG_EMERG, "cdb_delete(%d): %s\n",
                                        cdb, db_strerror(ret));
-                               abort();
+                               cdb_abort();
                        }
                } else {
                        txcommit(tid);
@@ -679,7 +650,7 @@ static DBC *localcursor(int cdb)
 
        if (ret) {
                syslog(LOG_EMERG, "localcursor: %s\n", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 
        return curs;
@@ -693,13 +664,13 @@ static DBC *localcursor(int cdb)
  */
 struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
 {
-
        struct cdbdata *tempcdb;
        DBT dkey, dret;
        int ret;
 
        memset(&dkey, 0, sizeof(DBT));
        dkey.size = keylen;
+       /* no we don't care about this error. */
        dkey.data = key;
 
        if (TSD->tid != NULL) {
@@ -724,7 +695,7 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
 
        if ((ret != 0) && (ret != DB_NOTFOUND)) {
                syslog(LOG_EMERG, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 
        if (ret != 0)
@@ -733,13 +704,16 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
 
        if (tempcdb == NULL) {
                syslog(LOG_EMERG, "cdb_fetch: Cannot allocate memory for tempcdb\n");
-               abort();
+               cdb_abort();
+               return NULL; /* make it easier for static analysis... */
+       }
+       else
+       {
+               tempcdb->len = dret.size;
+               tempcdb->ptr = dret.data;
+               cdb_decompress_if_necessary(tempcdb);
+               return (tempcdb);
        }
-
-       tempcdb->len = dret.size;
-       tempcdb->ptr = dret.data;
-       cdb_decompress_if_necessary(tempcdb);
-       return (tempcdb);
 }
 
 
@@ -779,8 +753,8 @@ void cdb_rewind(int cdb)
 
        if (TSD->cursors[cdb] != NULL) {
                syslog(LOG_EMERG,
-                       "cdb_rewind: must close cursor on database %d before reopening.\n", cdb);
-               abort();
+                      "cdb_rewind: must close cursor on database %d before reopening.\n", cdb);
+               cdb_abort();
                /* cclose(TSD->cursors[cdb]); */
        }
 
@@ -790,7 +764,7 @@ void cdb_rewind(int cdb)
        ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
        if (ret) {
                syslog(LOG_EMERG, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -815,7 +789,7 @@ struct cdbdata *cdb_next_item(int cdb)
        if (ret) {
                if (ret != DB_NOTFOUND) {
                        syslog(LOG_EMERG, "cdb_next_item(%d): %s\n", cdb, db_strerror(ret));
-                       abort();
+                       cdb_abort();
                }
                cdb_close_cursor(cdb);
                return NULL;    /* presumably, end of file */
@@ -842,7 +816,7 @@ void cdb_begin_transaction(void)
 
        if (TSD->tid != NULL) {
                syslog(LOG_EMERG, "cdb_begin_transaction: ERROR: nested transaction\n");
-               abort();
+               cdb_abort();
        }
 
        txbegin(&TSD->tid);
@@ -864,7 +838,7 @@ void cdb_end_transaction(void)
        if (TSD->tid == NULL) {
                syslog(LOG_EMERG,
                        "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
-               abort();
+               cdb_abort();
        } else {
                txcommit(TSD->tid);
        }
@@ -883,7 +857,7 @@ void cdb_trunc(int cdb)
 
        if (TSD->tid != NULL) {
                syslog(LOG_EMERG, "cdb_trunc must not be called in a transaction.");
-               abort();
+               cdb_abort();
        } else {
                bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
 
@@ -900,7 +874,7 @@ void cdb_trunc(int cdb)
                        } else {
                                syslog(LOG_EMERG, "cdb_truncate(%d): %s\n", cdb, db_strerror(ret));
                                if (ret == ENOMEM) {
-                                       syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php/faq:troubleshooting:out_of_lock_entries for more information.");
+                                       syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information.");
                                }
                                exit(CTDLEXIT_DB);
                        }
@@ -909,3 +883,141 @@ void cdb_trunc(int cdb)
                }
        }
 }
+
+int SeentDebugEnabled = 0;
+
+#define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (SeentDebugEnabled != 0))
+#define SEENM_syslog(LEVEL, FORMAT)                                    \
+       DBGLOG(LEVEL) syslog(LEVEL,                                     \
+                            "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT,     \
+                            IOSTR, ioid, ccid, Facility, cType)
+
+#define SEEN_syslog(LEVEL, FORMAT, ...)                                        \
+       DBGLOG(LEVEL) syslog(LEVEL,                                     \
+                            "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT,     \
+                            IOSTR, ioid, ccid, Facility, cType,        \
+                            __VA_ARGS__)
+
+time_t CheckIfAlreadySeen(const char *Facility,
+                         StrBuf *guid,
+                         time_t now,
+                         time_t antiexpire,
+                         eCheckType cType,
+                         long ccid,
+                         long ioid)
+{
+       time_t InDBTimeStamp = 0;
+       struct UseTable ut;
+       struct cdbdata *cdbut;
+
+       if (cType != eWrite)
+       {
+               SEEN_syslog(LOG_DEBUG, "Loading [%s]", ChrPtr(guid));
+               cdbut = cdb_fetch(CDB_USETABLE, SKEY(guid));
+               if ((cdbut != NULL) && (cdbut->ptr != NULL)) {
+                       memcpy(&ut, cdbut->ptr,
+                              ((cdbut->len > sizeof(struct UseTable)) ?
+                               sizeof(struct UseTable) : cdbut->len));
+                       InDBTimeStamp = now - ut.ut_timestamp;
+
+                       if (InDBTimeStamp < antiexpire)
+                       {
+                               SEEN_syslog(LOG_DEBUG, "Found - Not expired %ld < %ld", InDBTimeStamp, antiexpire);
+                               cdb_free(cdbut);
+                               return InDBTimeStamp;
+                       }
+                       else
+                       {
+                               SEEN_syslog(LOG_DEBUG, "Found - Expired. %ld >= %ld", InDBTimeStamp, antiexpire);
+                               cdb_free(cdbut);
+                       }
+               }
+               else
+               {
+                       if (cdbut) cdb_free(cdbut);
+                       
+                       SEENM_syslog(LOG_DEBUG, "not Found");
+                       if (cType == eCheckUpdate)
+                               return 0;
+               }
+
+               if (cType == eCheckExist)
+                       return InDBTimeStamp;
+       }
+
+       memcpy(ut.ut_msgid, SKEY(guid));
+       ut.ut_timestamp = now;
+
+       SEENM_syslog(LOG_DEBUG, "Saving new Timestamp");
+       /* rewrite the record anyway, to update the timestamp */
+       cdb_store(CDB_USETABLE,
+                 SKEY(guid),
+                 &ut, sizeof(struct UseTable) );
+
+       SEENM_syslog(LOG_DEBUG, "Done Saving");
+       return InDBTimeStamp;
+}
+
+
+void cmd_rsen(char *argbuf) {
+       char Token[SIZ];
+       long TLen;
+       char Time[SIZ];
+
+       struct UseTable ut;
+       struct cdbdata *cdbut;
+       
+       if (CtdlAccessCheck(ac_aide)) return;
+
+       TLen = extract_token(Token, argbuf, 1, '|', sizeof Token);
+       if (strncmp(argbuf, "GET", 3) == 0) {
+               cdbut = cdb_fetch(CDB_USETABLE, Token, TLen);
+               if (cdbut != NULL) {
+                       memcpy(&ut, cdbut->ptr,
+                              ((cdbut->len > sizeof(struct UseTable)) ?
+                               sizeof(struct UseTable) : cdbut->len));
+                       
+                       cprintf("%d %ld\n", CIT_OK, ut.ut_timestamp);
+               }
+               else {
+                       cprintf("%d not found\n", ERROR + NOT_HERE);
+               }
+
+       }
+       else if (strncmp(argbuf, "SET", 3) == 0) {
+               memcpy(ut.ut_msgid, Token, TLen);
+               extract_token(Time, argbuf, 2, '|', sizeof Time);
+               ut.ut_timestamp = atol(Time);
+               cdb_store(CDB_USETABLE,
+                         Token, TLen,
+                         &ut, sizeof(struct UseTable) );
+               cprintf("%d token updated\n", CIT_OK);
+       }
+       else if (strncmp(argbuf, "DEL", 3) == 0) {
+               if (cdb_delete(CDB_USETABLE, Token, TLen))
+                       cprintf("%d not found\n", ERROR + NOT_HERE);
+               else
+                       cprintf("%d deleted.\n", CIT_OK);
+
+       }
+       else {
+               cprintf("%d Usage: [GET|SET|DEL]|Token|timestamp\n", ERROR);
+       }
+
+}
+void LogDebugEnableSeenEnable(const int n)
+{
+       SeentDebugEnabled = n;
+}
+
+CTDL_MODULE_INIT(database)
+{
+       if (!threading)
+       {
+               CtdlRegisterDebugFlagHook(HKEY("SeenDebug"), LogDebugEnableSeenEnable, &SeentDebugEnabled);
+               CtdlRegisterProtoHook(cmd_rsen, "RSEN", "manipulate Aggregators seen database");
+       }
+
+       /* return our module id for the log */
+       return "database";
+}