]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_expire.c
* Replaced most of the very repetitive and very redundant access level checks
[citadel.git] / citadel / serv_expire.c
index c566bba045f015cff6b8b0d4c2134e854c334066..f8f2366e182d96b36a96d42ec14c0159ffc780a6 100644 (file)
@@ -36,9 +36,6 @@
 #include <sys/wait.h>
 #include <string.h>
 #include <limits.h>
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
-#endif
 #include "citadel.h"
 #include "server.h"
 #include <syslog.h>
@@ -87,15 +84,24 @@ struct ValidUser {
        long vu_usernum;
 };
 
+
+struct roomref {
+       struct roomref *next;
+       long msgnum;
+};
+
+
 struct PurgeList *UserPurgeList = NULL;
 struct PurgeList *RoomPurgeList = NULL;
 struct ValidRoom *ValidRoomList = NULL;
 struct ValidUser *ValidUserList = NULL;
 int messages_purged;
 
+struct roomref *rr = NULL;
+
 extern struct CitContext *ContextList;
 
-void DoPurgeMessages(struct quickroom *qrbuf) {
+void DoPurgeMessages(struct quickroom *qrbuf, void *data) {
        struct ExpirePolicy epbuf;
        long delnum;
        time_t xtime, now;
@@ -179,11 +185,11 @@ void DoPurgeMessages(struct quickroom *qrbuf) {
 void PurgeMessages(void) {
        lprintf(5, "PurgeMessages() called\n");
        messages_purged = 0;
-       ForEachRoom(DoPurgeMessages);
+       ForEachRoom(DoPurgeMessages, NULL);
 }
 
 
-void AddValidUser(struct usersupp *usbuf) {
+void AddValidUser(struct usersupp *usbuf, void *data) {
        struct ValidUser *vuptr;
 
        vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
@@ -192,7 +198,7 @@ void AddValidUser(struct usersupp *usbuf) {
        ValidUserList = vuptr;
 }
 
-void AddValidRoom(struct quickroom *qrbuf) {
+void AddValidRoom(struct quickroom *qrbuf, void *data) {
        struct ValidRoom *vrptr;
 
        vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
@@ -202,7 +208,7 @@ void AddValidRoom(struct quickroom *qrbuf) {
        ValidRoomList = vrptr;
 }
 
-void DoPurgeRooms(struct quickroom *qrbuf) {
+void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
        time_t age, purge_secs;
        struct PurgeList *pptr;
        struct ValidUser *vuptr;
@@ -228,6 +234,7 @@ void DoPurgeRooms(struct quickroom *qrbuf) {
        if (qrbuf->QRflags & QR_PERMANENT) return;
        if (qrbuf->QRflags & QR_DIRECTORY) return;
        if (qrbuf->QRflags & QR_NETWORK) return;
+       if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
        if (is_noneditable(qrbuf)) return;
 
        /* If we don't know the modification date, be safe and don't purge */
@@ -266,10 +273,10 @@ int PurgeRooms(void) {
 
        /* Load up a table full of valid user numbers so we can delete
         * user-owned rooms for users who no longer exist */
-       ForEachUser(AddValidUser);
+       ForEachUser(AddValidUser, NULL);
 
        /* Then cycle through the room file */
-       ForEachRoom(DoPurgeRooms);
+       ForEachRoom(DoPurgeRooms, NULL);
 
        /* Free the valid user list */
        while (ValidUserList != NULL) {
@@ -303,7 +310,7 @@ int PurgeRooms(void) {
 }
 
 
-void do_user_purge(struct usersupp *us) {
+void do_user_purge(struct usersupp *us, void *data) {
        int purge;
        time_t now;
        time_t purge_time;
@@ -364,7 +371,7 @@ int PurgeUsers(void) {
 
        lprintf(5, "PurgeUsers() called\n");
        if (config.c_userpurge > 0) {
-               ForEachUser(do_user_purge);
+               ForEachUser(do_user_purge, NULL);
        }
 
        transcript = mallok(256);
@@ -414,10 +421,10 @@ int PurgeVisits(void) {
        int RoomIsValid, UserIsValid;
 
        /* First, load up a table full of valid room/gen combinations */
-       ForEachRoom(AddValidRoom);
+       ForEachRoom(AddValidRoom, NULL);
 
        /* Then load up a table full of valid user numbers */
-       ForEachUser(AddValidUser);
+       ForEachUser(AddValidUser, NULL);
 
        /* Now traverse through the visits, purging irrelevant records... */
        cdb_rewind(CDB_VISIT);
@@ -492,17 +499,7 @@ void cmd_expi(char *argbuf) {
        char cmd[256];
        int retval;
 
-
-       if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
-               cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
-               return;
-       }
-
-       if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
-               cprintf("%d Higher access required.\n",
-                       ERROR+HIGHER_ACCESS_REQUIRED);
-               return;
-       }
+       if (CtdlAccessCheck(ac_aide)) return;
 
        extract(cmd, argbuf, 0);
        if (!strcasecmp(cmd, "users")) {
@@ -534,10 +531,97 @@ void cmd_expi(char *argbuf) {
        }
 }
 
+/*****************************************************************************/
+
+
+void do_fsck_msg(long msgnum) {
+       struct roomref *ptr;
+
+       ptr = (struct roomref *)mallok(sizeof(struct roomref));
+       ptr->next = rr;
+       ptr->msgnum = msgnum;
+       rr = ptr;
+}
+
+void do_fsck_room(struct quickroom *qrbuf, void *data)
+{
+       getroom(&CC->quickroom, qrbuf->QRname);
+       CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL, do_fsck_msg);
+}
+
+/*
+ * Check message reference counts
+ */
+void cmd_fsck(char *argbuf) {
+       long msgnum;
+       struct cdbdata *cdbmsg;
+       struct SuppMsgInfo smi;
+       struct roomref *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);
+               return;
+       }
+
+       cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
+
+       cprintf("\nThis could take a while.  Please be patient!\n\n");
+       cprintf("Gathering pointers...\n");
+       ForEachRoom(do_fsck_room, NULL);
+
+       get_control();
+       cprintf("Checking message base...\n");
+       for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
+
+               cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
+               if (cdbmsg != NULL) {
+                       cdb_free(cdbmsg);
+                       cprintf("Message %7ld    ", msgnum);
+
+                       GetSuppMsgInfo(&smi, msgnum);
+                       cprintf("refcount=%-2d   ", smi.smi_refcount);
+
+                       realcount = 0;
+                       for (ptr = rr; ptr != NULL; ptr = ptr->next) {
+                               if (ptr->msgnum == msgnum) ++realcount;
+                       }
+                       cprintf("realcount=%-2d\n", realcount);
+
+                       if ( (smi.smi_refcount != realcount)
+                          || (realcount == 0) ) {
+                               smi.smi_refcount = realcount;
+                               PutSuppMsgInfo(&smi);
+                               AdjRefCount(msgnum, 0); /* deletes if needed */
+                       }
+
+               }
+
+       }
+
+       cprintf("Freeing memory...\n");
+       while (rr != NULL) {
+               ptr = rr->next;
+               phree(rr);
+               rr = ptr;
+       }
+
+       cprintf("Done!\n");
+       cprintf("000\n");
+
+}
+
+
+
 
+/*****************************************************************************/
 
 char *Dynamic_Module_Init(void)
 {
        CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
+       CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
        return "$Id$";
 }