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"
66 #include "serv_network.h"
71 struct PurgeList *next;
72 char name[ROOMNAMELEN]; /* use the larger of username or roomname */
76 struct VPurgeList *next;
83 struct ValidRoom *next;
89 struct ValidUser *next;
100 struct UPurgeList *next;
105 struct PurgeList *UserPurgeList = NULL;
106 struct PurgeList *RoomPurgeList = NULL;
107 struct ValidRoom *ValidRoomList = NULL;
108 struct ValidUser *ValidUserList = NULL;
111 struct roomref *rr = NULL;
113 extern struct CitContext *ContextList;
117 * First phase of message purge -- gather the locations of messages which
118 * qualify for purging and write them to a temp file.
120 void GatherPurgeMessages(struct quickroom *qrbuf, void *data) {
121 struct ExpirePolicy epbuf;
124 struct CtdlMessage *msg;
126 struct cdbdata *cdbfr;
127 long *msglist = NULL;
131 purgelist = (FILE *)data;
132 fprintf(purgelist, "r=%s\n", qrbuf->QRname);
135 GetExpirePolicy(&epbuf, qrbuf);
137 /* If the room is set to never expire messages ... do nothing */
138 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
139 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
141 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
144 msglist = mallok(cdbfr->len);
145 memcpy(msglist, cdbfr->ptr, cdbfr->len);
146 num_msgs = cdbfr->len / sizeof(long);
150 /* Nothing to do if there aren't any messages */
152 if (msglist != NULL) phree(msglist);
156 /* If the room is set to expire by count, do that */
157 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
158 if (num_msgs > epbuf.expire_value) {
159 for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
160 fprintf(purgelist, "m=%ld\n", msglist[a]);
166 /* If the room is set to expire by age... */
167 if (epbuf.expire_mode == EXPIRE_AGE) {
168 for (a=0; a<num_msgs; ++a) {
171 msg = CtdlFetchMessage(delnum);
173 xtime = atol(msg->cm_fields['T']);
174 CtdlFreeMessage(msg);
180 && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
181 fprintf(purgelist, "m=%ld\n", delnum);
187 if (msglist != NULL) phree(msglist);
192 * Second phase of message purge -- read list of msgs from temp file and
195 void DoPurgeMessages(FILE *purgelist) {
196 char roomname[ROOMNAMELEN];
201 strcpy(roomname, "nonexistent room ___ ___");
202 while (fgets(buf, sizeof buf, purgelist) != NULL) {
203 buf[strlen(buf)-1]=0;
204 if (!strncasecmp(buf, "r=", 2)) {
205 strcpy(roomname, &buf[2]);
207 if (!strncasecmp(buf, "m=", 2)) {
208 msgnum = atol(&buf[2]);
210 CtdlDeleteMessages(roomname, msgnum, "");
217 void PurgeMessages(void) {
220 lprintf(5, "PurgeMessages() called\n");
223 purgelist = tmpfile();
224 if (purgelist == NULL) {
225 lprintf(3, "Can't create purgelist temp file: %s\n",
230 ForEachRoom(GatherPurgeMessages, (void *)purgelist );
231 DoPurgeMessages(purgelist);
236 void AddValidUser(struct usersupp *usbuf, void *data) {
237 struct ValidUser *vuptr;
239 vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
240 vuptr->next = ValidUserList;
241 vuptr->vu_usernum = usbuf->usernum;
242 ValidUserList = vuptr;
245 void AddValidRoom(struct quickroom *qrbuf, void *data) {
246 struct ValidRoom *vrptr;
248 vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
249 vrptr->next = ValidRoomList;
250 vrptr->vr_roomnum = qrbuf->QRnumber;
251 vrptr->vr_roomgen = qrbuf->QRgen;
252 ValidRoomList = vrptr;
255 void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
256 time_t age, purge_secs;
257 struct PurgeList *pptr;
258 struct ValidUser *vuptr;
261 /* For mailbox rooms, there's only one purging rule: if the user who
262 * owns the room still exists, we keep the room; otherwise, we purge
263 * it. Bypass any other rules.
265 if (qrbuf->QRflags & QR_MAILBOX) {
266 /* if user not found, do_purge will be 1 */
268 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
269 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
275 /* Any of these attributes render a room non-purgable */
276 if (qrbuf->QRflags & QR_PERMANENT) return;
277 if (qrbuf->QRflags & QR_DIRECTORY) return;
278 if (qrbuf->QRflags & QR_NETWORK) return;
279 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
280 if (is_noneditable(qrbuf)) return;
282 /* If we don't know the modification date, be safe and don't purge */
283 if (qrbuf->QRmtime <= (time_t)0) return;
285 /* If no room purge time is set, be safe and don't purge */
286 if (config.c_roompurge < 0) return;
288 /* Otherwise, check the date of last modification */
289 age = time(NULL) - (qrbuf->QRmtime);
290 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
291 if (purge_secs <= (time_t)0) return;
292 lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
293 if (age > purge_secs) do_purge = 1;
297 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
298 pptr->next = RoomPurgeList;
299 strcpy(pptr->name, qrbuf->QRname);
300 RoomPurgeList = pptr;
307 int PurgeRooms(void) {
308 struct PurgeList *pptr;
309 int num_rooms_purged = 0;
310 struct quickroom qrbuf;
311 struct ValidUser *vuptr;
312 char *transcript = NULL;
314 lprintf(5, "PurgeRooms() called\n");
317 /* Load up a table full of valid user numbers so we can delete
318 * user-owned rooms for users who no longer exist */
319 ForEachUser(AddValidUser, NULL);
321 /* Then cycle through the room file */
322 ForEachRoom(DoPurgeRooms, NULL);
324 /* Free the valid user list */
325 while (ValidUserList != NULL) {
326 vuptr = ValidUserList->next;
327 phree(ValidUserList);
328 ValidUserList = vuptr;
332 transcript = mallok(SIZ);
333 strcpy(transcript, "The following rooms have been auto-purged:\n");
335 while (RoomPurgeList != NULL) {
336 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
337 transcript=reallok(transcript, strlen(transcript)+SIZ);
338 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
342 pptr = RoomPurgeList->next;
343 phree(RoomPurgeList);
344 RoomPurgeList = pptr;
348 if (num_rooms_purged > 0) aide_message(transcript);
351 lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
352 return(num_rooms_purged);
356 void do_user_purge(struct usersupp *us, void *data) {
360 struct PurgeList *pptr;
362 /* stupid recovery routine to re-create missing mailboxen.
364 struct quickroom qrbuf;
365 char mailboxname[ROOMNAMELEN];
366 MailboxName(mailboxname, us, MAILROOM);
367 create_room(mailboxname, 4, "", 0, 1, 1);
368 if (getroom(&qrbuf, mailboxname) != 0) return;
369 lprintf(9, "Got %s\n", qrbuf.QRname);
373 /* Set purge time; if the user overrides the system default, use it */
374 if (us->USuserpurge > 0) {
375 purge_time = ((time_t)us->USuserpurge) * 86400L;
378 purge_time = ((time_t)config.c_userpurge) * 86400L;
381 /* The default rule is to not purge. */
384 /* If the user hasn't called in two months, his/her account
385 * has expired, so purge the record.
388 if ((now - us->lastcall) > purge_time) purge = 1;
390 /* If the user set his/her password to 'deleteme', he/she
391 * wishes to be deleted, so purge the record.
393 if (!strcasecmp(us->password, "deleteme")) purge = 1;
395 /* If the record is marked as permanent, don't purge it.
397 if (us->flags & US_PERM) purge = 0;
399 /* If the access level is 0, the record should already have been
400 * deleted, but maybe the user was logged in at the time or something.
401 * Delete the record now.
403 if (us->axlevel == 0) purge = 1;
405 /* 0 calls is impossible. If there are 0 calls, it must
406 * be a corrupted record, so purge it.
408 if (us->timescalled == 0) purge = 1;
411 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
412 pptr->next = UserPurgeList;
413 strcpy(pptr->name, us->fullname);
414 UserPurgeList = pptr;
421 int PurgeUsers(void) {
422 struct PurgeList *pptr;
423 int num_users_purged = 0;
424 char *transcript = NULL;
426 lprintf(5, "PurgeUsers() called\n");
427 if (config.c_userpurge > 0) {
428 ForEachUser(do_user_purge, NULL);
431 transcript = mallok(SIZ);
432 strcpy(transcript, "The following users have been auto-purged:\n");
434 while (UserPurgeList != NULL) {
435 transcript=reallok(transcript, strlen(transcript)+SIZ);
436 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
437 UserPurgeList->name);
438 purge_user(UserPurgeList->name);
439 pptr = UserPurgeList->next;
440 phree(UserPurgeList);
441 UserPurgeList = pptr;
445 if (num_users_purged > 0) aide_message(transcript);
448 lprintf(5, "Purged %d users.\n", num_users_purged);
449 return(num_users_purged);
456 * This is a really cumbersome "garbage collection" function. We have to
457 * delete visits which refer to rooms and/or users which no longer exist. In
458 * order to prevent endless traversals of the room and user files, we first
459 * build linked lists of rooms and users which _do_ exist on the system, then
460 * traverse the visit file, checking each record against those two lists and
461 * purging the ones that do not have a match on _both_ lists. (Remember, if
462 * either the room or user being referred to is no longer on the system, the
463 * record is completely useless.)
465 int PurgeVisits(void) {
466 struct cdbdata *cdbvisit;
468 struct VPurgeList *VisitPurgeList = NULL;
469 struct VPurgeList *vptr;
473 struct ValidRoom *vrptr;
474 struct ValidUser *vuptr;
475 int RoomIsValid, UserIsValid;
477 /* First, load up a table full of valid room/gen combinations */
478 ForEachRoom(AddValidRoom, NULL);
480 /* Then load up a table full of valid user numbers */
481 ForEachUser(AddValidUser, NULL);
483 /* Now traverse through the visits, purging irrelevant records... */
484 cdb_rewind(CDB_VISIT);
485 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
486 memset(&vbuf, 0, sizeof(struct visit));
487 memcpy(&vbuf, cdbvisit->ptr,
488 ( (cdbvisit->len > sizeof(struct visit)) ?
489 sizeof(struct visit) : cdbvisit->len) );
495 /* Check to see if the room exists */
496 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
497 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
498 && (vrptr->vr_roomgen==vbuf.v_roomgen))
502 /* Check to see if the user exists */
503 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
504 if (vuptr->vu_usernum == vbuf.v_usernum)
508 /* Put the record on the purge list if it's dead */
509 if ((RoomIsValid==0) || (UserIsValid==0)) {
510 vptr = (struct VPurgeList *)
511 mallok(sizeof(struct VPurgeList));
512 vptr->next = VisitPurgeList;
513 vptr->vp_roomnum = vbuf.v_roomnum;
514 vptr->vp_roomgen = vbuf.v_roomgen;
515 vptr->vp_usernum = vbuf.v_usernum;
516 VisitPurgeList = vptr;
521 /* Free the valid room/gen combination list */
522 while (ValidRoomList != NULL) {
523 vrptr = ValidRoomList->next;
524 phree(ValidRoomList);
525 ValidRoomList = vrptr;
528 /* Free the valid user list */
529 while (ValidUserList != NULL) {
530 vuptr = ValidUserList->next;
531 phree(ValidUserList);
532 ValidUserList = vuptr;
535 /* Now delete every visit on the purged list */
536 while (VisitPurgeList != NULL) {
537 IndexLen = GenerateRelationshipIndex(IndexBuf,
538 VisitPurgeList->vp_roomnum,
539 VisitPurgeList->vp_roomgen,
540 VisitPurgeList->vp_usernum);
541 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
542 vptr = VisitPurgeList->next;
543 phree(VisitPurgeList);
544 VisitPurgeList = vptr;
552 * Purge the use table of old entries.
555 int PurgeUseTable(void) {
557 struct cdbdata *cdbut;
559 struct UPurgeList *ul = NULL;
560 struct UPurgeList *uptr;
562 /* Phase 1: traverse through the table, discovering old records... */
563 lprintf(9, "Purge use table: phase 1\n");
564 cdb_rewind(CDB_USETABLE);
565 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
567 memcpy(&ut, cdbut->ptr,
568 ((cdbut->len > sizeof(struct UseTable)) ?
569 sizeof(struct UseTable) : cdbut->len));
572 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
573 uptr = (struct UPurgeList *) mallok(sizeof(struct UPurgeList));
576 safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
584 /* Phase 2: delete the records */
585 lprintf(9, "Purge use table: phase 2\n");
587 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
593 lprintf(9, "Purge use table: finished (purged %d records)\n", purged);
598 void cmd_expi(char *argbuf) {
602 if (CtdlAccessCheck(ac_aide)) return;
604 extract(cmd, argbuf, 0);
605 if (!strcasecmp(cmd, "users")) {
606 retval = PurgeUsers();
607 cprintf("%d Purged %d users.\n", CIT_OK, retval);
610 else if (!strcasecmp(cmd, "messages")) {
612 cprintf("%d Expired %d messages.\n", CIT_OK, messages_purged);
615 else if (!strcasecmp(cmd, "rooms")) {
616 retval = PurgeRooms();
617 cprintf("%d Expired %d rooms.\n", CIT_OK, retval);
620 else if (!strcasecmp(cmd, "visits")) {
621 retval = PurgeVisits();
622 cprintf("%d Purged %d visits.\n", CIT_OK, retval);
624 else if (!strcasecmp(cmd, "usetable")) {
625 retval = PurgeUseTable();
626 cprintf("%d Purged %d entries from the use table.\n",
629 else if (!strcasecmp(cmd, "defrag")) {
631 cprintf("%d Defragmented the databases.\n", CIT_OK);
634 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
639 /*****************************************************************************/
642 void do_fsck_msg(long msgnum, void *userdata) {
645 ptr = (struct roomref *)mallok(sizeof(struct roomref));
647 ptr->msgnum = msgnum;
651 void do_fsck_room(struct quickroom *qrbuf, void *data)
653 getroom(&CC->quickroom, qrbuf->QRname);
654 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
658 * Check message reference counts
660 void cmd_fsck(char *argbuf) {
662 struct cdbdata *cdbmsg;
667 if (CtdlAccessCheck(ac_aide)) return;
669 /* Lame way of checking whether anyone else is doing this now */
671 cprintf("%d Another FSCK is already running.\n", ERROR);
675 cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
677 cprintf("\nThis could take a while. Please be patient!\n\n");
678 cprintf("Gathering pointers...\n");
679 ForEachRoom(do_fsck_room, NULL);
682 cprintf("Checking message base...\n");
683 for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
685 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
686 if (cdbmsg != NULL) {
688 cprintf("Message %7ld ", msgnum);
690 GetMetaData(&smi, msgnum);
691 cprintf("refcount=%-2d ", smi.meta_refcount);
694 for (ptr = rr; ptr != NULL; ptr = ptr->next) {
695 if (ptr->msgnum == msgnum) ++realcount;
697 cprintf("realcount=%-2d\n", realcount);
699 if ( (smi.meta_refcount != realcount)
700 || (realcount == 0) ) {
701 smi.meta_refcount = realcount;
703 AdjRefCount(msgnum, 0); /* deletes if needed */
710 cprintf("Freeing memory...\n");
725 /*****************************************************************************/
727 char *serv_expire_init(void)
729 CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
730 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");