* Reference count adjustments are now deferred by queuing
[citadel.git] / citadel / serv_expire.c
index c65fee436587cc1d947dd25535db3611936daf85..e357ea615dfe7157078f4470b2c4a0280ccaeb12 100644 (file)
@@ -14,8 +14,8 @@
  * then the second stage deletes all listed objects from the database.
  *
  * At first glance this may seem cumbersome and unnecessary.  The reason it is
- * implemented in this way is because GDBM (and perhaps some other backends we
- * may hook into in the future) explicitly do _not_ support the deletion of
+ * implemented in this way is because Berkeley DB, and possibly other backends
+ * we may hook into in the future, explicitly do _not_ support the deletion of
  * records from a file while the file is being traversed.  The delete operation
  * will succeed, but the traversal is not guaranteed to visit every object if
  * this is done.  Therefore we utilize the two-stage purge.
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include <syslog.h>
 #include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #include "room_ops.h"
 #include "policy.h"
 #include "database.h"
@@ -91,14 +90,20 @@ struct ValidUser {
 };
 
 
-struct roomref {
-       struct roomref *next;
+struct ctdlroomref {
+       struct ctdlroomref *next;
        long msgnum;
 };
 
 struct UPurgeList {
        struct UPurgeList *next;
-       char up_key[SIZ];
+       char up_key[256];
+};
+
+struct EPurgeList {
+       struct EPurgeList *next;
+       int ep_keylen;
+       char *ep_key;
 };
 
 
@@ -108,7 +113,7 @@ struct ValidRoom *ValidRoomList = NULL;
 struct ValidUser *ValidUserList = NULL;
 int messages_purged;
 
-struct roomref *rr = NULL;
+struct ctdlroomref *rr = NULL;
 
 extern struct CitContext *ContextList;
 
@@ -117,7 +122,7 @@ extern struct CitContext *ContextList;
  * First phase of message purge -- gather the locations of messages which
  * qualify for purging and write them to a temp file.
  */
-void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
+void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        struct ExpirePolicy epbuf;
        long delnum;
        time_t xtime, now;
@@ -141,7 +146,7 @@ void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
 
         if (cdbfr != NULL) {
-               msglist = mallok(cdbfr->len);
+               msglist = malloc(cdbfr->len);
                memcpy(msglist, cdbfr->ptr, cdbfr->len);
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
@@ -149,7 +154,7 @@ void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
 
        /* Nothing to do if there aren't any messages */
        if (num_msgs == 0) {
-               if (msglist != NULL) phree(msglist);
+               if (msglist != NULL) free(msglist);
                return;
        }
 
@@ -168,7 +173,7 @@ void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
                for (a=0; a<num_msgs; ++a) {
                        delnum = msglist[a];
 
-                       msg = CtdlFetchMessage(delnum);
+                       msg = CtdlFetchMessage(delnum, 0); /* dont need body */
                        if (msg != NULL) {
                                xtime = atol(msg->cm_fields['T']);
                                CtdlFreeMessage(msg);
@@ -184,7 +189,7 @@ void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
                }
        }
 
-       if (msglist != NULL) phree(msglist);
+       if (msglist != NULL) free(msglist);
 }
 
 
@@ -207,7 +212,7 @@ void DoPurgeMessages(FILE *purgelist) {
                if (!strncasecmp(buf, "m=", 2)) {
                        msgnum = atol(&buf[2]);
                        if (msgnum > 0L) {
-                               CtdlDeleteMessages(roomname, msgnum, "");
+                               CtdlDeleteMessages(roomname, &msgnum, 1, "");
                        }
                }
        }
@@ -217,12 +222,12 @@ void DoPurgeMessages(FILE *purgelist) {
 void PurgeMessages(void) {
        FILE *purgelist;
 
-       lprintf(5, "PurgeMessages() called\n");
+       lprintf(CTDL_DEBUG, "PurgeMessages() called\n");
        messages_purged = 0;
 
        purgelist = tmpfile();
        if (purgelist == NULL) {
-               lprintf(3, "Can't create purgelist temp file: %s\n",
+               lprintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
                        strerror(errno));
                return;
        }
@@ -233,26 +238,26 @@ void PurgeMessages(void) {
 }
 
 
-void AddValidUser(struct usersupp *usbuf, void *data) {
+void AddValidUser(struct ctdluser *usbuf, void *data) {
        struct ValidUser *vuptr;
 
-       vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
+       vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
        vuptr->next = ValidUserList;
        vuptr->vu_usernum = usbuf->usernum;
        ValidUserList = vuptr;
 }
 
-void AddValidRoom(struct quickroom *qrbuf, void *data) {
+void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
        struct ValidRoom *vrptr;
 
-       vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
+       vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
        vrptr->next = ValidRoomList;
        vrptr->vr_roomnum = qrbuf->QRnumber;
        vrptr->vr_roomgen = qrbuf->QRgen;
        ValidRoomList = vrptr;
 }
 
-void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
+void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
        time_t age, purge_secs;
        struct PurgeList *pptr;
        struct ValidUser *vuptr;
@@ -289,12 +294,12 @@ void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
                age = time(NULL) - (qrbuf->QRmtime);
                purge_secs = (time_t)config.c_roompurge * (time_t)86400;
                if (purge_secs <= (time_t)0) return;
-               lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
+               lprintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
                if (age > purge_secs) do_purge = 1;
        } /* !QR_MAILBOX */
 
        if (do_purge) {
-               pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
+               pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = RoomPurgeList;
                strcpy(pptr->name, qrbuf->QRname);
                RoomPurgeList = pptr;
@@ -307,11 +312,11 @@ void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
 int PurgeRooms(void) {
        struct PurgeList *pptr;
        int num_rooms_purged = 0;
-       struct quickroom qrbuf;
+       struct ctdlroom qrbuf;
        struct ValidUser *vuptr;
        char *transcript = NULL;
 
-       lprintf(5, "PurgeRooms() called\n");
+       lprintf(CTDL_DEBUG, "PurgeRooms() called\n");
 
 
        /* Load up a table full of valid user numbers so we can delete
@@ -324,36 +329,59 @@ int PurgeRooms(void) {
        /* Free the valid user list */
        while (ValidUserList != NULL) {
                vuptr = ValidUserList->next;
-               phree(ValidUserList);
+               free(ValidUserList);
                ValidUserList = vuptr;
        }
 
 
-       transcript = mallok(SIZ);
+       transcript = malloc(SIZ);
        strcpy(transcript, "The following rooms have been auto-purged:\n");
 
        while (RoomPurgeList != NULL) {
                if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
-                       transcript=reallok(transcript, strlen(transcript)+SIZ);
+                       transcript=realloc(transcript, strlen(transcript)+SIZ);
                        snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                                qrbuf.QRname);
                        delete_room(&qrbuf);
                }
                pptr = RoomPurgeList->next;
-               phree(RoomPurgeList);
+               free(RoomPurgeList);
                RoomPurgeList = pptr;
                ++num_rooms_purged;
        }
 
-       if (num_rooms_purged > 0) aide_message(transcript);
-       phree(transcript);
+       if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
+       free(transcript);
 
-       lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
+       lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
        return(num_rooms_purged);
 }
 
 
-void do_user_purge(struct usersupp *us, void *data) {
+/*
+ * Back end function to check user accounts for associated Unix accounts
+ * which no longer exist.  (Only relevant for host auth mode.)
+ */
+void do_uid_user_purge(struct ctdluser *us, void *data) {
+       struct PurgeList *pptr;
+
+       if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
+               if (getpwuid(us->uid) == NULL) {
+                       pptr = (struct PurgeList *)
+                               malloc(sizeof(struct PurgeList));
+                       pptr->next = UserPurgeList;
+                       strcpy(pptr->name, us->fullname);
+                       UserPurgeList = pptr;
+               }
+       }
+}
+
+
+
+/*
+ * Back end function to check user accounts for expiration.
+ */
+void do_user_purge(struct ctdluser *us, void *data) {
        int purge;
        time_t now;
        time_t purge_time;
@@ -361,12 +389,12 @@ void do_user_purge(struct usersupp *us, void *data) {
 
        /* stupid recovery routine to re-create missing mailboxen.
         * don't enable this.
-       struct quickroom qrbuf;
+       struct ctdlroom qrbuf;
        char mailboxname[ROOMNAMELEN];
        MailboxName(mailboxname, us, MAILROOM);
-       create_room(mailboxname, 4, "", 0, 1, 1);
+       create_room(mailboxname, 4, "", 0, 1, 1, VIEW_BBS);
        if (getroom(&qrbuf, mailboxname) != 0) return;
-       lprintf(9, "Got %s\n", qrbuf.QRname);
+       lprintf(CTDL_DEBUG, "Got %s\n", qrbuf.QRname);
         */
 
 
@@ -407,8 +435,13 @@ void do_user_purge(struct usersupp *us, void *data) {
         */
        if (us->timescalled == 0) purge = 1;
 
+       /* User number 0, as well as any negative user number, is
+        * also impossible.
+        */
+       if (us->usernum < 1L) purge = 1;
+
        if (purge == 1) {
-               pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
+               pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = UserPurgeList;
                strcpy(pptr->name, us->fullname);
                UserPurgeList = pptr;
@@ -423,29 +456,37 @@ int PurgeUsers(void) {
        int num_users_purged = 0;
        char *transcript = NULL;
 
-       lprintf(5, "PurgeUsers() called\n");
-       if (config.c_userpurge > 0) {
-               ForEachUser(do_user_purge, NULL);
+       lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
+
+       if (config.c_auth_mode == 1) {
+               /* host auth mode */
+               ForEachUser(do_uid_user_purge, NULL);
+       }
+       else {
+               /* native auth mode */
+               if (config.c_userpurge > 0) {
+                       ForEachUser(do_user_purge, NULL);
+               }
        }
 
-       transcript = mallok(SIZ);
+       transcript = malloc(SIZ);
        strcpy(transcript, "The following users have been auto-purged:\n");
 
        while (UserPurgeList != NULL) {
-               transcript=reallok(transcript, strlen(transcript)+SIZ);
+               transcript=realloc(transcript, strlen(transcript)+SIZ);
                snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                        UserPurgeList->name);
                purge_user(UserPurgeList->name);
                pptr = UserPurgeList->next;
-               phree(UserPurgeList);
+               free(UserPurgeList);
                UserPurgeList = pptr;
                ++num_users_purged;
        }
 
-       if (num_users_purged > 0) aide_message(transcript);
-       phree(transcript);
+       if (num_users_purged > 0) aide_message(transcript,"User Purge Message");
+       free(transcript);
 
-       lprintf(5, "Purged %d users.\n", num_users_purged);
+       lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
        return(num_users_purged);
 }
 
@@ -508,7 +549,7 @@ int PurgeVisits(void) {
                /* Put the record on the purge list if it's dead */
                if ((RoomIsValid==0) || (UserIsValid==0)) {
                        vptr = (struct VPurgeList *)
-                               mallok(sizeof(struct VPurgeList));
+                               malloc(sizeof(struct VPurgeList));
                        vptr->next = VisitPurgeList;
                        vptr->vp_roomnum = vbuf.v_roomnum;
                        vptr->vp_roomgen = vbuf.v_roomgen;
@@ -521,14 +562,14 @@ int PurgeVisits(void) {
        /* Free the valid room/gen combination list */
        while (ValidRoomList != NULL) {
                vrptr = ValidRoomList->next;
-               phree(ValidRoomList);
+               free(ValidRoomList);
                ValidRoomList = vrptr;
        }
 
        /* Free the valid user list */
        while (ValidUserList != NULL) {
                vuptr = ValidUserList->next;
-               phree(ValidUserList);
+               free(ValidUserList);
                ValidUserList = vuptr;
        }
 
@@ -540,7 +581,7 @@ int PurgeVisits(void) {
                                VisitPurgeList->vp_usernum);
                cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
                vptr = VisitPurgeList->next;
-               phree(VisitPurgeList);
+               free(VisitPurgeList);
                VisitPurgeList = vptr;
                ++purged;
        }
@@ -560,7 +601,7 @@ int PurgeUseTable(void) {
        struct UPurgeList *uptr; 
 
        /* Phase 1: traverse through the table, discovering old records... */
-       lprintf(9, "Purge use table: phase 1\n");
+       lprintf(CTDL_DEBUG, "Purge use table: phase 1\n");
        cdb_rewind(CDB_USETABLE);
        while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
 
@@ -570,10 +611,10 @@ int PurgeUseTable(void) {
                 cdb_free(cdbut);
 
                if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
-                       uptr = (struct UPurgeList *) mallok(sizeof(struct UPurgeList));
+                       uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
                        if (uptr != NULL) {
                                uptr->next = ul;
-                               safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
+                               safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
                                ul = uptr;
                        }
                        ++purged;
@@ -582,77 +623,133 @@ int PurgeUseTable(void) {
        }
 
        /* Phase 2: delete the records */
-       lprintf(9, "Purge use table: phase 2\n");
+       lprintf(CTDL_DEBUG, "Purge use table: phase 2\n");
        while (ul != NULL) {
                cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
                uptr = ul->next;
-               phree(ul);
+               free(ul);
                ul = uptr;
        }
 
-       lprintf(9, "Purge use table: finished (purged %d records)\n", purged);
+       lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
        return(purged);
 }
 
 
-void cmd_expi(char *argbuf) {
-       char cmd[SIZ];
-       int retval;
 
-       if (CtdlAccessCheck(ac_aide)) return;
+/*
+ * Purge the EUID Index of old records.
+ *
+ */
+int PurgeEuidIndexTable(void) {
+       int purged = 0;
+       struct cdbdata *cdbei;
+       struct EPurgeList *el = NULL;
+       struct EPurgeList *eptr; 
+       long msgnum;
+       struct CtdlMessage *msg;
+
+       /* Phase 1: traverse through the table, discovering old records... */
+       lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
+       cdb_rewind(CDB_EUIDINDEX);
+       while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
+
+               memcpy(&msgnum, cdbei->ptr, sizeof(long));
+
+               msg = CtdlFetchMessage(msgnum, 0);
+               if (msg != NULL) {
+                       CtdlFreeMessage(msg);   /* it still exists, so do nothing */
+               }
+               else {
+                       eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
+                       if (eptr != NULL) {
+                               eptr->next = el;
+                               eptr->ep_keylen = cdbei->len - sizeof(long);
+                               eptr->ep_key = malloc(cdbei->len);
+                               memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
+                               el = eptr;
+                       }
+                       ++purged;
+               }
+
+                cdb_free(cdbei);
 
-       extract(cmd, argbuf, 0);
-       if (!strcasecmp(cmd, "users")) {
-               retval = PurgeUsers();
-               cprintf("%d Purged %d users.\n", CIT_OK, retval);
-               return;
-       }
-       else if (!strcasecmp(cmd, "messages")) {
-               PurgeMessages();
-               cprintf("%d Expired %d messages.\n", CIT_OK, messages_purged);
-               return;
-       }
-       else if (!strcasecmp(cmd, "rooms")) {
-               retval = PurgeRooms();
-               cprintf("%d Expired %d rooms.\n", CIT_OK, retval);
-               return;
-       }
-       else if (!strcasecmp(cmd, "visits")) {
-               retval = PurgeVisits();
-               cprintf("%d Purged %d visits.\n", CIT_OK, retval);
-       }
-       else if (!strcasecmp(cmd, "usetable")) {
-               retval = PurgeUseTable();
-               cprintf("%d Purged %d entries from the use table.\n",
-                       CIT_OK, retval);
-       }
-       else if (!strcasecmp(cmd, "defrag")) {
-               defrag_databases();
-               cprintf("%d Defragmented the databases.\n", CIT_OK);
        }
-       else {
-               cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
-               return;
+
+       /* Phase 2: delete the records */
+       lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
+       while (el != NULL) {
+               cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
+               free(el->ep_key);
+               eptr = el->next;
+               free(el);
+               el = eptr;
        }
+
+       lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
+       return(purged);
+}
+
+
+void purge_databases(void) {
+       int retval;
+       static time_t last_purge = 0;
+       time_t now;
+       struct tm tm;
+
+       /* Do the auto-purge if the current hour equals the purge hour,
+        * but not if the operation has already been performed in the
+        * last twelve hours.  This is usually enough granularity.
+        */
+       now = time(NULL);
+       localtime_r(&now, &tm);
+       if (tm.tm_hour != config.c_purge_hour) return;
+       if ((now - last_purge) < 43200) return;
+
+       lprintf(CTDL_INFO, "Auto-purger: starting.\n");
+
+       retval = PurgeUsers();
+       lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
+
+       PurgeMessages();
+       lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
+
+       retval = PurgeRooms();
+       lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
+
+       retval = PurgeVisits();
+       lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
+
+       retval = PurgeUseTable();
+       lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
+
+       retval = PurgeEuidIndexTable();
+       lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
+
+       retval = TDAP_ProcessAdjRefCountQueue();
+       lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
+
+       lprintf(CTDL_INFO, "Auto-purger: finished.\n");
+
+       last_purge = now;       /* So we don't do it again soon */
 }
 
 /*****************************************************************************/
 
 
 void do_fsck_msg(long msgnum, void *userdata) {
-       struct roomref *ptr;
+       struct ctdlroomref *ptr;
 
-       ptr = (struct roomref *)mallok(sizeof(struct roomref));
+       ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
        ptr->next = rr;
        ptr->msgnum = msgnum;
        rr = ptr;
 }
 
-void do_fsck_room(struct quickroom *qrbuf, void *data)
+void do_fsck_room(struct ctdlroom *qrbuf, void *data)
 {
-       getroom(&CC->quickroom, qrbuf->QRname);
-       CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL,
-               do_fsck_msg, NULL);
+       getroom(&CC->room, qrbuf->QRname);
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
 }
 
 /*
@@ -662,14 +759,14 @@ void cmd_fsck(char *argbuf) {
        long msgnum;
        struct cdbdata *cdbmsg;
        struct MetaData smi;
-       struct roomref *ptr;
+       struct ctdlroomref *ptr;
        int realcount;
 
        if (CtdlAccessCheck(ac_aide)) return;
 
        /* Lame way of checking whether anyone else is doing this now */
        if (rr != NULL) {
-               cprintf("%d Another FSCK is already running.\n", ERROR);
+               cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
                return;
        }
 
@@ -699,9 +796,7 @@ void cmd_fsck(char *argbuf) {
 
                        if ( (smi.meta_refcount != realcount)
                           || (realcount == 0) ) {
-                               smi.meta_refcount = realcount;
-                               PutMetaData(&smi);
-                               AdjRefCount(msgnum, 0); /* deletes if needed */
+                               AdjRefCount(msgnum, (smi.meta_refcount - realcount));
                        }
 
                }
@@ -711,7 +806,7 @@ void cmd_fsck(char *argbuf) {
        cprintf("Freeing memory...\n");
        while (rr != NULL) {
                ptr = rr->next;
-               phree(rr);
+               free(rr);
                rr = ptr;
        }
 
@@ -725,9 +820,9 @@ void cmd_fsck(char *argbuf) {
 
 /*****************************************************************************/
 
-char *Dynamic_Module_Init(void)
+char *serv_expire_init(void)
 {
-       CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
+       CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
        CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
        return "$Id$";
 }