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 = mallok(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) phree(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);
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) phree(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, "");
216 void PurgeMessages(void) {
219 lprintf(5, "PurgeMessages() called\n");
222 purgelist = tmpfile();
223 if (purgelist == NULL) {
224 lprintf(3, "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 *)mallok(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 *)mallok(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(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
292 if (age > purge_secs) do_purge = 1;
296 pptr = (struct PurgeList *) mallok(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(5, "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;
326 phree(ValidUserList);
327 ValidUserList = vuptr;
331 transcript = mallok(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=reallok(transcript, strlen(transcript)+SIZ);
337 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
341 pptr = RoomPurgeList->next;
342 phree(RoomPurgeList);
343 RoomPurgeList = pptr;
347 if (num_rooms_purged > 0) aide_message(transcript);
350 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
351 return(num_rooms_purged);
355 void do_user_purge(struct ctdluser *us, void *data) {
359 struct PurgeList *pptr;
361 /* stupid recovery routine to re-create missing mailboxen.
363 struct ctdlroom qrbuf;
364 char mailboxname[ROOMNAMELEN];
365 MailboxName(mailboxname, us, MAILROOM);
366 create_room(mailboxname, 4, "", 0, 1, 1);
367 if (getroom(&qrbuf, mailboxname) != 0) return;
368 lprintf(9, "Got %s\n", qrbuf.QRname);
372 /* Set purge time; if the user overrides the system default, use it */
373 if (us->USuserpurge > 0) {
374 purge_time = ((time_t)us->USuserpurge) * 86400L;
377 purge_time = ((time_t)config.c_userpurge) * 86400L;
380 /* The default rule is to not purge. */
383 /* If the user hasn't called in two months, his/her account
384 * has expired, so purge the record.
387 if ((now - us->lastcall) > purge_time) purge = 1;
389 /* If the user set his/her password to 'deleteme', he/she
390 * wishes to be deleted, so purge the record.
392 if (!strcasecmp(us->password, "deleteme")) purge = 1;
394 /* If the record is marked as permanent, don't purge it.
396 if (us->flags & US_PERM) purge = 0;
398 /* If the access level is 0, the record should already have been
399 * deleted, but maybe the user was logged in at the time or something.
400 * Delete the record now.
402 if (us->axlevel == 0) purge = 1;
404 /* 0 calls is impossible. If there are 0 calls, it must
405 * be a corrupted record, so purge it.
407 if (us->timescalled == 0) purge = 1;
410 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
411 pptr->next = UserPurgeList;
412 strcpy(pptr->name, us->fullname);
413 UserPurgeList = pptr;
420 int PurgeUsers(void) {
421 struct PurgeList *pptr;
422 int num_users_purged = 0;
423 char *transcript = NULL;
425 lprintf(5, "PurgeUsers() called\n");
426 if (config.c_userpurge > 0) {
427 ForEachUser(do_user_purge, NULL);
430 transcript = mallok(SIZ);
431 strcpy(transcript, "The following users have been auto-purged:\n");
433 while (UserPurgeList != NULL) {
434 transcript=reallok(transcript, strlen(transcript)+SIZ);
435 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
436 UserPurgeList->name);
437 purge_user(UserPurgeList->name);
438 pptr = UserPurgeList->next;
439 phree(UserPurgeList);
440 UserPurgeList = pptr;
444 if (num_users_purged > 0) aide_message(transcript);
447 lprintf(5, "Purged %d users.\n", num_users_purged);
448 return(num_users_purged);
455 * This is a really cumbersome "garbage collection" function. We have to
456 * delete visits which refer to rooms and/or users which no longer exist. In
457 * order to prevent endless traversals of the room and user files, we first
458 * build linked lists of rooms and users which _do_ exist on the system, then
459 * traverse the visit file, checking each record against those two lists and
460 * purging the ones that do not have a match on _both_ lists. (Remember, if
461 * either the room or user being referred to is no longer on the system, the
462 * record is completely useless.)
464 int PurgeVisits(void) {
465 struct cdbdata *cdbvisit;
467 struct VPurgeList *VisitPurgeList = NULL;
468 struct VPurgeList *vptr;
472 struct ValidRoom *vrptr;
473 struct ValidUser *vuptr;
474 int RoomIsValid, UserIsValid;
476 /* First, load up a table full of valid room/gen combinations */
477 ForEachRoom(AddValidRoom, NULL);
479 /* Then load up a table full of valid user numbers */
480 ForEachUser(AddValidUser, NULL);
482 /* Now traverse through the visits, purging irrelevant records... */
483 cdb_rewind(CDB_VISIT);
484 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
485 memset(&vbuf, 0, sizeof(struct visit));
486 memcpy(&vbuf, cdbvisit->ptr,
487 ( (cdbvisit->len > sizeof(struct visit)) ?
488 sizeof(struct visit) : cdbvisit->len) );
494 /* Check to see if the room exists */
495 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
496 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
497 && (vrptr->vr_roomgen==vbuf.v_roomgen))
501 /* Check to see if the user exists */
502 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
503 if (vuptr->vu_usernum == vbuf.v_usernum)
507 /* Put the record on the purge list if it's dead */
508 if ((RoomIsValid==0) || (UserIsValid==0)) {
509 vptr = (struct VPurgeList *)
510 mallok(sizeof(struct VPurgeList));
511 vptr->next = VisitPurgeList;
512 vptr->vp_roomnum = vbuf.v_roomnum;
513 vptr->vp_roomgen = vbuf.v_roomgen;
514 vptr->vp_usernum = vbuf.v_usernum;
515 VisitPurgeList = vptr;
520 /* Free the valid room/gen combination list */
521 while (ValidRoomList != NULL) {
522 vrptr = ValidRoomList->next;
523 phree(ValidRoomList);
524 ValidRoomList = vrptr;
527 /* Free the valid user list */
528 while (ValidUserList != NULL) {
529 vuptr = ValidUserList->next;
530 phree(ValidUserList);
531 ValidUserList = vuptr;
534 /* Now delete every visit on the purged list */
535 while (VisitPurgeList != NULL) {
536 IndexLen = GenerateRelationshipIndex(IndexBuf,
537 VisitPurgeList->vp_roomnum,
538 VisitPurgeList->vp_roomgen,
539 VisitPurgeList->vp_usernum);
540 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
541 vptr = VisitPurgeList->next;
542 phree(VisitPurgeList);
543 VisitPurgeList = vptr;
551 * Purge the use table of old entries.
554 int PurgeUseTable(void) {
556 struct cdbdata *cdbut;
558 struct UPurgeList *ul = NULL;
559 struct UPurgeList *uptr;
561 /* Phase 1: traverse through the table, discovering old records... */
562 lprintf(9, "Purge use table: phase 1\n");
563 cdb_rewind(CDB_USETABLE);
564 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
566 memcpy(&ut, cdbut->ptr,
567 ((cdbut->len > sizeof(struct UseTable)) ?
568 sizeof(struct UseTable) : cdbut->len));
571 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
572 uptr = (struct UPurgeList *) mallok(sizeof(struct UPurgeList));
575 safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
583 /* Phase 2: delete the records */
584 lprintf(9, "Purge use table: phase 2\n");
586 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
592 lprintf(9, "Purge use table: finished (purged %d records)\n", purged);
597 void purge_databases(void) {
599 static time_t last_purge = 0;
603 /* Do the auto-purge if the current hour equals the purge hour,
604 * but not if the operation has already been performed in the
605 * last twelve hours. This is usually enough granularity.
608 memcpy(&tm, localtime(&now), sizeof(struct tm));
609 if (tm.tm_hour != config.c_purge_hour) return;
610 if ((now - last_purge) < 43200) return;
612 lprintf(3, "Auto-purger: starting.\n");
614 retval = PurgeUsers();
615 lprintf(3, "Purged %d users.\n", retval);
618 lprintf(3, "Expired %d messages.\n", messages_purged);
620 retval = PurgeRooms();
621 lprintf(3, "Expired %d rooms.\n", retval);
623 retval = PurgeVisits();
624 lprintf(3, "Purged %d visits.\n", retval);
626 retval = PurgeUseTable();
627 lprintf(3, "Purged %d entries from the use table.\n", retval);
629 lprintf(3, "Auto-purger: finished.\n");
631 last_purge = now; /* So we don't do it again soon */
634 /*****************************************************************************/
637 void do_fsck_msg(long msgnum, void *userdata) {
638 struct ctdlroomref *ptr;
640 ptr = (struct ctdlroomref *)mallok(sizeof(struct ctdlroomref));
642 ptr->msgnum = msgnum;
646 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
648 getroom(&CC->room, qrbuf->QRname);
649 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
653 * Check message reference counts
655 void cmd_fsck(char *argbuf) {
657 struct cdbdata *cdbmsg;
659 struct ctdlroomref *ptr;
662 if (CtdlAccessCheck(ac_aide)) return;
664 /* Lame way of checking whether anyone else is doing this now */
666 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
670 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
672 cprintf("\nThis could take a while. Please be patient!\n\n");
673 cprintf("Gathering pointers...\n");
674 ForEachRoom(do_fsck_room, NULL);
677 cprintf("Checking message base...\n");
678 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
680 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
681 if (cdbmsg != NULL) {
683 cprintf("Message %7ld ", msgnum);
685 GetMetaData(&smi, msgnum);
686 cprintf("refcount=%-2d ", smi.meta_refcount);
689 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
690 if (ptr->msgnum == msgnum) ++realcount;
692 cprintf("realcount=%-2d\n", realcount);
694 if ( (smi.meta_refcount != realcount)
695 || (realcount == 0) ) {
696 smi.meta_refcount = realcount;
698 AdjRefCount(msgnum, 0); /* deletes if needed */
705 cprintf("Freeing memory...\n");
720 /*****************************************************************************/
722 char *serv_expire_init(void)
724 CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
725 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");