4 * This module handles the expiry of old messages and the purging of old users.
10 * A brief technical discussion:
12 * Several of the purge operations found in this module operate in two
13 * stages: the first stage generates a linked list of objects to be deleted,
14 * then the second stage deletes all listed objects from the database.
16 * At first glance this may seem cumbersome and unnecessary. The reason it is
17 * implemented in this way is because Berkeley DB, and possibly other backends
18 * we may hook into in the future, explicitly do _not_ support the deletion of
19 * records from a file while the file is being traversed. The delete operation
20 * will succeed, but the traversal is not guaranteed to visit every object if
21 * this is done. Therefore we utilize the two-stage purge.
23 * When using Berkeley DB, there's another reason for the two-phase purge: we
24 * don't want the entire thing being done as one huge transaction.
36 #include <sys/types.h>
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
54 #include "citserver.h"
63 #include "serv_network.h"
67 #include "ctdl_module.h"
71 struct PurgeList *next;
72 char name[ROOMNAMELEN]; /* use the larger of username or roomname */
76 struct VPurgeList *next;
83 struct ValidRoom *next;
89 struct ValidUser *next;
95 struct ctdlroomref *next;
100 struct UPurgeList *next;
105 struct EPurgeList *next;
111 struct PurgeList *UserPurgeList = NULL;
112 struct PurgeList *RoomPurgeList = NULL;
113 struct ValidRoom *ValidRoomList = NULL;
114 struct ValidUser *ValidUserList = NULL;
116 int users_not_purged;
118 struct ctdlroomref *rr = NULL;
120 extern struct CitContext *ContextList;
124 * First phase of message purge -- gather the locations of messages which
125 * qualify for purging and write them to a temp file.
127 void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
128 struct ExpirePolicy epbuf;
131 struct CtdlMessage *msg = NULL;
133 struct cdbdata *cdbfr;
134 long *msglist = NULL;
138 purgelist = (FILE *)data;
139 fprintf(purgelist, "r=%s\n", qrbuf->QRname);
142 GetExpirePolicy(&epbuf, qrbuf);
144 /* If the room is set to never expire messages ... do nothing */
145 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
146 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
148 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
151 msglist = malloc(cdbfr->len);
152 memcpy(msglist, cdbfr->ptr, cdbfr->len);
153 num_msgs = cdbfr->len / sizeof(long);
157 /* Nothing to do if there aren't any messages */
159 if (msglist != NULL) free(msglist);
163 /* If the room is set to expire by count, do that */
164 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
165 if (num_msgs > epbuf.expire_value) {
166 for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
167 fprintf(purgelist, "m=%ld\n", msglist[a]);
173 /* If the room is set to expire by age... */
174 if (epbuf.expire_mode == EXPIRE_AGE) {
175 for (a=0; a<num_msgs; ++a) {
178 msg = CtdlFetchMessage(delnum, 0); /* dont need body */
180 xtime = atol(msg->cm_fields['T']);
181 CtdlFreeMessage(msg);
187 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
188 fprintf(purgelist, "m=%ld\n", delnum);
194 if (msglist != NULL) free(msglist);
199 * Second phase of message purge -- read list of msgs from temp file and
202 void DoPurgeMessages(FILE *purgelist) {
203 char roomname[ROOMNAMELEN];
208 strcpy(roomname, "nonexistent room ___ ___");
209 while (fgets(buf, sizeof buf, purgelist) != NULL) {
210 buf[strlen(buf)-1]=0;
211 if (!strncasecmp(buf, "r=", 2)) {
212 strcpy(roomname, &buf[2]);
214 if (!strncasecmp(buf, "m=", 2)) {
215 msgnum = atol(&buf[2]);
217 CtdlDeleteMessages(roomname, &msgnum, 1, "");
224 void PurgeMessages(void) {
227 lprintf(CTDL_DEBUG, "PurgeMessages() called\n");
230 purgelist = tmpfile();
231 if (purgelist == NULL) {
232 lprintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
237 ForEachRoom(GatherPurgeMessages, (void *)purgelist );
238 DoPurgeMessages(purgelist);
243 void AddValidUser(struct ctdluser *usbuf, void *data) {
244 struct ValidUser *vuptr;
246 vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
247 vuptr->next = ValidUserList;
248 vuptr->vu_usernum = usbuf->usernum;
249 ValidUserList = vuptr;
252 void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
253 struct ValidRoom *vrptr;
255 vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
256 vrptr->next = ValidRoomList;
257 vrptr->vr_roomnum = qrbuf->QRnumber;
258 vrptr->vr_roomgen = qrbuf->QRgen;
259 ValidRoomList = vrptr;
262 void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
263 time_t age, purge_secs;
264 struct PurgeList *pptr;
265 struct ValidUser *vuptr;
268 /* For mailbox rooms, there's only one purging rule: if the user who
269 * owns the room still exists, we keep the room; otherwise, we purge
270 * it. Bypass any other rules.
272 if (qrbuf->QRflags & QR_MAILBOX) {
273 /* if user not found, do_purge will be 1 */
275 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
276 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
282 /* Any of these attributes render a room non-purgable */
283 if (qrbuf->QRflags & QR_PERMANENT) return;
284 if (qrbuf->QRflags & QR_DIRECTORY) return;
285 if (qrbuf->QRflags & QR_NETWORK) return;
286 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
287 if (is_noneditable(qrbuf)) return;
289 /* If we don't know the modification date, be safe and don't purge */
290 if (qrbuf->QRmtime <= (time_t)0) return;
292 /* If no room purge time is set, be safe and don't purge */
293 if (config.c_roompurge < 0) return;
295 /* Otherwise, check the date of last modification */
296 age = time(NULL) - (qrbuf->QRmtime);
297 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
298 if (purge_secs <= (time_t)0) return;
299 lprintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
300 if (age > purge_secs) do_purge = 1;
304 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
305 pptr->next = RoomPurgeList;
306 strcpy(pptr->name, qrbuf->QRname);
307 RoomPurgeList = pptr;
314 int PurgeRooms(void) {
315 struct PurgeList *pptr;
316 int num_rooms_purged = 0;
317 struct ctdlroom qrbuf;
318 struct ValidUser *vuptr;
319 char *transcript = NULL;
321 lprintf(CTDL_DEBUG, "PurgeRooms() called\n");
324 /* Load up a table full of valid user numbers so we can delete
325 * user-owned rooms for users who no longer exist */
326 ForEachUser(AddValidUser, NULL);
328 /* Then cycle through the room file */
329 ForEachRoom(DoPurgeRooms, NULL);
331 /* Free the valid user list */
332 while (ValidUserList != NULL) {
333 vuptr = ValidUserList->next;
335 ValidUserList = vuptr;
339 transcript = malloc(SIZ);
340 strcpy(transcript, "The following rooms have been auto-purged:\n");
342 while (RoomPurgeList != NULL) {
343 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
344 transcript=realloc(transcript, strlen(transcript)+SIZ);
345 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
349 pptr = RoomPurgeList->next;
351 RoomPurgeList = pptr;
355 if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
358 lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
359 return(num_rooms_purged);
364 * Back end function to check user accounts for associated Unix accounts
365 * which no longer exist. (Only relevant for host auth mode.)
367 void do_uid_user_purge(struct ctdluser *us, void *data) {
368 struct PurgeList *pptr;
370 if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
371 if (getpwuid(us->uid) == NULL) {
372 pptr = (struct PurgeList *)
373 malloc(sizeof(struct PurgeList));
374 pptr->next = UserPurgeList;
375 strcpy(pptr->name, us->fullname);
376 UserPurgeList = pptr;
387 * Back end function to check user accounts for expiration.
389 void do_user_purge(struct ctdluser *us, void *data) {
393 struct PurgeList *pptr;
395 /* Set purge time; if the user overrides the system default, use it */
396 if (us->USuserpurge > 0) {
397 purge_time = ((time_t)us->USuserpurge) * 86400L;
400 purge_time = ((time_t)config.c_userpurge) * 86400L;
403 /* The default rule is to not purge. */
406 /* If the user hasn't called in two months, his/her account
407 * has expired, so purge the record.
410 if ((now - us->lastcall) > purge_time) purge = 1;
412 /* If the user set his/her password to 'deleteme', he/she
413 * wishes to be deleted, so purge the record.
415 if (!strcasecmp(us->password, "deleteme")) purge = 1;
417 /* If the record is marked as permanent, don't purge it.
419 if (us->flags & US_PERM) purge = 0;
421 /* If the user is an Aide, don't purge him/her/it.
423 if (us->axlevel == 6) purge = 0;
425 /* If the access level is 0, the record should already have been
426 * deleted, but maybe the user was logged in at the time or something.
427 * Delete the record now.
429 if (us->axlevel == 0) purge = 1;
431 /* 0 calls is impossible. If there are 0 calls, it must
432 * be a corrupted record, so purge it.
434 if (us->timescalled == 0) purge = 1;
436 /* User number 0, as well as any negative user number, is
439 if (us->usernum < 1L) purge = 1;
442 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
443 pptr->next = UserPurgeList;
444 strcpy(pptr->name, us->fullname);
445 UserPurgeList = pptr;
455 int PurgeUsers(void) {
456 struct PurgeList *pptr;
457 int num_users_purged = 0;
458 char *transcript = NULL;
460 lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
461 users_not_purged = 0;
463 if (config.c_auth_mode == 1) {
465 ForEachUser(do_uid_user_purge, NULL);
468 /* native auth mode */
469 if (config.c_userpurge > 0) {
470 ForEachUser(do_user_purge, NULL);
474 transcript = malloc(SIZ);
476 if (users_not_purged == 0) {
477 strcpy(transcript, "The auto-purger was told to purge every user. It is\n"
478 "refusing to do this because it usually indicates a problem\n"
479 "such as an inability to communicate with a name service.\n"
481 while (UserPurgeList != NULL) {
482 pptr = UserPurgeList->next;
484 UserPurgeList = pptr;
490 strcpy(transcript, "The following users have been auto-purged:\n");
491 while (UserPurgeList != NULL) {
492 transcript=realloc(transcript, strlen(transcript)+SIZ);
493 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
494 UserPurgeList->name);
495 purge_user(UserPurgeList->name);
496 pptr = UserPurgeList->next;
498 UserPurgeList = pptr;
503 if (num_users_purged > 0) aide_message(transcript, "User Purge Message");
506 lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
507 return(num_users_purged);
514 * This is a really cumbersome "garbage collection" function. We have to
515 * delete visits which refer to rooms and/or users which no longer exist. In
516 * order to prevent endless traversals of the room and user files, we first
517 * build linked lists of rooms and users which _do_ exist on the system, then
518 * traverse the visit file, checking each record against those two lists and
519 * purging the ones that do not have a match on _both_ lists. (Remember, if
520 * either the room or user being referred to is no longer on the system, the
521 * record is completely useless.)
523 int PurgeVisits(void) {
524 struct cdbdata *cdbvisit;
526 struct VPurgeList *VisitPurgeList = NULL;
527 struct VPurgeList *vptr;
531 struct ValidRoom *vrptr;
532 struct ValidUser *vuptr;
533 int RoomIsValid, UserIsValid;
535 /* First, load up a table full of valid room/gen combinations */
536 ForEachRoom(AddValidRoom, NULL);
538 /* Then load up a table full of valid user numbers */
539 ForEachUser(AddValidUser, NULL);
541 /* Now traverse through the visits, purging irrelevant records... */
542 cdb_rewind(CDB_VISIT);
543 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
544 memset(&vbuf, 0, sizeof(struct visit));
545 memcpy(&vbuf, cdbvisit->ptr,
546 ( (cdbvisit->len > sizeof(struct visit)) ?
547 sizeof(struct visit) : cdbvisit->len) );
553 /* Check to see if the room exists */
554 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
555 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
556 && (vrptr->vr_roomgen==vbuf.v_roomgen))
560 /* Check to see if the user exists */
561 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
562 if (vuptr->vu_usernum == vbuf.v_usernum)
566 /* Put the record on the purge list if it's dead */
567 if ((RoomIsValid==0) || (UserIsValid==0)) {
568 vptr = (struct VPurgeList *)
569 malloc(sizeof(struct VPurgeList));
570 vptr->next = VisitPurgeList;
571 vptr->vp_roomnum = vbuf.v_roomnum;
572 vptr->vp_roomgen = vbuf.v_roomgen;
573 vptr->vp_usernum = vbuf.v_usernum;
574 VisitPurgeList = vptr;
579 /* Free the valid room/gen combination list */
580 while (ValidRoomList != NULL) {
581 vrptr = ValidRoomList->next;
583 ValidRoomList = vrptr;
586 /* Free the valid user list */
587 while (ValidUserList != NULL) {
588 vuptr = ValidUserList->next;
590 ValidUserList = vuptr;
593 /* Now delete every visit on the purged list */
594 while (VisitPurgeList != NULL) {
595 IndexLen = GenerateRelationshipIndex(IndexBuf,
596 VisitPurgeList->vp_roomnum,
597 VisitPurgeList->vp_roomgen,
598 VisitPurgeList->vp_usernum);
599 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
600 vptr = VisitPurgeList->next;
601 free(VisitPurgeList);
602 VisitPurgeList = vptr;
610 * Purge the use table of old entries.
613 int PurgeUseTable(void) {
615 struct cdbdata *cdbut;
617 struct UPurgeList *ul = NULL;
618 struct UPurgeList *uptr;
620 /* Phase 1: traverse through the table, discovering old records... */
621 lprintf(CTDL_DEBUG, "Purge use table: phase 1\n");
622 cdb_rewind(CDB_USETABLE);
623 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
625 memcpy(&ut, cdbut->ptr,
626 ((cdbut->len > sizeof(struct UseTable)) ?
627 sizeof(struct UseTable) : cdbut->len));
630 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
631 uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
634 safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
642 /* Phase 2: delete the records */
643 lprintf(CTDL_DEBUG, "Purge use table: phase 2\n");
645 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
651 lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
658 * Purge the EUID Index of old records.
661 int PurgeEuidIndexTable(void) {
663 struct cdbdata *cdbei;
664 struct EPurgeList *el = NULL;
665 struct EPurgeList *eptr;
667 struct CtdlMessage *msg = NULL;
669 /* Phase 1: traverse through the table, discovering old records... */
670 lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
671 cdb_rewind(CDB_EUIDINDEX);
672 while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
674 memcpy(&msgnum, cdbei->ptr, sizeof(long));
676 msg = CtdlFetchMessage(msgnum, 0);
678 CtdlFreeMessage(msg); /* it still exists, so do nothing */
681 eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
684 eptr->ep_keylen = cdbei->len - sizeof(long);
685 eptr->ep_key = malloc(cdbei->len);
686 memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
696 /* Phase 2: delete the records */
697 lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
699 cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
706 lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
711 void purge_databases(void) {
713 static time_t last_purge = 0;
717 /* Do the auto-purge if the current hour equals the purge hour,
718 * but not if the operation has already been performed in the
719 * last twelve hours. This is usually enough granularity.
722 localtime_r(&now, &tm);
723 if (tm.tm_hour != config.c_purge_hour) return;
724 if ((now - last_purge) < 43200) return;
726 lprintf(CTDL_INFO, "Auto-purger: starting.\n");
728 retval = PurgeUsers();
729 lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
732 lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
734 retval = PurgeRooms();
735 lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
737 retval = PurgeVisits();
738 lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
740 retval = PurgeUseTable();
741 lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
743 retval = PurgeEuidIndexTable();
744 lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
746 retval = TDAP_ProcessAdjRefCountQueue();
747 lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
749 lprintf(CTDL_INFO, "Auto-purger: finished.\n");
751 last_purge = now; /* So we don't do it again soon */
754 /*****************************************************************************/
757 void do_fsck_msg(long msgnum, void *userdata) {
758 struct ctdlroomref *ptr;
760 ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
762 ptr->msgnum = msgnum;
766 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
768 getroom(&CC->room, qrbuf->QRname);
769 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
773 * Check message reference counts
775 void cmd_fsck(char *argbuf) {
777 struct cdbdata *cdbmsg;
779 struct ctdlroomref *ptr;
782 if (CtdlAccessCheck(ac_aide)) return;
784 /* Lame way of checking whether anyone else is doing this now */
786 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
790 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
792 cprintf("\nThis could take a while. Please be patient!\n\n");
793 cprintf("Gathering pointers...\n");
794 ForEachRoom(do_fsck_room, NULL);
797 cprintf("Checking message base...\n");
798 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
800 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
801 if (cdbmsg != NULL) {
803 cprintf("Message %7ld ", msgnum);
805 GetMetaData(&smi, msgnum);
806 cprintf("refcount=%-2d ", smi.meta_refcount);
809 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
810 if (ptr->msgnum == msgnum) ++realcount;
812 cprintf("realcount=%-2d\n", realcount);
814 if ( (smi.meta_refcount != realcount)
815 || (realcount == 0) ) {
816 AdjRefCount(msgnum, (smi.meta_refcount - realcount));
823 cprintf("Freeing memory...\n");
838 /*****************************************************************************/
840 CTDL_MODULE_INIT(expire)
842 CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
843 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
845 /* return our Subversion id for the Log */