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 PurgeList *UserPurgeList = NULL;
105 struct PurgeList *RoomPurgeList = NULL;
106 struct ValidRoom *ValidRoomList = NULL;
107 struct ValidUser *ValidUserList = NULL;
110 struct ctdlroomref *rr = NULL;
112 extern struct CitContext *ContextList;
116 * First phase of message purge -- gather the locations of messages which
117 * qualify for purging and write them to a temp file.
119 void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
120 struct ExpirePolicy epbuf;
123 struct CtdlMessage *msg;
125 struct cdbdata *cdbfr;
126 long *msglist = NULL;
130 purgelist = (FILE *)data;
131 fprintf(purgelist, "r=%s\n", qrbuf->QRname);
134 GetExpirePolicy(&epbuf, qrbuf);
136 /* If the room is set to never expire messages ... do nothing */
137 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
138 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
140 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
143 msglist = malloc(cdbfr->len);
144 memcpy(msglist, cdbfr->ptr, cdbfr->len);
145 num_msgs = cdbfr->len / sizeof(long);
149 /* Nothing to do if there aren't any messages */
151 if (msglist != NULL) free(msglist);
155 /* If the room is set to expire by count, do that */
156 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
157 if (num_msgs > epbuf.expire_value) {
158 for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
159 fprintf(purgelist, "m=%ld\n", msglist[a]);
165 /* If the room is set to expire by age... */
166 if (epbuf.expire_mode == EXPIRE_AGE) {
167 for (a=0; a<num_msgs; ++a) {
170 msg = CtdlFetchMessage(delnum, 0); /* dont need body */
172 xtime = atol(msg->cm_fields['T']);
173 CtdlFreeMessage(msg);
179 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
180 fprintf(purgelist, "m=%ld\n", delnum);
186 if (msglist != NULL) free(msglist);
191 * Second phase of message purge -- read list of msgs from temp file and
194 void DoPurgeMessages(FILE *purgelist) {
195 char roomname[ROOMNAMELEN];
200 strcpy(roomname, "nonexistent room ___ ___");
201 while (fgets(buf, sizeof buf, purgelist) != NULL) {
202 buf[strlen(buf)-1]=0;
203 if (!strncasecmp(buf, "r=", 2)) {
204 strcpy(roomname, &buf[2]);
206 if (!strncasecmp(buf, "m=", 2)) {
207 msgnum = atol(&buf[2]);
209 CtdlDeleteMessages(roomname, msgnum, "", 0);
216 void PurgeMessages(void) {
219 lprintf(CTDL_DEBUG, "PurgeMessages() called\n");
222 purgelist = tmpfile();
223 if (purgelist == NULL) {
224 lprintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
229 ForEachRoom(GatherPurgeMessages, (void *)purgelist );
230 DoPurgeMessages(purgelist);
235 void AddValidUser(struct ctdluser *usbuf, void *data) {
236 struct ValidUser *vuptr;
238 vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
239 vuptr->next = ValidUserList;
240 vuptr->vu_usernum = usbuf->usernum;
241 ValidUserList = vuptr;
244 void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
245 struct ValidRoom *vrptr;
247 vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
248 vrptr->next = ValidRoomList;
249 vrptr->vr_roomnum = qrbuf->QRnumber;
250 vrptr->vr_roomgen = qrbuf->QRgen;
251 ValidRoomList = vrptr;
254 void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
255 time_t age, purge_secs;
256 struct PurgeList *pptr;
257 struct ValidUser *vuptr;
260 /* For mailbox rooms, there's only one purging rule: if the user who
261 * owns the room still exists, we keep the room; otherwise, we purge
262 * it. Bypass any other rules.
264 if (qrbuf->QRflags & QR_MAILBOX) {
265 /* if user not found, do_purge will be 1 */
267 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
268 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
274 /* Any of these attributes render a room non-purgable */
275 if (qrbuf->QRflags & QR_PERMANENT) return;
276 if (qrbuf->QRflags & QR_DIRECTORY) return;
277 if (qrbuf->QRflags & QR_NETWORK) return;
278 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
279 if (is_noneditable(qrbuf)) return;
281 /* If we don't know the modification date, be safe and don't purge */
282 if (qrbuf->QRmtime <= (time_t)0) return;
284 /* If no room purge time is set, be safe and don't purge */
285 if (config.c_roompurge < 0) return;
287 /* Otherwise, check the date of last modification */
288 age = time(NULL) - (qrbuf->QRmtime);
289 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
290 if (purge_secs <= (time_t)0) return;
291 lprintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
292 if (age > purge_secs) do_purge = 1;
296 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
297 pptr->next = RoomPurgeList;
298 strcpy(pptr->name, qrbuf->QRname);
299 RoomPurgeList = pptr;
306 int PurgeRooms(void) {
307 struct PurgeList *pptr;
308 int num_rooms_purged = 0;
309 struct ctdlroom qrbuf;
310 struct ValidUser *vuptr;
311 char *transcript = NULL;
313 lprintf(CTDL_DEBUG, "PurgeRooms() called\n");
316 /* Load up a table full of valid user numbers so we can delete
317 * user-owned rooms for users who no longer exist */
318 ForEachUser(AddValidUser, NULL);
320 /* Then cycle through the room file */
321 ForEachRoom(DoPurgeRooms, NULL);
323 /* Free the valid user list */
324 while (ValidUserList != NULL) {
325 vuptr = ValidUserList->next;
327 ValidUserList = vuptr;
331 transcript = malloc(SIZ);
332 strcpy(transcript, "The following rooms have been auto-purged:\n");
334 while (RoomPurgeList != NULL) {
335 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
336 transcript=realloc(transcript, strlen(transcript)+SIZ);
337 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
341 pptr = RoomPurgeList->next;
343 RoomPurgeList = pptr;
347 if (num_rooms_purged > 0) aide_message(transcript);
350 lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
351 return(num_rooms_purged);
356 * Back end function to check user accounts for associated Unix accounts
357 * which no longer exist.
359 void do_uid_user_purge(struct ctdluser *us, void *data) {
360 #ifdef ENABLE_AUTOLOGIN
361 struct PurgeList *pptr;
363 if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
364 if (getpwuid(us->uid) == NULL) {
365 pptr = (struct PurgeList *)
366 malloc(sizeof(struct PurgeList));
367 pptr->next = UserPurgeList;
368 strcpy(pptr->name, us->fullname);
369 UserPurgeList = pptr;
373 #endif /* ENABLE_AUTOLOGIN */
379 * Back end function to check user accounts for expiration.
381 void do_user_purge(struct ctdluser *us, void *data) {
385 struct PurgeList *pptr;
387 /* stupid recovery routine to re-create missing mailboxen.
389 struct ctdlroom qrbuf;
390 char mailboxname[ROOMNAMELEN];
391 MailboxName(mailboxname, us, MAILROOM);
392 create_room(mailboxname, 4, "", 0, 1, 1, VIEW_BBS);
393 if (getroom(&qrbuf, mailboxname) != 0) return;
394 lprintf(CTDL_DEBUG, "Got %s\n", qrbuf.QRname);
398 /* Set purge time; if the user overrides the system default, use it */
399 if (us->USuserpurge > 0) {
400 purge_time = ((time_t)us->USuserpurge) * 86400L;
403 purge_time = ((time_t)config.c_userpurge) * 86400L;
406 /* The default rule is to not purge. */
409 /* If the user hasn't called in two months, his/her account
410 * has expired, so purge the record.
413 if ((now - us->lastcall) > purge_time) purge = 1;
415 /* If the user set his/her password to 'deleteme', he/she
416 * wishes to be deleted, so purge the record.
418 if (!strcasecmp(us->password, "deleteme")) purge = 1;
420 /* If the record is marked as permanent, don't purge it.
422 if (us->flags & US_PERM) 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;
451 int PurgeUsers(void) {
452 struct PurgeList *pptr;
453 int num_users_purged = 0;
454 char *transcript = NULL;
456 lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
457 if (config.c_userpurge > 0) {
458 ForEachUser(do_user_purge, NULL);
460 ForEachUser(do_uid_user_purge, NULL);
462 transcript = malloc(SIZ);
463 strcpy(transcript, "The following users have been auto-purged:\n");
465 while (UserPurgeList != NULL) {
466 transcript=realloc(transcript, strlen(transcript)+SIZ);
467 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
468 UserPurgeList->name);
469 purge_user(UserPurgeList->name);
470 pptr = UserPurgeList->next;
472 UserPurgeList = pptr;
476 if (num_users_purged > 0) aide_message(transcript);
479 lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
480 return(num_users_purged);
487 * This is a really cumbersome "garbage collection" function. We have to
488 * delete visits which refer to rooms and/or users which no longer exist. In
489 * order to prevent endless traversals of the room and user files, we first
490 * build linked lists of rooms and users which _do_ exist on the system, then
491 * traverse the visit file, checking each record against those two lists and
492 * purging the ones that do not have a match on _both_ lists. (Remember, if
493 * either the room or user being referred to is no longer on the system, the
494 * record is completely useless.)
496 int PurgeVisits(void) {
497 struct cdbdata *cdbvisit;
499 struct VPurgeList *VisitPurgeList = NULL;
500 struct VPurgeList *vptr;
504 struct ValidRoom *vrptr;
505 struct ValidUser *vuptr;
506 int RoomIsValid, UserIsValid;
508 /* First, load up a table full of valid room/gen combinations */
509 ForEachRoom(AddValidRoom, NULL);
511 /* Then load up a table full of valid user numbers */
512 ForEachUser(AddValidUser, NULL);
514 /* Now traverse through the visits, purging irrelevant records... */
515 cdb_rewind(CDB_VISIT);
516 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
517 memset(&vbuf, 0, sizeof(struct visit));
518 memcpy(&vbuf, cdbvisit->ptr,
519 ( (cdbvisit->len > sizeof(struct visit)) ?
520 sizeof(struct visit) : cdbvisit->len) );
526 /* Check to see if the room exists */
527 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
528 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
529 && (vrptr->vr_roomgen==vbuf.v_roomgen))
533 /* Check to see if the user exists */
534 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
535 if (vuptr->vu_usernum == vbuf.v_usernum)
539 /* Put the record on the purge list if it's dead */
540 if ((RoomIsValid==0) || (UserIsValid==0)) {
541 vptr = (struct VPurgeList *)
542 malloc(sizeof(struct VPurgeList));
543 vptr->next = VisitPurgeList;
544 vptr->vp_roomnum = vbuf.v_roomnum;
545 vptr->vp_roomgen = vbuf.v_roomgen;
546 vptr->vp_usernum = vbuf.v_usernum;
547 VisitPurgeList = vptr;
552 /* Free the valid room/gen combination list */
553 while (ValidRoomList != NULL) {
554 vrptr = ValidRoomList->next;
556 ValidRoomList = vrptr;
559 /* Free the valid user list */
560 while (ValidUserList != NULL) {
561 vuptr = ValidUserList->next;
563 ValidUserList = vuptr;
566 /* Now delete every visit on the purged list */
567 while (VisitPurgeList != NULL) {
568 IndexLen = GenerateRelationshipIndex(IndexBuf,
569 VisitPurgeList->vp_roomnum,
570 VisitPurgeList->vp_roomgen,
571 VisitPurgeList->vp_usernum);
572 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
573 vptr = VisitPurgeList->next;
574 free(VisitPurgeList);
575 VisitPurgeList = vptr;
583 * Purge the use table of old entries.
586 int PurgeUseTable(void) {
588 struct cdbdata *cdbut;
590 struct UPurgeList *ul = NULL;
591 struct UPurgeList *uptr;
593 /* Phase 1: traverse through the table, discovering old records... */
594 lprintf(CTDL_DEBUG, "Purge use table: phase 1\n");
595 cdb_rewind(CDB_USETABLE);
596 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
598 memcpy(&ut, cdbut->ptr,
599 ((cdbut->len > sizeof(struct UseTable)) ?
600 sizeof(struct UseTable) : cdbut->len));
603 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
604 uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
607 safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
615 /* Phase 2: delete the records */
616 lprintf(CTDL_DEBUG, "Purge use table: phase 2\n");
618 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
624 lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
629 void purge_databases(void) {
631 static time_t last_purge = 0;
635 /* Do the auto-purge if the current hour equals the purge hour,
636 * but not if the operation has already been performed in the
637 * last twelve hours. This is usually enough granularity.
640 localtime_r(&now, &tm);
641 if (tm.tm_hour != config.c_purge_hour) return;
642 if ((now - last_purge) < 43200) return;
644 lprintf(CTDL_INFO, "Auto-purger: starting.\n");
646 retval = PurgeUsers();
647 lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
650 lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
652 retval = PurgeRooms();
653 lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
655 retval = PurgeVisits();
656 lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
658 retval = PurgeUseTable();
659 lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
661 lprintf(CTDL_INFO, "Auto-purger: finished.\n");
663 last_purge = now; /* So we don't do it again soon */
666 /*****************************************************************************/
669 void do_fsck_msg(long msgnum, void *userdata) {
670 struct ctdlroomref *ptr;
672 ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
674 ptr->msgnum = msgnum;
678 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
680 getroom(&CC->room, qrbuf->QRname);
681 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
685 * Check message reference counts
687 void cmd_fsck(char *argbuf) {
689 struct cdbdata *cdbmsg;
691 struct ctdlroomref *ptr;
694 if (CtdlAccessCheck(ac_aide)) return;
696 /* Lame way of checking whether anyone else is doing this now */
698 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
702 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
704 cprintf("\nThis could take a while. Please be patient!\n\n");
705 cprintf("Gathering pointers...\n");
706 ForEachRoom(do_fsck_room, NULL);
709 cprintf("Checking message base...\n");
710 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
712 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
713 if (cdbmsg != NULL) {
715 cprintf("Message %7ld ", msgnum);
717 GetMetaData(&smi, msgnum);
718 cprintf("refcount=%-2d ", smi.meta_refcount);
721 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
722 if (ptr->msgnum == msgnum) ++realcount;
724 cprintf("realcount=%-2d\n", realcount);
726 if ( (smi.meta_refcount != realcount)
727 || (realcount == 0) ) {
728 smi.meta_refcount = realcount;
730 AdjRefCount(msgnum, 0); /* deletes if needed */
737 cprintf("Freeing memory...\n");
752 /*****************************************************************************/
754 char *serv_expire_init(void)
756 CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
757 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");