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, "");
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 != BBSUID)) {
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;
436 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
437 pptr->next = UserPurgeList;
438 strcpy(pptr->name, us->fullname);
439 UserPurgeList = pptr;
446 int PurgeUsers(void) {
447 struct PurgeList *pptr;
448 int num_users_purged = 0;
449 char *transcript = NULL;
451 lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
452 if (config.c_userpurge > 0) {
453 ForEachUser(do_user_purge, NULL);
455 ForEachUser(do_uid_user_purge, NULL);
457 transcript = malloc(SIZ);
458 strcpy(transcript, "The following users have been auto-purged:\n");
460 while (UserPurgeList != NULL) {
461 transcript=realloc(transcript, strlen(transcript)+SIZ);
462 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
463 UserPurgeList->name);
464 purge_user(UserPurgeList->name);
465 pptr = UserPurgeList->next;
467 UserPurgeList = pptr;
471 if (num_users_purged > 0) aide_message(transcript);
474 lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
475 return(num_users_purged);
482 * This is a really cumbersome "garbage collection" function. We have to
483 * delete visits which refer to rooms and/or users which no longer exist. In
484 * order to prevent endless traversals of the room and user files, we first
485 * build linked lists of rooms and users which _do_ exist on the system, then
486 * traverse the visit file, checking each record against those two lists and
487 * purging the ones that do not have a match on _both_ lists. (Remember, if
488 * either the room or user being referred to is no longer on the system, the
489 * record is completely useless.)
491 int PurgeVisits(void) {
492 struct cdbdata *cdbvisit;
494 struct VPurgeList *VisitPurgeList = NULL;
495 struct VPurgeList *vptr;
499 struct ValidRoom *vrptr;
500 struct ValidUser *vuptr;
501 int RoomIsValid, UserIsValid;
503 /* First, load up a table full of valid room/gen combinations */
504 ForEachRoom(AddValidRoom, NULL);
506 /* Then load up a table full of valid user numbers */
507 ForEachUser(AddValidUser, NULL);
509 /* Now traverse through the visits, purging irrelevant records... */
510 cdb_rewind(CDB_VISIT);
511 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
512 memset(&vbuf, 0, sizeof(struct visit));
513 memcpy(&vbuf, cdbvisit->ptr,
514 ( (cdbvisit->len > sizeof(struct visit)) ?
515 sizeof(struct visit) : cdbvisit->len) );
521 /* Check to see if the room exists */
522 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
523 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
524 && (vrptr->vr_roomgen==vbuf.v_roomgen))
528 /* Check to see if the user exists */
529 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
530 if (vuptr->vu_usernum == vbuf.v_usernum)
534 /* Put the record on the purge list if it's dead */
535 if ((RoomIsValid==0) || (UserIsValid==0)) {
536 vptr = (struct VPurgeList *)
537 malloc(sizeof(struct VPurgeList));
538 vptr->next = VisitPurgeList;
539 vptr->vp_roomnum = vbuf.v_roomnum;
540 vptr->vp_roomgen = vbuf.v_roomgen;
541 vptr->vp_usernum = vbuf.v_usernum;
542 VisitPurgeList = vptr;
547 /* Free the valid room/gen combination list */
548 while (ValidRoomList != NULL) {
549 vrptr = ValidRoomList->next;
551 ValidRoomList = vrptr;
554 /* Free the valid user list */
555 while (ValidUserList != NULL) {
556 vuptr = ValidUserList->next;
558 ValidUserList = vuptr;
561 /* Now delete every visit on the purged list */
562 while (VisitPurgeList != NULL) {
563 IndexLen = GenerateRelationshipIndex(IndexBuf,
564 VisitPurgeList->vp_roomnum,
565 VisitPurgeList->vp_roomgen,
566 VisitPurgeList->vp_usernum);
567 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
568 vptr = VisitPurgeList->next;
569 free(VisitPurgeList);
570 VisitPurgeList = vptr;
578 * Purge the use table of old entries.
581 int PurgeUseTable(void) {
583 struct cdbdata *cdbut;
585 struct UPurgeList *ul = NULL;
586 struct UPurgeList *uptr;
588 /* Phase 1: traverse through the table, discovering old records... */
589 lprintf(CTDL_DEBUG, "Purge use table: phase 1\n");
590 cdb_rewind(CDB_USETABLE);
591 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
593 memcpy(&ut, cdbut->ptr,
594 ((cdbut->len > sizeof(struct UseTable)) ?
595 sizeof(struct UseTable) : cdbut->len));
598 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
599 uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
602 safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
610 /* Phase 2: delete the records */
611 lprintf(CTDL_DEBUG, "Purge use table: phase 2\n");
613 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
619 lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
624 void purge_databases(void) {
626 static time_t last_purge = 0;
630 /* Do the auto-purge if the current hour equals the purge hour,
631 * but not if the operation has already been performed in the
632 * last twelve hours. This is usually enough granularity.
635 memcpy(&tm, localtime(&now), sizeof(struct tm));
636 if (tm.tm_hour != config.c_purge_hour) return;
637 if ((now - last_purge) < 43200) return;
639 lprintf(CTDL_INFO, "Auto-purger: starting.\n");
641 retval = PurgeUsers();
642 lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
645 lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
647 retval = PurgeRooms();
648 lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
650 retval = PurgeVisits();
651 lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
653 retval = PurgeUseTable();
654 lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
656 lprintf(CTDL_INFO, "Auto-purger: finished.\n");
658 last_purge = now; /* So we don't do it again soon */
661 /*****************************************************************************/
664 void do_fsck_msg(long msgnum, void *userdata) {
665 struct ctdlroomref *ptr;
667 ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
669 ptr->msgnum = msgnum;
673 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
675 getroom(&CC->room, qrbuf->QRname);
676 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
680 * Check message reference counts
682 void cmd_fsck(char *argbuf) {
684 struct cdbdata *cdbmsg;
686 struct ctdlroomref *ptr;
689 if (CtdlAccessCheck(ac_aide)) return;
691 /* Lame way of checking whether anyone else is doing this now */
693 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
697 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
699 cprintf("\nThis could take a while. Please be patient!\n\n");
700 cprintf("Gathering pointers...\n");
701 ForEachRoom(do_fsck_room, NULL);
704 cprintf("Checking message base...\n");
705 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
707 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
708 if (cdbmsg != NULL) {
710 cprintf("Message %7ld ", msgnum);
712 GetMetaData(&smi, msgnum);
713 cprintf("refcount=%-2d ", smi.meta_refcount);
716 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
717 if (ptr->msgnum == msgnum) ++realcount;
719 cprintf("realcount=%-2d\n", realcount);
721 if ( (smi.meta_refcount != realcount)
722 || (realcount == 0) ) {
723 smi.meta_refcount = realcount;
725 AdjRefCount(msgnum, 0); /* deletes if needed */
732 cprintf("Freeing memory...\n");
747 /*****************************************************************************/
749 char *serv_expire_init(void)
751 CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
752 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");