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;
116 struct ctdlroomref *rr = NULL;
118 extern struct CitContext *ContextList;
122 * First phase of message purge -- gather the locations of messages which
123 * qualify for purging and write them to a temp file.
125 void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
126 struct ExpirePolicy epbuf;
129 struct CtdlMessage *msg;
131 struct cdbdata *cdbfr;
132 long *msglist = NULL;
136 purgelist = (FILE *)data;
137 fprintf(purgelist, "r=%s\n", qrbuf->QRname);
140 GetExpirePolicy(&epbuf, qrbuf);
142 /* If the room is set to never expire messages ... do nothing */
143 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
144 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
146 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
149 msglist = malloc(cdbfr->len);
150 memcpy(msglist, cdbfr->ptr, cdbfr->len);
151 num_msgs = cdbfr->len / sizeof(long);
155 /* Nothing to do if there aren't any messages */
157 if (msglist != NULL) free(msglist);
161 /* If the room is set to expire by count, do that */
162 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
163 if (num_msgs > epbuf.expire_value) {
164 for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
165 fprintf(purgelist, "m=%ld\n", msglist[a]);
171 /* If the room is set to expire by age... */
172 if (epbuf.expire_mode == EXPIRE_AGE) {
173 for (a=0; a<num_msgs; ++a) {
176 msg = CtdlFetchMessage(delnum, 0); /* dont need body */
178 xtime = atol(msg->cm_fields['T']);
179 CtdlFreeMessage(msg);
185 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
186 fprintf(purgelist, "m=%ld\n", delnum);
192 if (msglist != NULL) free(msglist);
197 * Second phase of message purge -- read list of msgs from temp file and
200 void DoPurgeMessages(FILE *purgelist) {
201 char roomname[ROOMNAMELEN];
206 strcpy(roomname, "nonexistent room ___ ___");
207 while (fgets(buf, sizeof buf, purgelist) != NULL) {
208 buf[strlen(buf)-1]=0;
209 if (!strncasecmp(buf, "r=", 2)) {
210 strcpy(roomname, &buf[2]);
212 if (!strncasecmp(buf, "m=", 2)) {
213 msgnum = atol(&buf[2]);
215 CtdlDeleteMessages(roomname, msgnum, "", 0);
222 void PurgeMessages(void) {
225 lprintf(CTDL_DEBUG, "PurgeMessages() called\n");
228 purgelist = tmpfile();
229 if (purgelist == NULL) {
230 lprintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
235 ForEachRoom(GatherPurgeMessages, (void *)purgelist );
236 DoPurgeMessages(purgelist);
241 void AddValidUser(struct ctdluser *usbuf, void *data) {
242 struct ValidUser *vuptr;
244 vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
245 vuptr->next = ValidUserList;
246 vuptr->vu_usernum = usbuf->usernum;
247 ValidUserList = vuptr;
250 void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
251 struct ValidRoom *vrptr;
253 vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
254 vrptr->next = ValidRoomList;
255 vrptr->vr_roomnum = qrbuf->QRnumber;
256 vrptr->vr_roomgen = qrbuf->QRgen;
257 ValidRoomList = vrptr;
260 void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
261 time_t age, purge_secs;
262 struct PurgeList *pptr;
263 struct ValidUser *vuptr;
266 /* For mailbox rooms, there's only one purging rule: if the user who
267 * owns the room still exists, we keep the room; otherwise, we purge
268 * it. Bypass any other rules.
270 if (qrbuf->QRflags & QR_MAILBOX) {
271 /* if user not found, do_purge will be 1 */
273 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
274 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
280 /* Any of these attributes render a room non-purgable */
281 if (qrbuf->QRflags & QR_PERMANENT) return;
282 if (qrbuf->QRflags & QR_DIRECTORY) return;
283 if (qrbuf->QRflags & QR_NETWORK) return;
284 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
285 if (is_noneditable(qrbuf)) return;
287 /* If we don't know the modification date, be safe and don't purge */
288 if (qrbuf->QRmtime <= (time_t)0) return;
290 /* If no room purge time is set, be safe and don't purge */
291 if (config.c_roompurge < 0) return;
293 /* Otherwise, check the date of last modification */
294 age = time(NULL) - (qrbuf->QRmtime);
295 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
296 if (purge_secs <= (time_t)0) return;
297 lprintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
298 if (age > purge_secs) do_purge = 1;
302 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
303 pptr->next = RoomPurgeList;
304 strcpy(pptr->name, qrbuf->QRname);
305 RoomPurgeList = pptr;
312 int PurgeRooms(void) {
313 struct PurgeList *pptr;
314 int num_rooms_purged = 0;
315 struct ctdlroom qrbuf;
316 struct ValidUser *vuptr;
317 char *transcript = NULL;
319 lprintf(CTDL_DEBUG, "PurgeRooms() called\n");
322 /* Load up a table full of valid user numbers so we can delete
323 * user-owned rooms for users who no longer exist */
324 ForEachUser(AddValidUser, NULL);
326 /* Then cycle through the room file */
327 ForEachRoom(DoPurgeRooms, NULL);
329 /* Free the valid user list */
330 while (ValidUserList != NULL) {
331 vuptr = ValidUserList->next;
333 ValidUserList = vuptr;
337 transcript = malloc(SIZ);
338 strcpy(transcript, "The following rooms have been auto-purged:\n");
340 while (RoomPurgeList != NULL) {
341 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
342 transcript=realloc(transcript, strlen(transcript)+SIZ);
343 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
347 pptr = RoomPurgeList->next;
349 RoomPurgeList = pptr;
353 if (num_rooms_purged > 0) aide_message(transcript);
356 lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
357 return(num_rooms_purged);
362 * Back end function to check user accounts for associated Unix accounts
363 * which no longer exist.
365 void do_uid_user_purge(struct ctdluser *us, void *data) {
366 #ifdef ENABLE_AUTOLOGIN
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;
379 #endif /* ENABLE_AUTOLOGIN */
385 * Back end function to check user accounts for expiration.
387 void do_user_purge(struct ctdluser *us, void *data) {
391 struct PurgeList *pptr;
393 /* stupid recovery routine to re-create missing mailboxen.
395 struct ctdlroom qrbuf;
396 char mailboxname[ROOMNAMELEN];
397 MailboxName(mailboxname, us, MAILROOM);
398 create_room(mailboxname, 4, "", 0, 1, 1, VIEW_BBS);
399 if (getroom(&qrbuf, mailboxname) != 0) return;
400 lprintf(CTDL_DEBUG, "Got %s\n", qrbuf.QRname);
404 /* Set purge time; if the user overrides the system default, use it */
405 if (us->USuserpurge > 0) {
406 purge_time = ((time_t)us->USuserpurge) * 86400L;
409 purge_time = ((time_t)config.c_userpurge) * 86400L;
412 /* The default rule is to not purge. */
415 /* If the user hasn't called in two months, his/her account
416 * has expired, so purge the record.
419 if ((now - us->lastcall) > purge_time) purge = 1;
421 /* If the user set his/her password to 'deleteme', he/she
422 * wishes to be deleted, so purge the record.
424 if (!strcasecmp(us->password, "deleteme")) purge = 1;
426 /* If the record is marked as permanent, don't purge it.
428 if (us->flags & US_PERM) purge = 0;
430 /* If the access level is 0, the record should already have been
431 * deleted, but maybe the user was logged in at the time or something.
432 * Delete the record now.
434 if (us->axlevel == 0) purge = 1;
436 /* 0 calls is impossible. If there are 0 calls, it must
437 * be a corrupted record, so purge it.
439 if (us->timescalled == 0) purge = 1;
441 /* User number 0, as well as any negative user number, is
444 if (us->usernum < 1L) purge = 1;
447 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
448 pptr->next = UserPurgeList;
449 strcpy(pptr->name, us->fullname);
450 UserPurgeList = pptr;
457 int PurgeUsers(void) {
458 struct PurgeList *pptr;
459 int num_users_purged = 0;
460 char *transcript = NULL;
462 lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
463 if (config.c_userpurge > 0) {
464 ForEachUser(do_user_purge, NULL);
466 ForEachUser(do_uid_user_purge, NULL);
468 transcript = malloc(SIZ);
469 strcpy(transcript, "The following users have been auto-purged:\n");
471 while (UserPurgeList != NULL) {
472 transcript=realloc(transcript, strlen(transcript)+SIZ);
473 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
474 UserPurgeList->name);
475 purge_user(UserPurgeList->name);
476 pptr = UserPurgeList->next;
478 UserPurgeList = pptr;
482 if (num_users_purged > 0) aide_message(transcript);
485 lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
486 return(num_users_purged);
493 * This is a really cumbersome "garbage collection" function. We have to
494 * delete visits which refer to rooms and/or users which no longer exist. In
495 * order to prevent endless traversals of the room and user files, we first
496 * build linked lists of rooms and users which _do_ exist on the system, then
497 * traverse the visit file, checking each record against those two lists and
498 * purging the ones that do not have a match on _both_ lists. (Remember, if
499 * either the room or user being referred to is no longer on the system, the
500 * record is completely useless.)
502 int PurgeVisits(void) {
503 struct cdbdata *cdbvisit;
505 struct VPurgeList *VisitPurgeList = NULL;
506 struct VPurgeList *vptr;
510 struct ValidRoom *vrptr;
511 struct ValidUser *vuptr;
512 int RoomIsValid, UserIsValid;
514 /* First, load up a table full of valid room/gen combinations */
515 ForEachRoom(AddValidRoom, NULL);
517 /* Then load up a table full of valid user numbers */
518 ForEachUser(AddValidUser, NULL);
520 /* Now traverse through the visits, purging irrelevant records... */
521 cdb_rewind(CDB_VISIT);
522 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
523 memset(&vbuf, 0, sizeof(struct visit));
524 memcpy(&vbuf, cdbvisit->ptr,
525 ( (cdbvisit->len > sizeof(struct visit)) ?
526 sizeof(struct visit) : cdbvisit->len) );
532 /* Check to see if the room exists */
533 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
534 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
535 && (vrptr->vr_roomgen==vbuf.v_roomgen))
539 /* Check to see if the user exists */
540 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
541 if (vuptr->vu_usernum == vbuf.v_usernum)
545 /* Put the record on the purge list if it's dead */
546 if ((RoomIsValid==0) || (UserIsValid==0)) {
547 vptr = (struct VPurgeList *)
548 malloc(sizeof(struct VPurgeList));
549 vptr->next = VisitPurgeList;
550 vptr->vp_roomnum = vbuf.v_roomnum;
551 vptr->vp_roomgen = vbuf.v_roomgen;
552 vptr->vp_usernum = vbuf.v_usernum;
553 VisitPurgeList = vptr;
558 /* Free the valid room/gen combination list */
559 while (ValidRoomList != NULL) {
560 vrptr = ValidRoomList->next;
562 ValidRoomList = vrptr;
565 /* Free the valid user list */
566 while (ValidUserList != NULL) {
567 vuptr = ValidUserList->next;
569 ValidUserList = vuptr;
572 /* Now delete every visit on the purged list */
573 while (VisitPurgeList != NULL) {
574 IndexLen = GenerateRelationshipIndex(IndexBuf,
575 VisitPurgeList->vp_roomnum,
576 VisitPurgeList->vp_roomgen,
577 VisitPurgeList->vp_usernum);
578 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
579 vptr = VisitPurgeList->next;
580 free(VisitPurgeList);
581 VisitPurgeList = vptr;
589 * Purge the use table of old entries.
592 int PurgeUseTable(void) {
594 struct cdbdata *cdbut;
596 struct UPurgeList *ul = NULL;
597 struct UPurgeList *uptr;
599 /* Phase 1: traverse through the table, discovering old records... */
600 lprintf(CTDL_DEBUG, "Purge use table: phase 1\n");
601 cdb_rewind(CDB_USETABLE);
602 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
604 memcpy(&ut, cdbut->ptr,
605 ((cdbut->len > sizeof(struct UseTable)) ?
606 sizeof(struct UseTable) : cdbut->len));
609 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
610 uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
613 safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
621 /* Phase 2: delete the records */
622 lprintf(CTDL_DEBUG, "Purge use table: phase 2\n");
624 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
630 lprintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
637 * Purge the EUID Index of old records.
640 int PurgeEuidIndexTable(void) {
642 struct cdbdata *cdbei;
643 struct EPurgeList *el = NULL;
644 struct EPurgeList *eptr;
646 struct CtdlMessage *msg;
648 /* Phase 1: traverse through the table, discovering old records... */
649 lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
650 cdb_rewind(CDB_EUIDINDEX);
651 while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
653 memcpy(&msgnum, cdbei->ptr, sizeof(long));
655 msg = CtdlFetchMessage(msgnum, 0);
657 CtdlFreeMessage(msg); /* it still exists, so do nothing */
660 eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
663 eptr->ep_keylen = cdbei->len - sizeof(long);
664 eptr->ep_key = malloc(cdbei->len);
665 memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
675 /* Phase 2: delete the records */
676 lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
678 cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
685 lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
690 void purge_databases(void) {
692 static time_t last_purge = 0;
696 /* Do the auto-purge if the current hour equals the purge hour,
697 * but not if the operation has already been performed in the
698 * last twelve hours. This is usually enough granularity.
701 localtime_r(&now, &tm);
702 if (tm.tm_hour != config.c_purge_hour) return;
703 if ((now - last_purge) < 43200) return;
705 lprintf(CTDL_INFO, "Auto-purger: starting.\n");
707 retval = PurgeUsers();
708 lprintf(CTDL_NOTICE, "Purged %d users.\n", retval);
711 lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
713 retval = PurgeRooms();
714 lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
716 retval = PurgeVisits();
717 lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
719 retval = PurgeUseTable();
720 lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
722 retval = PurgeEuidIndexTable();
723 lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
725 lprintf(CTDL_INFO, "Auto-purger: finished.\n");
727 last_purge = now; /* So we don't do it again soon */
730 /*****************************************************************************/
733 void do_fsck_msg(long msgnum, void *userdata) {
734 struct ctdlroomref *ptr;
736 ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
738 ptr->msgnum = msgnum;
742 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
744 getroom(&CC->room, qrbuf->QRname);
745 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
749 * Check message reference counts
751 void cmd_fsck(char *argbuf) {
753 struct cdbdata *cdbmsg;
755 struct ctdlroomref *ptr;
758 if (CtdlAccessCheck(ac_aide)) return;
760 /* Lame way of checking whether anyone else is doing this now */
762 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
766 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
768 cprintf("\nThis could take a while. Please be patient!\n\n");
769 cprintf("Gathering pointers...\n");
770 ForEachRoom(do_fsck_room, NULL);
773 cprintf("Checking message base...\n");
774 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
776 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
777 if (cdbmsg != NULL) {
779 cprintf("Message %7ld ", msgnum);
781 GetMetaData(&smi, msgnum);
782 cprintf("refcount=%-2d ", smi.meta_refcount);
785 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
786 if (ptr->msgnum == msgnum) ++realcount;
788 cprintf("realcount=%-2d\n", realcount);
790 if ( (smi.meta_refcount != realcount)
791 || (realcount == 0) ) {
792 smi.meta_refcount = realcount;
794 AdjRefCount(msgnum, 0); /* deletes if needed */
801 cprintf("Freeing memory...\n");
816 /*****************************************************************************/
818 char *serv_expire_init(void)
820 CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
821 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");