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 "sysdep_decls.h"
55 #include "citserver.h"
58 #include "serv_extensions.h"
65 #include "serv_network.h"
70 struct PurgeList *next;
71 char name[ROOMNAMELEN]; /* use the larger of username or roomname */
75 struct VPurgeList *next;
82 struct ValidRoom *next;
88 struct ValidUser *next;
94 struct ctdlroomref *next;
99 struct UPurgeList *next;
104 struct EPurgeList *next;
110 struct PurgeList *UserPurgeList = NULL;
111 struct PurgeList *RoomPurgeList = NULL;
112 struct ValidRoom *ValidRoomList = NULL;
113 struct ValidUser *ValidUserList = NULL;
115 int users_not_purged;
117 struct ctdlroomref *rr = NULL;
119 extern struct CitContext *ContextList;
123 * First phase of message purge -- gather the locations of messages which
124 * qualify for purging and write them to a temp file.
126 void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
127 struct ExpirePolicy epbuf;
130 struct CtdlMessage *msg = NULL;
132 struct cdbdata *cdbfr;
133 long *msglist = NULL;
137 purgelist = (FILE *)data;
138 fprintf(purgelist, "r=%s\n", qrbuf->QRname);
141 GetExpirePolicy(&epbuf, qrbuf);
143 /* If the room is set to never expire messages ... do nothing */
144 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
145 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
147 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
150 msglist = malloc(cdbfr->len);
151 memcpy(msglist, cdbfr->ptr, cdbfr->len);
152 num_msgs = cdbfr->len / sizeof(long);
156 /* Nothing to do if there aren't any messages */
158 if (msglist != NULL) free(msglist);
162 /* If the room is set to expire by count, do that */
163 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
164 if (num_msgs > epbuf.expire_value) {
165 for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
166 fprintf(purgelist, "m=%ld\n", msglist[a]);
172 /* If the room is set to expire by age... */
173 if (epbuf.expire_mode == EXPIRE_AGE) {
174 for (a=0; a<num_msgs; ++a) {
177 msg = CtdlFetchMessage(delnum, 0); /* dont need body */
179 xtime = atol(msg->cm_fields['T']);
180 CtdlFreeMessage(msg);
186 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
187 fprintf(purgelist, "m=%ld\n", delnum);
193 if (msglist != NULL) free(msglist);
198 * Second phase of message purge -- read list of msgs from temp file and
201 void DoPurgeMessages(FILE *purgelist) {
202 char roomname[ROOMNAMELEN];
207 strcpy(roomname, "nonexistent room ___ ___");
208 while (fgets(buf, sizeof buf, purgelist) != NULL) {
209 buf[strlen(buf)-1]=0;
210 if (!strncasecmp(buf, "r=", 2)) {
211 strcpy(roomname, &buf[2]);
213 if (!strncasecmp(buf, "m=", 2)) {
214 msgnum = atol(&buf[2]);
216 CtdlDeleteMessages(roomname, &msgnum, 1, "");
223 void PurgeMessages(void) {
226 lprintf(CTDL_DEBUG, "PurgeMessages() called\n");
229 purgelist = tmpfile();
230 if (purgelist == NULL) {
231 lprintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
236 ForEachRoom(GatherPurgeMessages, (void *)purgelist );
237 DoPurgeMessages(purgelist);
242 void AddValidUser(struct ctdluser *usbuf, void *data) {
243 struct ValidUser *vuptr;
245 vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
246 vuptr->next = ValidUserList;
247 vuptr->vu_usernum = usbuf->usernum;
248 ValidUserList = vuptr;
251 void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
252 struct ValidRoom *vrptr;
254 vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
255 vrptr->next = ValidRoomList;
256 vrptr->vr_roomnum = qrbuf->QRnumber;
257 vrptr->vr_roomgen = qrbuf->QRgen;
258 ValidRoomList = vrptr;
261 void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
262 time_t age, purge_secs;
263 struct PurgeList *pptr;
264 struct ValidUser *vuptr;
267 /* For mailbox rooms, there's only one purging rule: if the user who
268 * owns the room still exists, we keep the room; otherwise, we purge
269 * it. Bypass any other rules.
271 if (qrbuf->QRflags & QR_MAILBOX) {
272 /* if user not found, do_purge will be 1 */
274 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
275 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
281 /* Any of these attributes render a room non-purgable */
282 if (qrbuf->QRflags & QR_PERMANENT) return;
283 if (qrbuf->QRflags & QR_DIRECTORY) return;
284 if (qrbuf->QRflags & QR_NETWORK) return;
285 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
286 if (is_noneditable(qrbuf)) return;
288 /* If we don't know the modification date, be safe and don't purge */
289 if (qrbuf->QRmtime <= (time_t)0) return;
291 /* If no room purge time is set, be safe and don't purge */
292 if (config.c_roompurge < 0) return;
294 /* Otherwise, check the date of last modification */
295 age = time(NULL) - (qrbuf->QRmtime);
296 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
297 if (purge_secs <= (time_t)0) return;
298 lprintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
299 if (age > purge_secs) do_purge = 1;
303 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
304 pptr->next = RoomPurgeList;
305 strcpy(pptr->name, qrbuf->QRname);
306 RoomPurgeList = pptr;
313 int PurgeRooms(void) {
314 struct PurgeList *pptr;
315 int num_rooms_purged = 0;
316 struct ctdlroom qrbuf;
317 struct ValidUser *vuptr;
318 char *transcript = NULL;
320 lprintf(CTDL_DEBUG, "PurgeRooms() called\n");
323 /* Load up a table full of valid user numbers so we can delete
324 * user-owned rooms for users who no longer exist */
325 ForEachUser(AddValidUser, NULL);
327 /* Then cycle through the room file */
328 ForEachRoom(DoPurgeRooms, NULL);
330 /* Free the valid user list */
331 while (ValidUserList != NULL) {
332 vuptr = ValidUserList->next;
334 ValidUserList = vuptr;
338 transcript = malloc(SIZ);
339 strcpy(transcript, "The following rooms have been auto-purged:\n");
341 while (RoomPurgeList != NULL) {
342 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
343 transcript=realloc(transcript, strlen(transcript)+SIZ);
344 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
348 pptr = RoomPurgeList->next;
350 RoomPurgeList = pptr;
354 if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
357 lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
358 return(num_rooms_purged);
363 * Back end function to check user accounts for associated Unix accounts
364 * which no longer exist. (Only relevant for host auth mode.)
366 void do_uid_user_purge(struct ctdluser *us, void *data) {
367 struct PurgeList *pptr;
369 if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
370 if (getpwuid(us->uid) == NULL) {
371 pptr = (struct PurgeList *)
372 malloc(sizeof(struct PurgeList));
373 pptr->next = UserPurgeList;
374 strcpy(pptr->name, us->fullname);
375 UserPurgeList = pptr;
386 * Back end function to check user accounts for expiration.
388 void do_user_purge(struct ctdluser *us, void *data) {
392 struct PurgeList *pptr;
394 /* Set purge time; if the user overrides the system default, use it */
395 if (us->USuserpurge > 0) {
396 purge_time = ((time_t)us->USuserpurge) * 86400L;
399 purge_time = ((time_t)config.c_userpurge) * 86400L;
402 /* The default rule is to not purge. */
405 /* If the user hasn't called in two months, his/her account
406 * has expired, so purge the record.
409 if ((now - us->lastcall) > purge_time) purge = 1;
411 /* If the user set his/her password to 'deleteme', he/she
412 * wishes to be deleted, so purge the record.
414 if (!strcasecmp(us->password, "deleteme")) purge = 1;
416 /* If the record is marked as permanent, don't purge it.
418 if (us->flags & US_PERM) purge = 0;
420 /* If the user is an Aide, don't purge him/her/it.
422 if (us->axlevel == 6) purge = 0;
424 /* If the access level is 0, the record should already have been
425 * deleted, but maybe the user was logged in at the time or something.
426 * Delete the record now.
428 if (us->axlevel == 0) purge = 1;
430 /* 0 calls is impossible. If there are 0 calls, it must
431 * be a corrupted record, so purge it.
433 if (us->timescalled == 0) purge = 1;
435 /* User number 0, as well as any negative user number, is
438 if (us->usernum < 1L) purge = 1;
441 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
442 pptr->next = UserPurgeList;
443 strcpy(pptr->name, us->fullname);
444 UserPurgeList = pptr;
454 int PurgeUsers(void) {
455 struct PurgeList *pptr;
456 int num_users_purged = 0;
457 char *transcript = NULL;
459 lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
460 users_not_purged = 0;
462 if (config.c_auth_mode == 1) {
464 ForEachUser(do_uid_user_purge, NULL);
467 /* native auth mode */
468 if (config.c_userpurge > 0) {
469 ForEachUser(do_user_purge, NULL);
473 transcript = malloc(SIZ);
475 if (users_not_purged == 0) {
476 strcpy(transcript, "The auto-purger was told to purge every user. It is\n"
477 "refusing to do this because it usually indicates a problem\n"
478 "such as an inability to communicate with a name service.\n"
480 while (UserPurgeList != NULL) {
481 pptr = UserPurgeList->next;
483 UserPurgeList = pptr;
489 strcpy(transcript, "The following users have been auto-purged:\n");
490 while (UserPurgeList != NULL) {
491 transcript=realloc(transcript, strlen(transcript)+SIZ);
492 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
493 UserPurgeList->name);
494 purge_user(UserPurgeList->name);
495 pptr = UserPurgeList->next;
497 UserPurgeList = pptr;
502 if (num_users_purged > 0) aide_message(transcript, "User Purge Message");
505 lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
506 return(num_users_purged);
513 * This is a really cumbersome "garbage collection" function. We have to
514 * delete visits which refer to rooms and/or users which no longer exist. In
515 * order to prevent endless traversals of the room and user files, we first
516 * build linked lists of rooms and users which _do_ exist on the system, then
517 * traverse the visit file, checking each record against those two lists and
518 * purging the ones that do not have a match on _both_ lists. (Remember, if
519 * either the room or user being referred to is no longer on the system, the
520 * record is completely useless.)
522 int PurgeVisits(void) {
523 struct cdbdata *cdbvisit;
525 struct VPurgeList *VisitPurgeList = NULL;
526 struct VPurgeList *vptr;
530 struct ValidRoom *vrptr;
531 struct ValidUser *vuptr;
532 int RoomIsValid, UserIsValid;
534 /* First, load up a table full of valid room/gen combinations */
535 ForEachRoom(AddValidRoom, NULL);
537 /* Then load up a table full of valid user numbers */
538 ForEachUser(AddValidUser, NULL);
540 /* Now traverse through the visits, purging irrelevant records... */
541 cdb_rewind(CDB_VISIT);
542 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
543 memset(&vbuf, 0, sizeof(struct visit));
544 memcpy(&vbuf, cdbvisit->ptr,
545 ( (cdbvisit->len > sizeof(struct visit)) ?
546 sizeof(struct visit) : cdbvisit->len) );
552 /* Check to see if the room exists */
553 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
554 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
555 && (vrptr->vr_roomgen==vbuf.v_roomgen))
559 /* Check to see if the user exists */
560 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
561 if (vuptr->vu_usernum == vbuf.v_usernum)
565 /* Put the record on the purge list if it's dead */
566 if ((RoomIsValid==0) || (UserIsValid==0)) {
567 vptr = (struct VPurgeList *)
568 malloc(sizeof(struct VPurgeList));
569 vptr->next = VisitPurgeList;
570 vptr->vp_roomnum = vbuf.v_roomnum;
571 vptr->vp_roomgen = vbuf.v_roomgen;
572 vptr->vp_usernum = vbuf.v_usernum;
573 VisitPurgeList = vptr;
578 /* Free the valid room/gen combination list */
579 while (ValidRoomList != NULL) {
580 vrptr = ValidRoomList->next;
582 ValidRoomList = vrptr;
585 /* Free the valid user list */
586 while (ValidUserList != NULL) {
587 vuptr = ValidUserList->next;
589 ValidUserList = vuptr;
592 /* Now delete every visit on the purged list */
593 while (VisitPurgeList != NULL) {
594 IndexLen = GenerateRelationshipIndex(IndexBuf,
595 VisitPurgeList->vp_roomnum,
596 VisitPurgeList->vp_roomgen,
597 VisitPurgeList->vp_usernum);
598 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
599 vptr = VisitPurgeList->next;
600 free(VisitPurgeList);
601 VisitPurgeList = vptr;
609 * Purge the use table of old entries.
612 int PurgeUseTable(void) {
614 struct cdbdata *cdbut;
616 struct UPurgeList *ul = NULL;
617 struct UPurgeList *uptr;
619 /* Phase 1: traverse through the table, discovering old records... */
620 lprintf(CTDL_DEBUG, "Purge use table: phase 1\n");
621 cdb_rewind(CDB_USETABLE);
622 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
624 memcpy(&ut, cdbut->ptr,
625 ((cdbut->len > sizeof(struct UseTable)) ?
626 sizeof(struct UseTable) : cdbut->len));
629 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
630 uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
633 safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
641 /* Phase 2: delete the records */
642 lprintf(CTDL_DEBUG, "Purge use table: phase 2\n");
644 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
650 lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
657 * Purge the EUID Index of old records.
660 int PurgeEuidIndexTable(void) {
662 struct cdbdata *cdbei;
663 struct EPurgeList *el = NULL;
664 struct EPurgeList *eptr;
666 struct CtdlMessage *msg = NULL;
668 /* Phase 1: traverse through the table, discovering old records... */
669 lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
670 cdb_rewind(CDB_EUIDINDEX);
671 while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
673 memcpy(&msgnum, cdbei->ptr, sizeof(long));
675 msg = CtdlFetchMessage(msgnum, 0);
677 CtdlFreeMessage(msg); /* it still exists, so do nothing */
680 eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
683 eptr->ep_keylen = cdbei->len - sizeof(long);
684 eptr->ep_key = malloc(cdbei->len);
685 memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
695 /* Phase 2: delete the records */
696 lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
698 cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
705 lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
710 void purge_databases(void) {
712 static time_t last_purge = 0;
716 /* Do the auto-purge if the current hour equals the purge hour,
717 * but not if the operation has already been performed in the
718 * last twelve hours. This is usually enough granularity.
721 localtime_r(&now, &tm);
722 if (tm.tm_hour != config.c_purge_hour) return;
723 if ((now - last_purge) < 43200) return;
725 lprintf(CTDL_INFO, "Auto-purger: starting.\n");
727 retval = PurgeUsers();
728 lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
731 lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
733 retval = PurgeRooms();
734 lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
736 retval = PurgeVisits();
737 lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
739 retval = PurgeUseTable();
740 lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
742 retval = PurgeEuidIndexTable();
743 lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
745 retval = TDAP_ProcessAdjRefCountQueue();
746 lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
748 lprintf(CTDL_INFO, "Auto-purger: finished.\n");
750 last_purge = now; /* So we don't do it again soon */
753 /*****************************************************************************/
756 void do_fsck_msg(long msgnum, void *userdata) {
757 struct ctdlroomref *ptr;
759 ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
761 ptr->msgnum = msgnum;
765 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
767 getroom(&CC->room, qrbuf->QRname);
768 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
772 * Check message reference counts
774 void cmd_fsck(char *argbuf) {
776 struct cdbdata *cdbmsg;
778 struct ctdlroomref *ptr;
781 if (CtdlAccessCheck(ac_aide)) return;
783 /* Lame way of checking whether anyone else is doing this now */
785 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
789 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
791 cprintf("\nThis could take a while. Please be patient!\n\n");
792 cprintf("Gathering pointers...\n");
793 ForEachRoom(do_fsck_room, NULL);
796 cprintf("Checking message base...\n");
797 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
799 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
800 if (cdbmsg != NULL) {
802 cprintf("Message %7ld ", msgnum);
804 GetMetaData(&smi, msgnum);
805 cprintf("refcount=%-2d ", smi.meta_refcount);
808 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
809 if (ptr->msgnum == msgnum) ++realcount;
811 cprintf("realcount=%-2d\n", realcount);
813 if ( (smi.meta_refcount != realcount)
814 || (realcount == 0) ) {
815 AdjRefCount(msgnum, (smi.meta_refcount - realcount));
822 cprintf("Freeing memory...\n");
837 /*****************************************************************************/
839 char *serv_expire_init(void)
841 CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
842 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");