use the gnu format string checker for CtdlLogPrintf; fix associated new warnings...
[citadel.git] / citadel / modules / expire / serv_expire.c
index d15623a33b3bc51af6bec20b6bbb4c5ec6b1cf35..22442ad1526cc255ff5dfbad339fd39d096fdd6d 100644 (file)
@@ -1,12 +1,25 @@
 /*
- * $Id$
- *
  * This module handles the expiry of old messages and the purging of old users.
  *
- */
-
-
-/*
+ * You might also see this module affectionately referred to as the DAP (the Dreaded Auto-Purger).
+ *
+ * Copyright (c) 1988-2009 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
+ *
+ *  This program is free 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.
+ *
+ *  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
+ *
+ *
  * A brief technical discussion:
  *
  * Several of the purge operations found in this module operate in two
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "room_ops.h"
 #include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "user_ops.h"
 #include "control.h"
-#include "serv_network.h"      /* Needed for defenition of UseTable */
+#include "serv_network.h"      /* Needed for definition of UseTable */
 #include "threads.h"
+#include "context.h"
 
 #include "ctdl_module.h"
 
@@ -121,10 +134,8 @@ int messages_purged;
 int users_not_purged;
 char *users_corrupt_msg = NULL;
 char *users_zero_msg = NULL;
-
 struct ctdlroomref *rr = NULL;
-
-extern struct CitContext *ContextList;
+int force_purge_now = 0;                       /* set to nonzero to force a run right now */
 
 
 /*
@@ -152,6 +163,10 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
        if (epbuf.expire_mode == EXPIRE_MANUAL) return;
 
+       /* Don't purge messages containing system configuration, dumbass. */
+       if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
+
+       /* Ok, we got this far ... now let's see what's in the room */
         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
 
         if (cdbfr != NULL) {
@@ -167,6 +182,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
                return;
        }
 
+
        /* If the room is set to expire by count, do that */
        if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
                if (num_msgs > epbuf.expire_value) {
@@ -241,7 +257,7 @@ void PurgeMessages(void) {
                return;
        }
 
-       ForEachRoom(GatherPurgeMessages, (void *)purgelist );
+       CtdlForEachRoom(GatherPurgeMessages, (void *)purgelist );
        DoPurgeMessages(purgelist);
        fclose(purgelist);
 }
@@ -290,8 +306,9 @@ void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
                if (qrbuf->QRflags & QR_PERMANENT) return;
                if (qrbuf->QRflags & QR_DIRECTORY) return;
                if (qrbuf->QRflags & QR_NETWORK) return;
+               if (qrbuf->QRflags2 & QR2_SYSTEM) return;
                if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
-               if (is_noneditable(qrbuf)) return;
+               if (CtdlIsNonEditable(qrbuf)) return;
 
                /* If we don't know the modification date, be safe and don't purge */
                if (qrbuf->QRmtime <= (time_t)0) return;
