X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fmodules%2Fexpire%2Fserv_expire.c;h=94999b9d71eba4bbdb61838034109560a57693ad;hb=58f686487cf5f14d5da5357c67f2e6624dbde027;hp=d856761c8657991d6d9081f26f0886bd55bb8963;hpb=eb5fe4c7a556fe54065a40c7b34d12a9ca839b04;p=citadel.git diff --git a/citadel/modules/expire/serv_expire.c b/citadel/modules/expire/serv_expire.c index d856761c8..94999b9d7 100644 --- a/citadel/modules/expire/serv_expire.c +++ b/citadel/modules/expire/serv_expire.c @@ -1,12 +1,20 @@ /* - * $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-2015 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 + * 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. + * * A brief technical discussion: * * Several of the purge operations found in this module operate in two @@ -22,6 +30,11 @@ * * 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. */ @@ -55,14 +68,13 @@ #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 "threads.h" +#include "context.h" #include "ctdl_module.h" @@ -114,10 +126,10 @@ struct ValidRoom *ValidRoomList = NULL; struct ValidUser *ValidUserList = NULL; 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 */ /* @@ -130,7 +142,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) { time_t xtime, now; struct CtdlMessage *msg = NULL; int a; - struct cdbdata *cdbfr; + struct cdbdata *cdbfr; long *msglist = NULL; int num_msgs = 0; FILE *purgelist; @@ -145,13 +157,17 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) { if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return; if (epbuf.expire_mode == EXPIRE_MANUAL) return; - cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long)); + /* Don't purge messages containing system configuration, dumbass. */ + if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return; - if (cdbfr != NULL) { - msglist = malloc(cdbfr->len); - memcpy(msglist, cdbfr->ptr, cdbfr->len); - num_msgs = cdbfr->len / sizeof(long); - cdb_free(cdbfr); + /* 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) { + msglist = malloc(cdbfr->len); + memcpy(msglist, cdbfr->ptr, cdbfr->len); + num_msgs = cdbfr->len / sizeof(long); + cdb_free(cdbfr); } /* Nothing to do if there aren't any messages */ @@ -160,6 +176,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) { @@ -175,10 +192,10 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) { for (a=0; acm_fields['T']); - CtdlFreeMessage(msg); + xtime = atol(msg->cm_fields[eTimestamp]); + CM_Free(msg); } else { xtime = 0L; } @@ -224,17 +241,16 @@ void DoPurgeMessages(FILE *purgelist) { void PurgeMessages(void) { FILE *purgelist; - lprintf(CTDL_DEBUG, "PurgeMessages() called\n"); + syslog(LOG_DEBUG, "PurgeMessages() called"); messages_purged = 0; purgelist = tmpfile(); if (purgelist == NULL) { - lprintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n", - strerror(errno)); + syslog(LOG_CRIT, "Can't create purgelist temp file: %s", strerror(errno)); return; } - ForEachRoom(GatherPurgeMessages, (void *)purgelist ); + CtdlForEachRoom(GatherPurgeMessages, (void *)purgelist ); DoPurgeMessages(purgelist); fclose(purgelist); } @@ -283,20 +299,21 @@ 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; /* 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; - lprintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age); + syslog(LOG_DEBUG, "<%s> is <%ld> seconds old", qrbuf->QRname, (long)age); if (age > purge_secs) do_purge = 1; } /* !QR_MAILBOX */ @@ -318,7 +335,7 @@ int PurgeRooms(void) { struct ValidUser *vuptr; char *transcript = NULL; - lprintf(CTDL_DEBUG, "PurgeRooms() called\n"); + syslog(LOG_DEBUG, "PurgeRooms() called"); /* Load up a table full of valid user numbers so we can delete @@ -326,7 +343,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) { @@ -340,11 +357,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); @@ -352,10 +369,10 @@ 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); - lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged); + syslog(LOG_DEBUG, "Purged %d rooms.", num_rooms_purged); return(num_rooms_purged); } @@ -383,6 +400,7 @@ void do_uid_user_purge(struct ctdluser *us, void *data) { + /* * Back end function to check user accounts for expiration. */ @@ -394,25 +412,23 @@ void do_user_purge(struct ctdluser *us, void *data) { /* Set purge time; if the user overrides the system default, use it */ if (us->USuserpurge > 0) { - purge_time = ((time_t)us->USuserpurge) * 86400L; + 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. */ purge = 0; - - /* If the user hasn't called in two months, his/her account + + /* 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. */ - now = time(NULL); - if ((now - us->lastcall) > purge_time) purge = 1; - - /* If the user set his/her password to 'deleteme', he/she - * wishes to be deleted, so purge the record. - */ - if (!strcasecmp(us->password, "deleteme")) purge = 1; + if (CtdlGetConfigLong("c_userpurge") > 0) + { + now = time(NULL); + if ((now - us->lastcall) > purge_time) purge = 1; + } /* If the record is marked as permanent, don't purge it. */ @@ -428,15 +444,62 @@ void do_user_purge(struct ctdluser *us, void *data) { */ 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; + /* 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; - /* User number 0, as well as any negative user number, is + /* any negative user number, is * also impossible. */ - if (us->usernum < 1L) purge = 1; + if (us->usernum < 0L) purge = 1; + + /* Don't purge user 0. That user is there for the system */ + 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); + // purge = 0; + } + + /* If the user has no full name entry then we can't purge them + * since the actual purge can't find them. + * This shouldn't happen but does somehow. + */ + if (IsEmptyStr(us->fullname)) + { + purge = 0; + + if (us->usernum > 0L) + { + purge=0; + if (users_corrupt_msg == NULL) + { + users_corrupt_msg = malloc(SIZ); + strcpy(users_corrupt_msg, + "The auto-purger found the following user numbers with no name.\n" + "The system has no way to purge a user with no name," + " and should not be able to 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)+30); + snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], 29, " %ld\n", us->usernum); + } + } if (purge == 1) { pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList)); @@ -457,18 +520,19 @@ int PurgeUsers(void) { int num_users_purged = 0; char *transcript = NULL; - lprintf(CTDL_DEBUG, "PurgeUsers() called\n"); + syslog(LOG_DEBUG, "PurgeUsers() called"); users_not_purged = 0; - if (config.c_auth_mode == 1) { - /* host auth mode */ - ForEachUser(do_uid_user_purge, NULL); - } - else { - /* native auth mode */ - if (config.c_userpurge > 0) { + 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.", CtdlGetConfigInt("c_auth_mode")); + break; } transcript = malloc(SIZ); @@ -500,10 +564,24 @@ 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); - lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged); + if(users_corrupt_msg) + { + CtdlAideMessage(users_corrupt_msg, "User Corruption Message"); + free (users_corrupt_msg); + users_corrupt_msg = NULL; + } + + if(users_zero_msg) + { + CtdlAideMessage(users_zero_msg, "User Zero Message"); + free (users_zero_msg); + users_zero_msg = NULL; + } + + syslog(LOG_DEBUG, "Purged %d users.", num_users_purged); return(num_users_purged); } @@ -522,7 +600,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; @@ -533,7 +611,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); @@ -541,10 +619,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; @@ -610,7 +688,7 @@ int PurgeVisits(void) { * Purge the use table of old entries. * */ -int PurgeUseTable(void) { +int PurgeUseTable(StrBuf *ErrMsg) { int purged = 0; struct cdbdata *cdbut; struct UseTable ut; @@ -618,23 +696,32 @@ int PurgeUseTable(void) { struct UPurgeList *uptr; /* Phase 1: traverse through the table, discovering old records... */ - lprintf(CTDL_DEBUG, "Purge use table: phase 1\n"); + 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) { + while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) + { + if (cdbut->len > sizeof(struct UseTable)) + memcpy(&ut, cdbut->ptr, sizeof(struct UseTable)); + else + { + memset(&ut, 0, sizeof(struct UseTable)); + memcpy(&ut, cdbut->ptr, cdbut->len); + } + cdb_free(cdbut); - /* - * 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 - */ - memcpy(&ut, cdbut->ptr, - ((cdbut->len > sizeof(struct UseTable)) ? - sizeof(struct UseTable) : cdbut->len)); - cdb_free(cdbut); + syslog(LOG_DEBUG, "UT: [%s] at %s", ut.ut_msgid, asctime(localtime(&ut.ut_timestamp))); // FIXME take this out ajc - 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; @@ -645,7 +732,7 @@ int PurgeUseTable(void) { } /* Phase 2: delete the records */ - lprintf(CTDL_DEBUG, "Purge use table: phase 2\n"); + syslog(LOG_DEBUG, "Purge use table: phase 2"); while (ul != NULL) { cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key)); uptr = ul->next; @@ -653,7 +740,7 @@ int PurgeUseTable(void) { ul = uptr; } - lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged); + syslog(LOG_DEBUG, "Purge use table: finished (purged %d records)", purged); return(purged); } @@ -672,15 +759,15 @@ int PurgeEuidIndexTable(void) { struct CtdlMessage *msg = NULL; /* Phase 1: traverse through the table, discovering old records... */ - lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n"); + syslog(LOG_DEBUG, "Purge EUID index: phase 1"); cdb_rewind(CDB_EUIDINDEX); while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) { 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)); @@ -694,12 +781,12 @@ int PurgeEuidIndexTable(void) { ++purged; } - cdb_free(cdbei); + cdb_free(cdbei); } /* Phase 2: delete the records */ - lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n"); + syslog(LOG_DEBUG, "Purge euid index: phase 2"); while (el != NULL) { cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen); free(el->ep_key); @@ -708,184 +795,168 @@ int PurgeEuidIndexTable(void) { el = eptr; } - lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged); + syslog(LOG_DEBUG, "Purge euid index: finished (purged %d records)", purged); return(purged); } -void *purge_databases(void *args) -{ - int retval; - static time_t last_purge = 0; - time_t now; - struct tm tm; - - CT_PUSH(); // Makes it easier to access this threads structure - - cdb_allocate_tsd(); - while (!CtdlThreadCheckStop(CT)) { - /* 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) || ((now - last_purge) < 43200)) { - CtdlThreadSleep(60); - continue; - } - - - lprintf(CTDL_INFO, "Auto-purger: starting.\n"); - - if (!CtdlThreadCheckStop(CT)) - { - retval = PurgeUsers(); - lprintf(CTDL_NOTICE, "Purged %d users.\n", retval); - } - - if (!CtdlThreadCheckStop(CT)) - { - PurgeMessages(); - lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged); - } - - if (!CtdlThreadCheckStop(CT)) - { - retval = PurgeRooms(); - lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval); - } - - if (!CtdlThreadCheckStop(CT)) - { - retval = PurgeVisits(); - lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval); - } - - if (!CtdlThreadCheckStop(CT)) - { - retval = PurgeUseTable(); - lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval); - } - - if (!CtdlThreadCheckStop(CT)) - { - retval = PurgeEuidIndexTable(); - lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval); - } - - if (!CtdlThreadCheckStop(CT)) - { - retval = TDAP_ProcessAdjRefCountQueue(); - lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval); +/* + * Purge OpenID assocations for missing users (theoretically this will never delete anything) + */ +int PurgeStaleOpenIDassociations(void) { + struct cdbdata *cdboi; + struct ctdluser usbuf; + HashList *keys = NULL; + HashPos *HashPos; + char *deleteme = NULL; + long len; + void *Value; + const char *Key; + int num_deleted = 0; + long usernum = 0L; + + keys = NewHash(1, NULL); + if (!keys) return(0); + + + cdb_rewind(CDB_OPENID); + while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) { + if (cdboi->len > sizeof(long)) { + memcpy(&usernum, cdboi->ptr, sizeof(long)); + if (CtdlGetUserByNumber(&usbuf, usernum) != 0) { + deleteme = strdup(cdboi->ptr + sizeof(long)), + Put(keys, deleteme, strlen(deleteme), deleteme, NULL); + } } + cdb_free(cdboi); + } - if (!CtdlThreadCheckStop(CT)) - { - lprintf(CTDL_INFO, "Auto-purger: finished.\n"); - last_purge = now; /* So we don't do it again soon */ - } - else - lprintf(CTDL_INFO, "Auto-purger: STOPPED.\n"); + /* Go through the hash list, deleting keys we stored in it */ - } - return NULL; + 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)); + /* note: don't free(Value) -- deleting the hash list will handle this for us */ + ++num_deleted; + } + DeleteHashPos(&HashPos); + DeleteHash(&keys); + return num_deleted; } -/*****************************************************************************/ - -void do_fsck_msg(long msgnum, void *userdata) { - struct ctdlroomref *ptr; - ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref)); - ptr->next = rr; - ptr->msgnum = msgnum; - rr = ptr; -} -void do_fsck_room(struct ctdlroom *qrbuf, void *data) -{ - getroom(&CC->room, qrbuf->QRname); - CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL); -} -/* - * Check message reference counts - */ -void cmd_fsck(char *argbuf) { - long msgnum; - struct cdbdata *cdbmsg; - struct MetaData smi; - struct ctdlroomref *ptr; - int realcount; - if (CtdlAccessCheck(ac_aide)) return; +void purge_databases(void) +{ + int retval; + static time_t last_purge = 0; + time_t now; + struct tm tm; - /* Lame way of checking whether anyone else is doing this now */ - if (rr != NULL) { - cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY); - return; + /* 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 != CtdlGetConfigInt("c_purge_hour")) || ((now - last_purge) < 43200)) && (force_purge_now == 0)) + { + 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); + syslog(LOG_INFO, "Auto-purger: starting."); - 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); + if (!server_shutting_down) + { + retval = PurgeUsers(); + syslog(LOG_NOTICE, "Purged %d users.", retval); + } + + if (!server_shutting_down) + { + PurgeMessages(); + syslog(LOG_NOTICE, "Expired %d messages.", messages_purged); + } - GetMetaData(&smi, msgnum); - cprintf("refcount=%-2d ", smi.meta_refcount); + if (!server_shutting_down) + { + retval = PurgeRooms(); + syslog(LOG_NOTICE, "Expired %d rooms.", retval); + } - realcount = 0; - for (ptr = rr; ptr != NULL; ptr = ptr->next) { - if (ptr->msgnum == msgnum) ++realcount; - } - cprintf("realcount=%-2d\n", realcount); + if (!server_shutting_down) + { + retval = PurgeVisits(); + syslog(LOG_NOTICE, "Purged %d visits.", retval); + } - if ( (smi.meta_refcount != realcount) - || (realcount == 0) ) { - AdjRefCount(msgnum, (smi.meta_refcount - realcount)); - } + if (!server_shutting_down) + { + StrBuf *ErrMsg; - } + ErrMsg = NewStrBuf (); + retval = PurgeUseTable(ErrMsg); + syslog(LOG_NOTICE, "Purged %d entries from the use table.", retval); +////TODO: fix errmsg + FreeStrBuf(&ErrMsg); + } + if (!server_shutting_down) + { + retval = PurgeEuidIndexTable(); + syslog(LOG_NOTICE, "Purged %d entries from the EUID index.", retval); } - cprintf("Freeing memory...\n"); - while (rr != NULL) { - ptr = rr->next; - free(rr); - rr = ptr; + if (!server_shutting_down) + { + retval = PurgeStaleOpenIDassociations(); + syslog(LOG_NOTICE, "Purged %d stale OpenID associations.", retval); } - cprintf("Done!\n"); - cprintf("000\n"); + if (!server_shutting_down) + { + retval = TDAP_ProcessAdjRefCountQueue(); + syslog(LOG_NOTICE, "Processed %d message reference count adjustments.", retval); + } + if (!server_shutting_down) + { + syslog(LOG_INFO, "Auto-purger: finished."); + last_purge = now; /* So we don't do it again soon */ + force_purge_now = 0; + } + else { + syslog(LOG_INFO, "Auto-purger: STOPPED."); + } } +/* + * 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); +} -/*****************************************************************************/ CTDL_MODULE_INIT(expire) { if (!threading) { - 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"); + CtdlRegisterSessionHook(purge_databases, EVT_TIMER, PRIO_CLEANUP + 20); } - else - CtdlThreadCreate("Auto Purger", CTDLTHREAD_BIGSTACK, purge_databases, NULL); - /* return our Subversion id for the Log */ - return "$Id$"; + + /* return our module name for the log */ + return "expire"; }