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 GDBM (and perhaps some other backends we
18 * 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>
55 #include "sysdep_decls.h"
56 #include "citserver.h"
59 #include "dynloader.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;
99 struct PurgeList *UserPurgeList = NULL;
100 struct PurgeList *RoomPurgeList = NULL;
101 struct ValidRoom *ValidRoomList = NULL;
102 struct ValidUser *ValidUserList = NULL;
105 struct roomref *rr = NULL;
107 extern struct CitContext *ContextList;
111 * First phase of message purge -- gather the locations of messages which
112 * qualify for purging and write them to a temp file.
114 void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
115 struct ExpirePolicy epbuf;
118 struct CtdlMessage *msg;
120 struct cdbdata *cdbfr;
121 long *msglist = NULL;
125 purgelist = (FILE *)data;
126 fprintf(purgelist, "r=%s\n", qrbuf->QRname);
129 GetExpirePolicy(&epbuf, qrbuf);
131 /* If the room is set to never expire messages ... do nothing */
132 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
133 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
135 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
138 msglist = mallok(cdbfr->len);
139 memcpy(msglist, cdbfr->ptr, cdbfr->len);
140 num_msgs = cdbfr->len / sizeof(long);
144 /* Nothing to do if there aren't any messages */
146 if (msglist != NULL) phree(msglist);
150 /* If the room is set to expire by count, do that */
151 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
152 if (num_msgs > epbuf.expire_value) {
153 for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
154 fprintf(purgelist, "m=%ld\n", msglist[a]);
160 /* If the room is set to expire by age... */
161 if (epbuf.expire_mode == EXPIRE_AGE) {
162 for (a=0; a<num_msgs; ++a) {
165 msg = CtdlFetchMessage(delnum);
167 xtime = atol(msg->cm_fields['T']);
168 CtdlFreeMessage(msg);
174 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
175 fprintf(purgelist, "m=%ld\n", delnum);
181 if (msglist != NULL) phree(msglist);
186 * Second phase of message purge -- read list of msgs from temp file and
189 void DoPurgeMessages(FILE *purgelist) {
190 char roomname[ROOMNAMELEN];
195 strcpy(roomname, "nonexistent room ___ ___");
196 while (fgets(buf, sizeof buf, purgelist) != NULL) {
197 buf[strlen(buf)-1]=0;
198 if (!strncasecmp(buf, "r=", 2)) {
199 strcpy(roomname, &buf[2]);
201 if (!strncasecmp(buf, "m=", 2)) {
202 msgnum = atol(&buf[2]);
204 CtdlDeleteMessages(roomname, msgnum, "");
211 void PurgeMessages(void) {
214 lprintf(5, "PurgeMessages() called\n");
217 purgelist = tmpfile();
218 if (purgelist == NULL) {
219 lprintf(3, "Can't create purgelist temp file: %s\n",
224 ForEachRoom(GatherPurgeMessages, (void *)purgelist );
225 DoPurgeMessages(purgelist);
230 void AddValidUser(struct usersupp *usbuf, void *data) {
231 struct ValidUser *vuptr;
233 vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
234 vuptr->next = ValidUserList;
235 vuptr->vu_usernum = usbuf->usernum;
236 ValidUserList = vuptr;
239 void AddValidRoom(struct quickroom *qrbuf, void *data) {
240 struct ValidRoom *vrptr;
242 vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
243 vrptr->next = ValidRoomList;
244 vrptr->vr_roomnum = qrbuf->QRnumber;
245 vrptr->vr_roomgen = qrbuf->QRgen;
246 ValidRoomList = vrptr;
249 void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
250 time_t age, purge_secs;
251 struct PurgeList *pptr;
252 struct ValidUser *vuptr;
255 /* For mailbox rooms, there's only one purging rule: if the user who
256 * owns the room still exists, we keep the room; otherwise, we purge
257 * it. Bypass any other rules.
259 if (qrbuf->QRflags & QR_MAILBOX) {
260 /* if user not found, do_purge will be 1 */
262 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
263 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
269 /* Any of these attributes render a room non-purgable */
270 if (qrbuf->QRflags & QR_PERMANENT) return;
271 if (qrbuf->QRflags & QR_DIRECTORY) return;
272 if (qrbuf->QRflags & QR_NETWORK) return;
273 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
274 if (is_noneditable(qrbuf)) return;
276 /* If we don't know the modification date, be safe and don't purge */
277 if (qrbuf->QRmtime <= (time_t)0) return;
279 /* If no room purge time is set, be safe and don't purge */
280 if (config.c_roompurge < 0) return;
282 /* Otherwise, check the date of last modification */
283 age = time(NULL) - (qrbuf->QRmtime);
284 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
285 if (purge_secs <= (time_t)0) return;
286 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
287 if (age > purge_secs) do_purge = 1;
291 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
292 pptr->next = RoomPurgeList;
293 strcpy(pptr->name, qrbuf->QRname);
294 RoomPurgeList = pptr;
301 int PurgeRooms(void) {
302 struct PurgeList *pptr;
303 int num_rooms_purged = 0;
304 struct quickroom qrbuf;
305 struct ValidUser *vuptr;
306 char *transcript = NULL;
308 lprintf(5, "PurgeRooms() called\n");
311 /* Load up a table full of valid user numbers so we can delete
312 * user-owned rooms for users who no longer exist */
313 ForEachUser(AddValidUser, NULL);
315 /* Then cycle through the room file */
316 ForEachRoom(DoPurgeRooms, NULL);
318 /* Free the valid user list */
319 while (ValidUserList != NULL) {
320 vuptr = ValidUserList->next;
321 phree(ValidUserList);
322 ValidUserList = vuptr;
326 transcript = mallok(SIZ);
327 strcpy(transcript, "The following rooms have been auto-purged:\n");
329 while (RoomPurgeList != NULL) {
330 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
331 transcript=reallok(transcript, strlen(transcript)+SIZ);
332 sprintf(&transcript[strlen(transcript)], " %s\n",
336 pptr = RoomPurgeList->next;
337 phree(RoomPurgeList);
338 RoomPurgeList = pptr;
342 if (num_rooms_purged > 0) aide_message(transcript);
345 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
346 return(num_rooms_purged);
350 void do_user_purge(struct usersupp *us, void *data) {
354 struct PurgeList *pptr;
356 /* stupid recovery routine to re-create missing mailboxen.
358 struct quickroom qrbuf;
359 char mailboxname[ROOMNAMELEN];
360 MailboxName(mailboxname, us, MAILROOM);
361 create_room(mailboxname, 4, "", 0, 1);
362 if (getroom(&qrbuf, mailboxname) != 0) return;
363 lprintf(9, "Got %s\n", qrbuf.QRname);
367 /* Set purge time; if the user overrides the system default, use it */
368 if (us->USuserpurge > 0) {
369 purge_time = ((time_t)us->USuserpurge) * 86400L;
372 purge_time = ((time_t)config.c_userpurge) * 86400L;
375 /* The default rule is to not purge. */
378 /* If the user hasn't called in two months, his/her account
379 * has expired, so purge the record.
382 if ((now - us->lastcall) > purge_time) purge = 1;
384 /* If the user set his/her password to 'deleteme', he/she
385 * wishes to be deleted, so purge the record.
387 if (!strcasecmp(us->password, "deleteme")) purge = 1;
389 /* If the record is marked as permanent, don't purge it.
391 if (us->flags & US_PERM) purge = 0;
393 /* If the access level is 0, the record should already have been
394 * deleted, but maybe the user was logged in at the time or something.
395 * Delete the record now.
397 if (us->axlevel == 0) purge = 1;
399 /* 0 calls is impossible. If there are 0 calls, it must
400 * be a corrupted record, so purge it.
402 if (us->timescalled == 0) purge = 1;
405 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
406 pptr->next = UserPurgeList;
407 strcpy(pptr->name, us->fullname);
408 UserPurgeList = pptr;
415 int PurgeUsers(void) {
416 struct PurgeList *pptr;
417 int num_users_purged = 0;
418 char *transcript = NULL;
420 lprintf(5, "PurgeUsers() called\n");
421 if (config.c_userpurge > 0) {
422 ForEachUser(do_user_purge, NULL);
425 transcript = mallok(SIZ);
426 strcpy(transcript, "The following users have been auto-purged:\n");
428 while (UserPurgeList != NULL) {
429 transcript=reallok(transcript, strlen(transcript)+SIZ);
430 sprintf(&transcript[strlen(transcript)], " %s\n",
431 UserPurgeList->name);
432 purge_user(UserPurgeList->name);
433 pptr = UserPurgeList->next;
434 phree(UserPurgeList);
435 UserPurgeList = pptr;
439 if (num_users_purged > 0) aide_message(transcript);
442 lprintf(5, "Purged %d users.\n", num_users_purged);
443 return(num_users_purged);
450 * This is a really cumbersome "garbage collection" function. We have to
451 * delete visits which refer to rooms and/or users which no longer exist. In
452 * order to prevent endless traversals of the room and user files, we first
453 * build linked lists of rooms and users which _do_ exist on the system, then
454 * traverse the visit file, checking each record against those two lists and
455 * purging the ones that do not have a match on _both_ lists. (Remember, if
456 * either the room or user being referred to is no longer on the system, the
457 * record is completely useless.)
459 int PurgeVisits(void) {
460 struct cdbdata *cdbvisit;
462 struct VPurgeList *VisitPurgeList = NULL;
463 struct VPurgeList *vptr;
467 struct ValidRoom *vrptr;
468 struct ValidUser *vuptr;
469 int RoomIsValid, UserIsValid;
471 /* First, load up a table full of valid room/gen combinations */
472 ForEachRoom(AddValidRoom, NULL);
474 /* Then load up a table full of valid user numbers */
475 ForEachUser(AddValidUser, NULL);
477 /* Now traverse through the visits, purging irrelevant records... */
478 cdb_rewind(CDB_VISIT);
479 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
480 memset(&vbuf, 0, sizeof(struct visit));
481 memcpy(&vbuf, cdbvisit->ptr,
482 ( (cdbvisit->len > sizeof(struct visit)) ?
483 sizeof(struct visit) : cdbvisit->len) );
489 /* Check to see if the room exists */
490 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
491 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
492 && (vrptr->vr_roomgen==vbuf.v_roomgen))
496 /* Check to see if the user exists */
497 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
498 if (vuptr->vu_usernum == vbuf.v_usernum)
502 /* Put the record on the purge list if it's dead */
503 if ((RoomIsValid==0) || (UserIsValid==0)) {
504 vptr = (struct VPurgeList *)
505 mallok(sizeof(struct VPurgeList));
506 vptr->next = VisitPurgeList;
507 vptr->vp_roomnum = vbuf.v_roomnum;
508 vptr->vp_roomgen = vbuf.v_roomgen;
509 vptr->vp_usernum = vbuf.v_usernum;
510 VisitPurgeList = vptr;
515 /* Free the valid room/gen combination list */
516 while (ValidRoomList != NULL) {
517 vrptr = ValidRoomList->next;
518 phree(ValidRoomList);
519 ValidRoomList = vrptr;
522 /* Free the valid user list */
523 while (ValidUserList != NULL) {
524 vuptr = ValidUserList->next;
525 phree(ValidUserList);
526 ValidUserList = vuptr;
529 /* Now delete every visit on the purged list */
530 while (VisitPurgeList != NULL) {
531 IndexLen = GenerateRelationshipIndex(IndexBuf,
532 VisitPurgeList->vp_roomnum,
533 VisitPurgeList->vp_roomgen,
534 VisitPurgeList->vp_usernum);
535 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
536 vptr = VisitPurgeList->next;
537 phree(VisitPurgeList);
538 VisitPurgeList = vptr;
546 void cmd_expi(char *argbuf) {
550 if (CtdlAccessCheck(ac_aide)) return;
552 extract(cmd, argbuf, 0);
553 if (!strcasecmp(cmd, "users")) {
554 retval = PurgeUsers();
555 cprintf("%d Purged %d users.\n", OK, retval);
558 else if (!strcasecmp(cmd, "messages")) {
560 cprintf("%d Expired %d messages.\n", OK, messages_purged);
563 else if (!strcasecmp(cmd, "rooms")) {
564 retval = PurgeRooms();
565 cprintf("%d Expired %d rooms.\n", OK, retval);
568 else if (!strcasecmp(cmd, "visits")) {
569 retval = PurgeVisits();
570 cprintf("%d Purged %d visits.\n", OK, retval);
572 else if (!strcasecmp(cmd, "defrag")) {
574 cprintf("%d Defragmented the databases.\n", OK);
577 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
582 /*****************************************************************************/
585 void do_fsck_msg(long msgnum, void *userdata) {
588 ptr = (struct roomref *)mallok(sizeof(struct roomref));
590 ptr->msgnum = msgnum;
594 void do_fsck_room(struct quickroom *qrbuf, void *data)
596 getroom(&CC->quickroom, qrbuf->QRname);
597 CtdlForEachMessage(MSGS_ALL, 0L, (-127), NULL, NULL,
602 * Check message reference counts
604 void cmd_fsck(char *argbuf) {
606 struct cdbdata *cdbmsg;
607 struct SuppMsgInfo smi;
611 if (CtdlAccessCheck(ac_aide)) return;
613 /* Lame way of checking whether anyone else is doing this now */
615 cprintf("%d Another FSCK is already running.\n", ERROR);
619 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
621 cprintf("\nThis could take a while. Please be patient!\n\n");
622 cprintf("Gathering pointers...\n");
623 ForEachRoom(do_fsck_room, NULL);
626 cprintf("Checking message base...\n");
627 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
629 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
630 if (cdbmsg != NULL) {
632 cprintf("Message %7ld ", msgnum);
634 GetSuppMsgInfo(&smi, msgnum);
635 cprintf("refcount=%-2d ", smi.smi_refcount);
638 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
639 if (ptr->msgnum == msgnum) ++realcount;
641 cprintf("realcount=%-2d\n", realcount);
643 if ( (smi.smi_refcount != realcount)
644 || (realcount == 0) ) {
645 smi.smi_refcount = realcount;
646 PutSuppMsgInfo(&smi);
647 AdjRefCount(msgnum, 0); /* deletes if needed */
654 cprintf("Freeing memory...\n");
669 /*****************************************************************************/
671 char *Dynamic_Module_Init(void)
673 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
674 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");