@@ -333,7 +350,7 @@ int PurgeRooms(void) {
        ForEachUser(AddValidUser, NULL);
 
        /* Then cycle through the room file */
-       ForEachRoom(DoPurgeRooms, NULL);
+       CtdlForEachRoom(DoPurgeRooms, NULL);
 
        /* Free the valid user list */
        while (ValidUserList != NULL) {
@@ -347,11 +364,11 @@ int PurgeRooms(void) {
        strcpy(transcript, "The following rooms have been auto-purged:\n");
 
        while (RoomPurgeList != NULL) {
-               if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
+               if (CtdlGetRoom(&qrbuf, RoomPurgeList->name) == 0) {
                        transcript=realloc(transcript, strlen(transcript)+SIZ);
                        snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                                qrbuf.QRname);
-                       delete_room(&qrbuf);
+                       CtdlDeleteRoom(&qrbuf);
                }
                pptr = RoomPurgeList->next;
                free(RoomPurgeList);
@@ -359,7 +376,7 @@ int PurgeRooms(void) {
                ++num_rooms_purged;
        }
 
-       if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
+       if (num_rooms_purged > 0) CtdlAideMessage(transcript, "Room Autopurger Message");
        free(transcript);
 
        CtdlLogPrintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
@@ -410,6 +427,10 @@ void do_user_purge(struct ctdluser *us, void *data) {
 
        /* The default rule is to not purge. */
        purge = 0;
+       
+       /* don't attempt to purge system users. */
+       if (!strncmp(us->fullname, "SYS_", 4))
+               goto skip_all_this;
 
        /* If the user hasn't called in two months and expiring of accounts is turned on, his/her account
         * has expired, so purge the record.
@@ -446,13 +467,22 @@ void do_user_purge(struct ctdluser *us, void *data) {
         */
        if (us->timescalled < 0) purge = 1;
 
-       /* User number 0, as well as any negative user number, is
+       /* any negative user number, is
         * also impossible.
         */
        if (us->usernum < 0L) purge = 1;
        
        /** Don't purge user 0. That user is there for the system */
-       if (us->usernum == 0) purge = 0;
+       if (us->usernum == 0L)
+       {
+               /* FIXME: Temporary log message. Until we do unauth access with user 0 we should
+                * try to get rid of all user 0 occurences. Many will be remnants from old code so
+                * we will need to try and purge them from users data bases.Some will not have names but
+                * those with names should be purged.
+                */
+               CtdlLogPrintf(CTDL_DEBUG, "Auto purger found a user 0 with name \"%s\"\n", us->fullname);
+               // purge = 0;
+       }
        
        /* If the user has no full name entry then we can't purge them
         * since the actual purge can't find them.
@@ -461,11 +491,6 @@ void do_user_purge(struct ctdluser *us, void *data) {
        if (IsEmptyStr(us->fullname))
        {
                purge = 0;
-/*
- ** Keeping this block of code for later referance.
- * We don't actually want to send the user 0 messages as part of the purger cycle
- * But we might want to use some of this code to do a user database integrity check
- * some time in the future.
                
                if (us->usernum > 0L)
                {
@@ -474,43 +499,19 @@ void do_user_purge(struct ctdluser *us, void *data) {
                        {
                                users_corrupt_msg = malloc(SIZ);
                                strcpy(users_corrupt_msg, "The auto-purger found the following user numbers with no name.\n"
-                               "If the user number is 0 you should report this to the Citadel development\n"
-                               "team either by a bugzilla report at http://bugzilla.citadel.org or\n"
-                               "posting a message in the Citadel Support room on Uncensored at\n"
-                               "https://uncensored.citadel.org You should make it clear that you have seen a\n"
-                               "user 0 messages in the Aide room which means a module has not named its\n"
-                               "private context.\n"
-                               "Unfortunately the auto-purger is not yet able to fix this problem.\n"
-                               "This problem is not considered serious since a user with no name can\n"
-                               "not log in.\n");
+                               "The system has no way to purge user with no name and should not be able to\n"
+                               "create them either.\n"
+                               "This indicates corruption of the user DB or possibly a bug.\n"
+                               "It may be a good idea to restore your DB from a backup.\n");
                        }
                
-                       users_corrupt_msg=realloc(users_corrupt_msg, strlen(users_corrupt_msg)+SIZ);
-                       snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], SIZ, " %ld\n", us->usernum);
+                       users_corrupt_msg=realloc(users_corrupt_msg, strlen(users_corrupt_msg)+30);
+                       snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], 29, " %ld\n", us->usernum);
                }
-               else if (us->usernum == 0L)
-               {
-                       purge=0;
-                       if (users_zero_msg == NULL)
-                       {
-                               users_zero_msg = malloc(SIZ);
-                               strcpy(users_zero_msg, "The auto-purger found a user with a user number of 0 but no name.\n"
-                               "This is the result of a bug where a private contaxt has been created but\n"
-                               "not named.\n\n"
-                               "Please report this to the Citadel development team either by a bugzilla\n"
-                               "report at http://bugzilla.citadel.org or by posting a message in the\n"
-                               "Citadel Support room on Uncensored at https://uncensored.citadel.org\n"
-                               "You should make it clear that you have seen a user 0 messages in the\n"
-                               "Aide room which means a module has not named its private context.\n\n"
-                               "This problem is not considered serious since it does not constitute a\n"
-                               "security risk and should not impare system operation.\n"
-                               );
-                       }
-               }
-*/
        }
 
-
+skip_all_this:
+               
        if (purge == 1) {
                pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = UserPurgeList;
@@ -541,7 +542,8 @@ int PurgeUsers(void) {
                        ForEachUser(do_uid_user_purge, NULL);
                        break;
                default:
-                       CtdlLogPrintf(CTDL_DEBUG, "Unknown authentication mode!\n");
+                       CtdlLogPrintf(CTDL_DEBUG, "User purge for auth mode %d is not implemented.\n",
+                               config.c_auth_mode);
                        break;
        }
 
@@ -574,19 +576,19 @@ int PurgeUsers(void) {
                }
        }
 
-       if (num_users_purged > 0) aide_message(transcript, "User Purge Message");
+       if (num_users_purged > 0) CtdlAideMessage(transcript, "User Purge Message");
        free(transcript);
 
        if(users_corrupt_msg)
        {
-               aide_message(users_corrupt_msg, "User Corruption Message");
+               CtdlAideMessage(users_corrupt_msg, "User Corruption Message");
                free (users_corrupt_msg);
                users_corrupt_msg = NULL;
        }
        
        if(users_zero_msg)
        {
-               aide_message(users_zero_msg, "User Zero Message");
+               CtdlAideMessage(users_zero_msg, "User Zero Message");
                free (users_zero_msg);
                users_zero_msg = NULL;
        }
@@ -610,7 +612,7 @@ int PurgeUsers(void) {
  */
 int PurgeVisits(void) {
        struct cdbdata *cdbvisit;
-       struct visit vbuf;
+       visit vbuf;
        struct VPurgeList *VisitPurgeList = NULL;
        struct VPurgeList *vptr;
        int purged = 0;
@@ -621,7 +623,7 @@ int PurgeVisits(void) {
        int RoomIsValid, UserIsValid;
 
        /* First, load up a table full of valid room/gen combinations */
-       ForEachRoom(AddValidRoom, NULL);
+       CtdlForEachRoom(AddValidRoom, NULL);
 
        /* Then load up a table full of valid user numbers */
        ForEachUser(AddValidUser, NULL);
@@ -629,10 +631,10 @@ int PurgeVisits(void) {
        /* Now traverse through the visits, purging irrelevant records... */
        cdb_rewind(CDB_VISIT);
        while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
-               memset(&vbuf, 0, sizeof(struct visit));
+               memset(&vbuf, 0, sizeof(visit));
                memcpy(&vbuf, cdbvisit->ptr,
-                       ( (cdbvisit->len > sizeof(struct visit)) ?
-                       sizeof(struct visit) : cdbvisit->len) );
+                       ( (cdbvisit->len > sizeof(visit)) ?
+                         sizeof(visit) : cdbvisit->len) );
                cdb_free(cdbvisit);
 
                RoomIsValid = 0;
@@ -813,8 +815,9 @@ int PurgeStaleOpenIDassociations(void) {
        char *deleteme = NULL;
        long len;
        void *Value;
-       char *Key;
+       const char *Key;
        int num_deleted = 0;
+       long usernum = 0L;
 
        keys = NewHash(1, NULL);
        if (!keys) return(0);
@@ -823,11 +826,10 @@ int PurgeStaleOpenIDassociations(void) {
        cdb_rewind(CDB_OPENID);
        while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
                if (cdboi->len > sizeof(long)) {
-                       long usernum;
-                       usernum = ((long)*(cdboi->ptr));
-                       if (getuserbynumber(&usbuf, usernum) != 0) {
+                       memcpy(&usernum, cdboi->ptr, sizeof(long));
+                       if (CtdlGetUserByNumber(&usbuf, usernum) != 0) {
                                deleteme = strdup(cdboi->ptr + sizeof(long)),
-                               Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
+                               Put(keys, deleteme, strlen(deleteme), deleteme, NULL);
                        }
                }
                cdb_free(cdboi);
@@ -835,10 +837,10 @@ int PurgeStaleOpenIDassociations(void) {
 
        /* Go through the hash list, deleting keys we stored in it */
 
-       HashPos = GetNewHashPos();
+       HashPos = GetNewHashPos(keys, 0);
        while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
        {
-               CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
+               CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n",  (char*)Value);
                cdb_delete(CDB_OPENID, Value, strlen(Value));
                /* note: don't free(Value) -- deleting the hash list will handle this for us */
                ++num_deleted;
@@ -860,10 +862,9 @@ void *purge_databases(void *args)
         struct tm tm;
        struct CitContext purgerCC;
 
-       CtdlLogPrintf(CTDL_DEBUG, "Auto-purger_thread() initializing\n");
-
-       CtdlFillPrivateContext(&purgerCC, "purger");
+       CtdlFillSystemContext(&purgerCC, "purger");
        citthread_setspecific(MyConKey, (void *)&purgerCC );
+       CtdlLogPrintf(CTDL_DEBUG, "Auto-purger_thread() initializing\n");
 
         while (!CtdlThreadCheckStop()) {
                 /* Do the auto-purge if the current hour equals the purge hour,
@@ -872,7 +873,10 @@ void *purge_databases(void *args)
                  */
                 now = time(NULL);
                 localtime_r(&now, &tm);
-                if ((tm.tm_hour != config.c_purge_hour) || ((now - last_purge) < 43200)) {
+                if (
+                       ((tm.tm_hour != config.c_purge_hour) || ((now - last_purge) < 43200))
+                       && (force_purge_now == 0)
+               ) {
                         CtdlThreadSleep(60);
                         continue;
                 }
@@ -932,16 +936,22 @@ void *purge_databases(void *args)
                {
                        CtdlLogPrintf(CTDL_INFO, "Auto-purger: finished.\n");
                        last_purge = now;       /* So we don't do it again soon */
+                       force_purge_now = 0;
                }
                else
                        CtdlLogPrintf(CTDL_INFO, "Auto-purger: STOPPED.\n");
 
         }
+       CtdlClearSystemContext();
         return NULL;
 }
 /*****************************************************************************/
 
 
+/* The FSCK command has been removed because people were misusing it */
+
+#if 0
+
 void do_fsck_msg(long msgnum, void *userdata) {
        struct ctdlroomref *ptr;
 
@@ -953,7 +963,7 @@ void do_fsck_msg(long msgnum, void *userdata) {
 
 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
 {
-       getroom(&CC->room, qrbuf->QRname);
+       CtdlGetRoom(&CC->room, qrbuf->QRname);
        CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
 }
 
@@ -979,7 +989,7 @@ void cmd_fsck(char *argbuf) {
 
        cprintf("\nThis could take a while.  Please be patient!\n\n");
        cprintf("Gathering pointers...\n");
-       ForEachRoom(do_fsck_room, NULL);
+       CtdlForEachRoom(do_fsck_room, NULL);
 
        get_control();
        cprintf("Checking message base...\n");
@@ -1020,7 +1030,16 @@ void cmd_fsck(char *argbuf) {
 
 }
 
+#endif /* end of commented-out fsck cmd */
 
+/*
+ * Manually initiate a run of The Dreaded Auto-Purger (tm)
+ */
+void cmd_tdap(char *argbuf) {
+       if (CtdlAccessCheck(ac_aide)) return;
+       force_purge_now = 1;
+       cprintf("%d Manually initiating a purger run now.\n", CIT_OK);
+}
 
 
 /*****************************************************************************/
@@ -1029,10 +1048,14 @@ CTDL_MODULE_INIT(expire)
 {
        if (!threading)
        {
-               CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
+               /* CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts"); */
+               CtdlRegisterProtoHook(cmd_tdap, "TDAP", "Manually initiate auto-purger");
+
+               CtdlRegisterProtoHook(cmd_gpex, "GPEX", "Get expire policy");
+               CtdlRegisterProtoHook(cmd_spex, "SPEX", "Set expire policy");
        }
        else
                CtdlThreadCreate("Auto Purger", CTDLTHREAD_BIGSTACK, purge_databases, NULL);
        /* return our Subversion id for the Log */
-       return "$Id$";
+       return "expire";
 }