Modified the behavior of ForEachUser() to do the two phase load/perform cycle
[citadel.git] / citadel / modules / expire / serv_expire.c
index 93ad7e31340cb8cb41728a50e6f880727e027e27..09e9eab1093fada7ae5418c40d64024448c755b8 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * 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).
+ * You might also see this module affectionately referred to as TDAP (The Dreaded Auto-Purger).
  *
- * Copyright (c) 1988-2011 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
+ * Copyright (c) 1988-2019 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
  *
  * 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
  * 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
- * stages: the first stage generates a linked list of objects to be deleted,
- * 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 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.
- *
- * When using Berkeley DB, there's another reason for the two-phase purge: we
- * don't want the entire thing being done as one huge transaction.
- *
- * You'll also notice that we build the in-memory list of records to be deleted
- * sometimes with a linked list and sometimes with a hash table.  There is no
- * reason for this aside from the fact that the linked list ones were written
- * before we had the hash table library available.
  */
 
 
@@ -107,7 +81,6 @@ struct ValidUser {
        long vu_usernum;
 };
 
-
 struct ctdlroomref {
        struct ctdlroomref *next;
        long msgnum;
@@ -181,7 +154,6 @@ 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) {
@@ -197,10 +169,10 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
                for (a=0; a<num_msgs; ++a) {
                        delnum = msglist[a];
 
-                       msg = CtdlFetchMessage(delnum, 0); /* dont need body */
+                       msg = CtdlFetchMessage(delnum, 0, 1); /* dont need body */
                        if (msg != NULL) {
                                xtime = atol(msg->cm_fields[eTimestamp]);
-                               CtdlFreeMessage(msg);
+                               CM_Free(msg);
                        } else {
                                xtime = 0L;
                        }
@@ -261,12 +233,17 @@ void PurgeMessages(void) {
 }
 
 
-void AddValidUser(struct ctdluser *usbuf, void *data) {
+void AddValidUser(char *username, void *data) {
        struct ValidUser *vuptr;
+       struct ctdluser usbuf;
+
+       if (CtdlGetUser(&usbuf, username) != 0) {
+               return;
+       }
 
        vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
        vuptr->next = ValidUserList;
-       vuptr->vu_usernum = usbuf->usernum;
+       vuptr->vu_usernum = usbuf.usernum;
        ValidUserList = vuptr;
 }
 
@@ -312,11 +289,11 @@ void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
                if (qrbuf->QRmtime <= (time_t)0) return;
 
                /* If no room purge time is set, be safe and don't purge */
-               if (config.c_roompurge < 0) return;
+               if (CtdlGetConfigLong("c_roompurge") < 0) return;
 
                /* Otherwise, check the date of last modification */
                age = time(NULL) - (qrbuf->QRmtime);
-               purge_secs = (time_t)config.c_roompurge * (time_t)86400;
+               purge_secs = CtdlGetConfigLong("c_roompurge") * 86400;
                if (purge_secs <= (time_t)0) return;
                syslog(LOG_DEBUG, "<%s> is <%ld> seconds old", qrbuf->QRname, (long)age);
                if (age > purge_secs) do_purge = 1;
@@ -332,7 +309,6 @@ void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
 }
 
 
-
 int PurgeRooms(void) {
        struct PurgeList *pptr;
        int num_rooms_purged = 0;
@@ -342,7 +318,6 @@ int PurgeRooms(void) {
 
        syslog(LOG_DEBUG, "PurgeRooms() called");
 
-
        /* Load up a table full of valid user numbers so we can delete
         * user-owned rooms for users who no longer exist */
        ForEachUser(AddValidUser, NULL);
@@ -357,7 +332,6 @@ int PurgeRooms(void) {
                ValidUserList = vuptr;
        }
 
-
        transcript = malloc(SIZ);
        strcpy(transcript, "The following rooms have been auto-purged:\n");
 
@@ -382,45 +356,26 @@ int PurgeRooms(void) {
 }
 
 
-/*
- * 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;
-               }
-       }
-       else {
-               ++users_not_purged;
-       }
-}
-
-
-
-
 /*
  * Back end function to check user accounts for expiration.
  */
-void do_user_purge(struct ctdluser *us, void *data) {
+void do_user_purge(char *username, void *data) {
        int purge;
        time_t now;
        time_t purge_time;
        struct PurgeList *pptr;
+       struct ctdluser us;
+
+       if (CtdlGetUser(&us, username) != 0) {
+               return;
+       }
 
        /* Set purge time; if the user overrides the system default, use it */
-       if (us->USuserpurge > 0) {
-               purge_time = ((time_t)us->USuserpurge) * 86400L;
+       if (us.USuserpurge > 0) {
+               purge_time = ((time_t)us.USuserpurge) * 86400;
        }
        else {
-               purge_time = ((time_t)config.c_userpurge) * 86400L;
+               purge_time = CtdlGetConfigLong("c_userpurge") * 86400;
        }
 
        /* The default rule is to not purge. */
@@ -429,52 +384,52 @@ void do_user_purge(struct ctdluser *us, void *data) {
        /* 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.
         */
-       if (config.c_userpurge > 0)
+       if (CtdlGetConfigLong("c_userpurge") > 0)
        {
                now = time(NULL);
-               if ((now - us->lastcall) > purge_time) purge = 1;
+               if ((now - us.lastcall) > purge_time) purge = 1;
        }
 
        /* If the record is marked as permanent, don't purge it.
         */
-       if (us->flags & US_PERM) purge = 0;
+       if (us.flags & US_PERM) purge = 0;
 
        /* If the user is an Aide, don't purge him/her/it.
         */
-       if (us->axlevel == 6) purge = 0;
+       if (us.axlevel == 6) purge = 0;
 
        /* If the access level is 0, the record should already have been
         * deleted, but maybe the user was logged in at the time or something.
         * Delete the record now.
         */
-       if (us->axlevel == 0) purge = 1;
+       if (us.axlevel == 0) purge = 1;
 
        /* If the user set his/her password to 'deleteme', he/she
         * wishes to be deleted, so purge the record.
         * Moved this lower down so that aides and permanent users get purged if they ask to be.
         */
-       if (!strcasecmp(us->password, "deleteme")) purge = 1;
+       if (!strcasecmp(us.password, "deleteme")) purge = 1;
        
        /* 0 calls is impossible.  If there are 0 calls, it must
         * be a corrupted record, so purge it.
         * Actually it is possible if an Aide created the user so now we check for less than 0 (DRW)
         */
-       if (us->timescalled < 0) purge = 1;
+       if (us.timescalled < 0) purge = 1;
 
        /* any negative user number, is
         * also impossible.
         */
-       if (us->usernum < 0L) purge = 1;
+       if (us.usernum < 0L) purge = 1;
        
        /* Don't purge user 0. That user is there for the system */
-       if (us->usernum == 0L)
+       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.
                 */
-               syslog(LOG_DEBUG, "Auto purger found a user 0 with name <%s>", us->fullname);
+               syslog(LOG_DEBUG, "Auto purger found a user 0 with name <%s>", us.fullname);
                // purge = 0;
        }
        
@@ -482,11 +437,11 @@ void do_user_purge(struct ctdluser *us, void *data) {
         * since the actual purge can't find them.
         * This shouldn't happen but does somehow.
         */
-       if (IsEmptyStr(us->fullname))
+       if (IsEmptyStr(us.fullname))
        {
                purge = 0;
                
-               if (us->usernum > 0L)
+               if (us.usernum > 0L)
                {
                        purge=0;
                        if (users_corrupt_msg == NULL)
@@ -502,14 +457,14 @@ void do_user_purge(struct ctdluser *us, void *data) {
                        }
                
                        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);
+                       snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], 29, " %ld\n", us.usernum);
                }
        }
 
        if (purge == 1) {
                pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = UserPurgeList;
-               strcpy(pptr->name, us->fullname);
+               strcpy(pptr->name, us.fullname);
                UserPurgeList = pptr;
        }
        else {
@@ -519,7 +474,6 @@ void do_user_purge(struct ctdluser *us, void *data) {
 }
 
 
-
 int PurgeUsers(void) {
        struct PurgeList *pptr;
        int num_users_purged = 0;
@@ -528,16 +482,12 @@ int PurgeUsers(void) {
        syslog(LOG_DEBUG, "PurgeUsers() called");
        users_not_purged = 0;
 
-       switch(config.c_auth_mode) {
+       switch(CtdlGetConfigInt("c_auth_mode")) {
                case AUTHMODE_NATIVE:
                        ForEachUser(do_user_purge, NULL);
                        break;
-               case AUTHMODE_HOST:
-                       ForEachUser(do_uid_user_purge, NULL);
-                       break;
                default:
-                       syslog(LOG_DEBUG, "User purge for auth mode %d is not implemented.",
-                               config.c_auth_mode);
+                       syslog(LOG_DEBUG, "User purge for auth mode %d is not implemented.", CtdlGetConfigInt("c_auth_mode"));
                        break;
        }
 
@@ -573,15 +523,13 @@ int PurgeUsers(void) {
        if (num_users_purged > 0) CtdlAideMessage(transcript, "User Purge Message");
        free(transcript);
 
-       if(users_corrupt_msg)
-       {
+       if (users_corrupt_msg) {
                CtdlAideMessage(users_corrupt_msg, "User Corruption Message");
                free (users_corrupt_msg);
                users_corrupt_msg = NULL;
        }
        
-       if(users_zero_msg)
-       {
+       if(users_zero_msg) {
                CtdlAideMessage(users_zero_msg, "User Zero Message");
                free (users_zero_msg);
                users_zero_msg = NULL;
@@ -690,6 +638,7 @@ int PurgeVisits(void) {
        return(purged);
 }
 
+
 /*
  * Purge the use table of old entries.
  *
@@ -702,32 +651,25 @@ int PurgeUseTable(StrBuf *ErrMsg) {
        struct UPurgeList *uptr; 
 
        /* Phase 1: traverse through the table, discovering old records... */
-       if (CheckTDAPVeto(CDB_USETABLE, ErrMsg))
-       {
-               syslog(LOG_DEBUG, "Purge use table: VETO!");
-               return 0;
-       }
 
        syslog(LOG_DEBUG, "Purge use table: phase 1");
        cdb_rewind(CDB_USETABLE);
-       while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
-
-               /*
-                * TODODRW: change this to create a new function time_t cdb_get_timestamp( struct cdbdata *)
-                * this will release this file from the serv_network.h
-                * Maybe it could be a macro that extracts and casts the reult
-                */
+       while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL)
+       {
                if (cdbut->len > sizeof(struct UseTable))
                        memcpy(&ut, cdbut->ptr, sizeof(struct UseTable));
-               else {
+               else
+               {
                        memset(&ut, 0, sizeof(struct UseTable));
                        memcpy(&ut, cdbut->ptr, cdbut->len);
                }
                cdb_free(cdbut);
 
-               if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
+               if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN )
+               {
                        uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
-                       if (uptr != NULL) {
+                       if (uptr != NULL)
+                       {
                                uptr->next = ul;
                                safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
                                ul = uptr;
@@ -751,7 +693,6 @@ int PurgeUseTable(StrBuf *ErrMsg) {
 }
 
 
-
 /*
  * Purge the EUID Index of old records.
  *
@@ -771,9 +712,9 @@ int PurgeEuidIndexTable(void) {
 
                memcpy(&msgnum, cdbei->ptr, sizeof(long));
 
-               msg = CtdlFetchMessage(msgnum, 0);
+               msg = CtdlFetchMessage(msgnum, 0, 1);
                if (msg != NULL) {
-                       CtdlFreeMessage(msg);   /* it still exists, so do nothing */
+                       CM_Free(msg);   /* it still exists, so do nothing */
                }
                else {
                        eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
@@ -806,11 +747,10 @@ int PurgeEuidIndexTable(void) {
 }
 
 
-
 /*
- * Purge OpenID assocations for missing users (theoretically this will never delete anything)
+ * Purge external auth assocations for missing users (theoretically this will never delete anything)
  */
-int PurgeStaleOpenIDassociations(void) {
+int PurgeStaleExtAuthAssociations(void) {
        struct cdbdata *cdboi;
        struct ctdluser usbuf;
        HashList *keys = NULL;
@@ -826,8 +766,8 @@ int PurgeStaleOpenIDassociations(void) {
        if (!keys) return(0);
 
 
-       cdb_rewind(CDB_OPENID);
-       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+       cdb_rewind(CDB_EXTAUTH);
+       while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
                if (cdboi->len > sizeof(long)) {
                        memcpy(&usernum, cdboi->ptr, sizeof(long));
                        if (CtdlGetUserByNumber(&usbuf, usernum) != 0) {
@@ -843,8 +783,8 @@ int PurgeStaleOpenIDassociations(void) {
        HashPos = GetNewHashPos(keys, 0);
        while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
        {
-               syslog(LOG_DEBUG, "Deleting associated OpenID <%s>",  (char*)Value);
-               cdb_delete(CDB_OPENID, Value, strlen(Value));
+               syslog(LOG_DEBUG, "Deleting associated external authenticator <%s>",  (char*)Value);
+               cdb_delete(CDB_EXTAUTH, Value, strlen(Value));
                /* note: don't free(Value) -- deleting the hash list will handle this for us */
                ++num_deleted;
        }
@@ -854,9 +794,6 @@ int PurgeStaleOpenIDassociations(void) {
 }
 
 
-
-
-
 void purge_databases(void)
 {
        int retval;
@@ -870,10 +807,8 @@ void purge_databases(void)
         */
        now = time(NULL);
        localtime_r(&now, &tm);
-       if (
-               ((tm.tm_hour != config.c_purge_hour) || ((now - last_purge) < 43200))
-               && (force_purge_now == 0)
-       ) {
+       if (((tm.tm_hour != CtdlGetConfigInt("c_purge_hour")) || ((now - last_purge) < 43200)) && (force_purge_now == 0))
+       {
                        return;
        }
 
@@ -910,7 +845,6 @@ void purge_databases(void)
                ErrMsg = NewStrBuf ();
                retval = PurgeUseTable(ErrMsg);
                        syslog(LOG_NOTICE, "Purged %d entries from the use table.", retval);
-////TODO: fix errmsg
                FreeStrBuf(&ErrMsg);
        }
 
@@ -922,20 +856,24 @@ void purge_databases(void)
 
        if (!server_shutting_down)
        {
-               retval = PurgeStaleOpenIDassociations();
-               syslog(LOG_NOTICE, "Purged %d stale OpenID associations.", retval);
+               retval = PurgeStaleExtAuthAssociations();
+               syslog(LOG_NOTICE, "Purged %d stale external auth associations.", retval);
        }
 
-       if (!server_shutting_down)
+       //if (!server_shutting_down)
+       //{
+       //      FIXME this is where we could do a non-interactive delete of zero-refcount messages
+       //}
+
+       if ( (!server_shutting_down) && (CtdlGetConfigInt("c_shrink_db_files") != 0) )
        {
-                       retval = TDAP_ProcessAdjRefCountQueue();
-               syslog(LOG_NOTICE, "Processed %d message reference count adjustments.", retval);
+               cdb_compact();                                  // Shrink the DB files on disk
        }
 
        if (!server_shutting_down)
        {
                syslog(LOG_INFO, "Auto-purger: finished.");
-               last_purge = now;       /* So we don't do it again soon */
+               last_purge = now;                               // So we don't do it again soon
                force_purge_now = 0;
        }
        else {
@@ -954,7 +892,6 @@ void cmd_tdap(char *argbuf) {
 }
 
 
-
 CTDL_MODULE_INIT(expire)
 {
        if (!threading)