Now we name all the private contexts.
[citadel.git] / citadel / modules / expire / serv_expire.c
1 /*
2  * $Id$
3  *
4  * This module handles the expiry of old messages and the purging of old users.
5  *
6  */
7
8
9 /*
10  * A brief technical discussion:
11  *
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.
15  *
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.
22  *
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.
25  */
26
27
28 #include "sysdep.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <pwd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 #  include <sys/time.h>
44 # else
45 #  include <time.h>
46 # endif
47 #endif
48
49 #include <sys/wait.h>
50 #include <string.h>
51 #include <limits.h>
52 #include <libcitadel.h>
53 #include "citadel.h"
54 #include "server.h"
55 #include "citserver.h"
56 #include "support.h"
57 #include "config.h"
58 #include "room_ops.h"
59 #include "policy.h"
60 #include "database.h"
61 #include "msgbase.h"
62 #include "user_ops.h"
63 #include "control.h"
64 #include "serv_network.h"       /* Needed for defenition of UseTable */
65 #include "threads.h"
66
67 #include "ctdl_module.h"
68
69
70 struct PurgeList {
71         struct PurgeList *next;
72         char name[ROOMNAMELEN]; /* use the larger of username or roomname */
73 };
74
75 struct VPurgeList {
76         struct VPurgeList *next;
77         long vp_roomnum;
78         long vp_roomgen;
79         long vp_usernum;
80 };
81
82 struct ValidRoom {
83         struct ValidRoom *next;
84         long vr_roomnum;
85         long vr_roomgen;
86 };
87
88 struct ValidUser {
89         struct ValidUser *next;
90         long vu_usernum;
91 };
92
93
94 struct ctdlroomref {
95         struct ctdlroomref *next;
96         long msgnum;
97 };
98
99 struct UPurgeList {
100         struct UPurgeList *next;
101         char up_key[256];
102 };
103
104 struct EPurgeList {
105         struct EPurgeList *next;
106         int ep_keylen;
107         char *ep_key;
108 };
109
110
111 struct PurgeList *UserPurgeList = NULL;
112 struct PurgeList *RoomPurgeList = NULL;
113 struct ValidRoom *ValidRoomList = NULL;
114 struct ValidUser *ValidUserList = NULL;
115 int messages_purged;
116 int users_not_purged;
117 char *users_corrupt_msg = NULL;
118 char *users_zero_msg = NULL;
119
120 struct ctdlroomref *rr = NULL;
121
122 extern struct CitContext *ContextList;
123
124
125 /*
126  * First phase of message purge -- gather the locations of messages which
127  * qualify for purging and write them to a temp file.
128  */
129 void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
130         struct ExpirePolicy epbuf;
131         long delnum;
132         time_t xtime, now;
133         struct CtdlMessage *msg = NULL;
134         int a;
135         struct cdbdata *cdbfr;
136         long *msglist = NULL;
137         int num_msgs = 0;
138         FILE *purgelist;
139
140         purgelist = (FILE *)data;
141         fprintf(purgelist, "r=%s\n", qrbuf->QRname);
142
143         time(&now);
144         GetExpirePolicy(&epbuf, qrbuf);
145
146         /* If the room is set to never expire messages ... do nothing */
147         if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
148         if (epbuf.expire_mode == EXPIRE_MANUAL) return;
149
150         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
151
152         if (cdbfr != NULL) {
153                 msglist = malloc(cdbfr->len);
154                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
155                 num_msgs = cdbfr->len / sizeof(long);
156                 cdb_free(cdbfr);
157         }
158
159         /* Nothing to do if there aren't any messages */
160         if (num_msgs == 0) {
161                 if (msglist != NULL) free(msglist);
162                 return;
163         }
164
165         /* If the room is set to expire by count, do that */
166         if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
167                 if (num_msgs > epbuf.expire_value) {
168                         for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
169                                 fprintf(purgelist, "m=%ld\n", msglist[a]);
170                                 ++messages_purged;
171                         }
172                 }
173         }
174
175         /* If the room is set to expire by age... */
176         if (epbuf.expire_mode == EXPIRE_AGE) {
177                 for (a=0; a<num_msgs; ++a) {
178                         delnum = msglist[a];
179
180                         msg = CtdlFetchMessage(delnum, 0); /* dont need body */
181                         if (msg != NULL) {
182                                 xtime = atol(msg->cm_fields['T']);
183                                 CtdlFreeMessage(msg);
184                         } else {
185                                 xtime = 0L;
186                         }
187
188                         if ((xtime > 0L)
189                            && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
190                                 fprintf(purgelist, "m=%ld\n", delnum);
191                                 ++messages_purged;
192                         }
193                 }
194         }
195
196         if (msglist != NULL) free(msglist);
197 }
198
199
200 /*
201  * Second phase of message purge -- read list of msgs from temp file and
202  * delete them.
203  */
204 void DoPurgeMessages(FILE *purgelist) {
205         char roomname[ROOMNAMELEN];
206         long msgnum;
207         char buf[SIZ];
208
209         rewind(purgelist);
210         strcpy(roomname, "nonexistent room ___ ___");
211         while (fgets(buf, sizeof buf, purgelist) != NULL) {
212                 buf[strlen(buf)-1]=0;
213                 if (!strncasecmp(buf, "r=", 2)) {
214                         strcpy(roomname, &buf[2]);
215                 }
216                 if (!strncasecmp(buf, "m=", 2)) {
217                         msgnum = atol(&buf[2]);
218                         if (msgnum > 0L) {
219                                 CtdlDeleteMessages(roomname, &msgnum, 1, "");
220                         }
221                 }
222         }
223 }
224
225
226 void PurgeMessages(void) {
227         FILE *purgelist;
228
229         CtdlLogPrintf(CTDL_DEBUG, "PurgeMessages() called\n");
230         messages_purged = 0;
231
232         purgelist = tmpfile();
233         if (purgelist == NULL) {
234                 CtdlLogPrintf(CTDL_CRIT, "Can't create purgelist temp file: %s\n",
235                         strerror(errno));
236                 return;
237         }
238
239         ForEachRoom(GatherPurgeMessages, (void *)purgelist );
240         DoPurgeMessages(purgelist);
241         fclose(purgelist);
242 }
243
244
245 void AddValidUser(struct ctdluser *usbuf, void *data) {
246         struct ValidUser *vuptr;
247
248         vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
249         vuptr->next = ValidUserList;
250         vuptr->vu_usernum = usbuf->usernum;
251         ValidUserList = vuptr;
252 }
253
254 void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
255         struct ValidRoom *vrptr;
256
257         vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
258         vrptr->next = ValidRoomList;
259         vrptr->vr_roomnum = qrbuf->QRnumber;
260         vrptr->vr_roomgen = qrbuf->QRgen;
261         ValidRoomList = vrptr;
262 }
263
264 void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
265         time_t age, purge_secs;
266         struct PurgeList *pptr;
267         struct ValidUser *vuptr;
268         int do_purge = 0;
269
270         /* For mailbox rooms, there's only one purging rule: if the user who
271          * owns the room still exists, we keep the room; otherwise, we purge
272          * it.  Bypass any other rules.
273          */
274         if (qrbuf->QRflags & QR_MAILBOX) {
275                 /* if user not found, do_purge will be 1 */
276                 do_purge = 1;
277                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
278                         if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
279                                 do_purge = 0;
280                         }
281                 }
282         }
283         else {
284                 /* Any of these attributes render a room non-purgable */
285                 if (qrbuf->QRflags & QR_PERMANENT) return;
286                 if (qrbuf->QRflags & QR_DIRECTORY) return;
287                 if (qrbuf->QRflags & QR_NETWORK) return;
288                 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
289                 if (is_noneditable(qrbuf)) return;
290
291                 /* If we don't know the modification date, be safe and don't purge */
292                 if (qrbuf->QRmtime <= (time_t)0) return;
293
294                 /* If no room purge time is set, be safe and don't purge */
295                 if (config.c_roompurge < 0) return;
296
297                 /* Otherwise, check the date of last modification */
298                 age = time(NULL) - (qrbuf->QRmtime);
299                 purge_secs = (time_t)config.c_roompurge * (time_t)86400;
300                 if (purge_secs <= (time_t)0) return;
301                 CtdlLogPrintf(CTDL_DEBUG, "<%s> is <%ld> seconds old\n", qrbuf->QRname, (long)age);
302                 if (age > purge_secs) do_purge = 1;
303         } /* !QR_MAILBOX */
304
305         if (do_purge) {
306                 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
307                 pptr->next = RoomPurgeList;
308                 strcpy(pptr->name, qrbuf->QRname);
309                 RoomPurgeList = pptr;
310         }
311
312 }
313
314
315
316 int PurgeRooms(void) {
317         struct PurgeList *pptr;
318         int num_rooms_purged = 0;
319         struct ctdlroom qrbuf;
320         struct ValidUser *vuptr;
321         char *transcript = NULL;
322
323         CtdlLogPrintf(CTDL_DEBUG, "PurgeRooms() called\n");
324
325
326         /* Load up a table full of valid user numbers so we can delete
327          * user-owned rooms for users who no longer exist */
328         ForEachUser(AddValidUser, NULL);
329
330         /* Then cycle through the room file */
331         ForEachRoom(DoPurgeRooms, NULL);
332
333         /* Free the valid user list */
334         while (ValidUserList != NULL) {
335                 vuptr = ValidUserList->next;
336                 free(ValidUserList);
337                 ValidUserList = vuptr;
338         }
339
340
341         transcript = malloc(SIZ);
342         strcpy(transcript, "The following rooms have been auto-purged:\n");
343
344         while (RoomPurgeList != NULL) {
345                 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
346                         transcript=realloc(transcript, strlen(transcript)+SIZ);
347                         snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
348                                 qrbuf.QRname);
349                         delete_room(&qrbuf);
350                 }
351                 pptr = RoomPurgeList->next;
352                 free(RoomPurgeList);
353                 RoomPurgeList = pptr;
354                 ++num_rooms_purged;
355         }
356
357         if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
358         free(transcript);
359
360         CtdlLogPrintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
361         return(num_rooms_purged);
362 }
363
364
365 /*
366  * Back end function to check user accounts for associated Unix accounts
367  * which no longer exist.  (Only relevant for host auth mode.)
368  */
369 void do_uid_user_purge(struct ctdluser *us, void *data) {
370         struct PurgeList *pptr;
371
372         if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
373                 if (getpwuid(us->uid) == NULL) {
374                         pptr = (struct PurgeList *)
375                                 malloc(sizeof(struct PurgeList));
376                         pptr->next = UserPurgeList;
377                         strcpy(pptr->name, us->fullname);
378                         UserPurgeList = pptr;
379                 }
380         }
381         else {
382                 ++users_not_purged;
383         }
384 }
385
386
387
388
389 /*
390  * Back end function to check user accounts for expiration.
391  */
392 void do_user_purge(struct ctdluser *us, void *data) {
393         int purge;
394         time_t now;
395         time_t purge_time;
396         struct PurgeList *pptr;
397
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;
401         }
402         else {
403                 purge_time = ((time_t)config.c_userpurge) * 86400L;
404         }
405
406         /* The default rule is to not purge. */
407         purge = 0;
408
409         /* If the user hasn't called in two months and expiring of accounts is turned on, his/her account
410          * has expired, so purge the record.
411          */
412         if (config.c_userpurge > 0)
413         {
414                 now = time(NULL);
415                 if ((now - us->lastcall) > purge_time) purge = 1;
416         }
417
418         /* If the record is marked as permanent, don't purge it.
419          */
420         if (us->flags & US_PERM) purge = 0;
421
422         /* If the user is an Aide, don't purge him/her/it.
423          */
424         if (us->axlevel == 6) purge = 0;
425
426         /* If the access level is 0, the record should already have been
427          * deleted, but maybe the user was logged in at the time or something.
428          * Delete the record now.
429          */
430         if (us->axlevel == 0) purge = 1;
431
432         /* If the user set his/her password to 'deleteme', he/she
433          * wishes to be deleted, so purge the record.
434          * Moved this lower down so that aides and permanent users get purged if they ask to be.
435          */
436         if (!strcasecmp(us->password, "deleteme")) purge = 1;
437         
438         /* 0 calls is impossible.  If there are 0 calls, it must
439          * be a corrupted record, so purge it.
440          * Actually it is possible if an Aide created the user so now we check for less than 0 (DRW)
441          */
442         if (us->timescalled < 0) purge = 1;
443
444         /* User number 0, as well as any negative user number, is
445          * also impossible.
446          */
447         if (us->usernum < 0L) purge = 1;
448         
449         /** Don't purge user 0. That user is there for the system */
450         if (us->usernum == 0) purge = 0;
451         
452         /* If the user has no full name entry then we can't purge them
453          * since the actual purge can't find them.
454          * This shouldn't happen but does somehow.
455          * So we make an Aide message to alert to it but don't add it to the purge list
456          */
457         if (IsEmptyStr(us->fullname))
458         {
459                 if (us->usernum > 0L)
460                 {
461                         purge=0;
462                         if (users_corrupt_msg == NULL)
463                         {
464                                 users_corrupt_msg = malloc(SIZ);
465                                 strcpy(users_corrupt_msg, "The auto-purger found the following user numbers with no name.\n"
466                                 "If the user number is 0 you should report this to the Citadel development\n"
467                                 "team either by a bugzilla report at http://bugzilla.citadel.org or\n"
468                                 "posting a message in the Citadel Support room on Uncensored at\n"
469                                 "https://uncensored.citadel.org You should make it clear that you have seen a\n"
470                                 "user 0 messages in the Aide room which means a module has not named its\n"
471                                 "private context.\n"
472                                 "Unfortunately the auto-purger is not yet able to fix this problem.\n"
473                                 "This problem is not considered serious since a user with no name can\n"
474                                 "not log in.\n");
475                         }
476                 
477                         users_corrupt_msg=realloc(users_corrupt_msg, strlen(users_corrupt_msg)+SIZ);
478                         snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], SIZ, " %ld\n", us->usernum);
479                 }
480                 else if (us->usernum == 0L)
481                 {
482                         purge=0;
483                         if (users_zero_msg == NULL)
484                         {
485                                 users_zero_msg = malloc(SIZ);
486                                 strcpy(users_zero_msg, "The auto-purger found a user with a user number of 0 but no name.\n"
487                                 "This is the result of a bug where a private contaxt has been created but\n"
488                                 "not named.\n\n"
489                                 "Please report this to the Citadel development team either by a bugzilla\n"
490                                 "report at http://bugzilla.citadel.org or by posting a message in the\n"
491                                 "Citadel Support room on Uncensored at https://uncensored.citadel.org\n"
492                                 "You should make it clear that you have seen a user 0 messages in the\n"
493                                 "Aide room which means a module has not named its private context.\n\n"
494                                 "This problem is not considered serious since it does not constitute a\n"
495                                 "security risk and should not impare system operation.\n"
496                                 );
497                         }
498                 }
499
500         }
501
502
503         if (purge == 1) {
504                 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
505                 pptr->next = UserPurgeList;
506                 strcpy(pptr->name, us->fullname);
507                 UserPurgeList = pptr;
508         }
509         else {
510                 ++users_not_purged;
511         }
512
513 }
514
515
516
517 int PurgeUsers(void) {
518         struct PurgeList *pptr;
519         int num_users_purged = 0;
520         char *transcript = NULL;
521
522         CtdlLogPrintf(CTDL_DEBUG, "PurgeUsers() called\n");
523         users_not_purged = 0;
524
525         switch(config.c_auth_mode) {
526                 case AUTHMODE_NATIVE:
527                         ForEachUser(do_user_purge, NULL);
528                         break;
529                 case AUTHMODE_HOST:
530                         ForEachUser(do_uid_user_purge, NULL);
531                         break;
532                 default:
533                         CtdlLogPrintf(CTDL_DEBUG, "Unknown authentication mode!\n");
534                         break;
535         }
536
537         transcript = malloc(SIZ);
538
539         if (users_not_purged == 0) {
540                 strcpy(transcript, "The auto-purger was told to purge every user.  It is\n"
541                                 "refusing to do this because it usually indicates a problem\n"
542                                 "such as an inability to communicate with a name service.\n"
543                 );
544                 while (UserPurgeList != NULL) {
545                         pptr = UserPurgeList->next;
546                         free(UserPurgeList);
547                         UserPurgeList = pptr;
548                         ++num_users_purged;
549                 }
550         }
551
552         else {
553                 strcpy(transcript, "The following users have been auto-purged:\n");
554                 while (UserPurgeList != NULL) {
555                         transcript=realloc(transcript, strlen(transcript)+SIZ);
556                         snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
557                                 UserPurgeList->name);
558                         purge_user(UserPurgeList->name);
559                         pptr = UserPurgeList->next;
560                         free(UserPurgeList);
561                         UserPurgeList = pptr;
562                         ++num_users_purged;
563                 }
564         }
565
566         if (num_users_purged > 0) aide_message(transcript, "User Purge Message");
567         free(transcript);
568
569         if(users_corrupt_msg)
570         {
571                 aide_message(users_corrupt_msg, "User Corruption Message");
572                 free (users_corrupt_msg);
573                 users_corrupt_msg = NULL;
574         }
575         
576         if(users_zero_msg)
577         {
578                 aide_message(users_zero_msg, "User Zero Message");
579                 free (users_zero_msg);
580                 users_zero_msg = NULL;
581         }
582                 
583         CtdlLogPrintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
584         return(num_users_purged);
585 }
586
587
588 /*
589  * Purge visits
590  *
591  * This is a really cumbersome "garbage collection" function.  We have to
592  * delete visits which refer to rooms and/or users which no longer exist.  In
593  * order to prevent endless traversals of the room and user files, we first
594  * build linked lists of rooms and users which _do_ exist on the system, then
595  * traverse the visit file, checking each record against those two lists and
596  * purging the ones that do not have a match on _both_ lists.  (Remember, if
597  * either the room or user being referred to is no longer on the system, the
598  * record is completely useless.)
599  */
600 int PurgeVisits(void) {
601         struct cdbdata *cdbvisit;
602         struct visit vbuf;
603         struct VPurgeList *VisitPurgeList = NULL;
604         struct VPurgeList *vptr;
605         int purged = 0;
606         char IndexBuf[32];
607         int IndexLen;
608         struct ValidRoom *vrptr;
609         struct ValidUser *vuptr;
610         int RoomIsValid, UserIsValid;
611
612         /* First, load up a table full of valid room/gen combinations */
613         ForEachRoom(AddValidRoom, NULL);
614
615         /* Then load up a table full of valid user numbers */
616         ForEachUser(AddValidUser, NULL);
617
618         /* Now traverse through the visits, purging irrelevant records... */
619         cdb_rewind(CDB_VISIT);
620         while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
621                 memset(&vbuf, 0, sizeof(struct visit));
622                 memcpy(&vbuf, cdbvisit->ptr,
623                         ( (cdbvisit->len > sizeof(struct visit)) ?
624                         sizeof(struct visit) : cdbvisit->len) );
625                 cdb_free(cdbvisit);
626
627                 RoomIsValid = 0;
628                 UserIsValid = 0;
629
630                 /* Check to see if the room exists */
631                 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
632                         if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
633                              && (vrptr->vr_roomgen==vbuf.v_roomgen))
634                                 RoomIsValid = 1;
635                 }
636
637                 /* Check to see if the user exists */
638                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
639                         if (vuptr->vu_usernum == vbuf.v_usernum)
640                                 UserIsValid = 1;
641                 }
642
643                 /* Put the record on the purge list if it's dead */
644                 if ((RoomIsValid==0) || (UserIsValid==0)) {
645                         vptr = (struct VPurgeList *)
646                                 malloc(sizeof(struct VPurgeList));
647                         vptr->next = VisitPurgeList;
648                         vptr->vp_roomnum = vbuf.v_roomnum;
649                         vptr->vp_roomgen = vbuf.v_roomgen;
650                         vptr->vp_usernum = vbuf.v_usernum;
651                         VisitPurgeList = vptr;
652                 }
653
654         }
655
656         /* Free the valid room/gen combination list */
657         while (ValidRoomList != NULL) {
658                 vrptr = ValidRoomList->next;
659                 free(ValidRoomList);
660                 ValidRoomList = vrptr;
661         }
662
663         /* Free the valid user list */
664         while (ValidUserList != NULL) {
665                 vuptr = ValidUserList->next;
666                 free(ValidUserList);
667                 ValidUserList = vuptr;
668         }
669
670         /* Now delete every visit on the purged list */
671         while (VisitPurgeList != NULL) {
672                 IndexLen = GenerateRelationshipIndex(IndexBuf,
673                                 VisitPurgeList->vp_roomnum,
674                                 VisitPurgeList->vp_roomgen,
675                                 VisitPurgeList->vp_usernum);
676                 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
677                 vptr = VisitPurgeList->next;
678                 free(VisitPurgeList);
679                 VisitPurgeList = vptr;
680                 ++purged;
681         }
682
683         return(purged);
684 }
685
686 /*
687  * Purge the use table of old entries.
688  *
689  */
690 int PurgeUseTable(void) {
691         int purged = 0;
692         struct cdbdata *cdbut;
693         struct UseTable ut;
694         struct UPurgeList *ul = NULL;
695         struct UPurgeList *uptr; 
696
697         /* Phase 1: traverse through the table, discovering old records... */
698         CtdlLogPrintf(CTDL_DEBUG, "Purge use table: phase 1\n");
699         cdb_rewind(CDB_USETABLE);
700         while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
701
702         /*
703          * TODODRW: change this to create a new function time_t cdb_get_timestamp( struct cdbdata *)
704          * this will release this file from the serv_network.h
705          * Maybe it could be a macro that extracts and casts the reult
706          */
707                 memcpy(&ut, cdbut->ptr,
708                        ((cdbut->len > sizeof(struct UseTable)) ?
709                         sizeof(struct UseTable) : cdbut->len));
710                 cdb_free(cdbut);
711
712                 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
713                         uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
714                         if (uptr != NULL) {
715                                 uptr->next = ul;
716                                 safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
717                                 ul = uptr;
718                         }
719                         ++purged;
720                 }
721
722         }
723
724         /* Phase 2: delete the records */
725         CtdlLogPrintf(CTDL_DEBUG, "Purge use table: phase 2\n");
726         while (ul != NULL) {
727                 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
728                 uptr = ul->next;
729                 free(ul);
730                 ul = uptr;
731         }
732
733         CtdlLogPrintf(CTDL_DEBUG, "Purge use table: finished (purged %d records)\n", purged);
734         return(purged);
735 }
736
737
738
739 /*
740  * Purge the EUID Index of old records.
741  *
742  */
743 int PurgeEuidIndexTable(void) {
744         int purged = 0;
745         struct cdbdata *cdbei;
746         struct EPurgeList *el = NULL;
747         struct EPurgeList *eptr; 
748         long msgnum;
749         struct CtdlMessage *msg = NULL;
750
751         /* Phase 1: traverse through the table, discovering old records... */
752         CtdlLogPrintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
753         cdb_rewind(CDB_EUIDINDEX);
754         while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
755
756                 memcpy(&msgnum, cdbei->ptr, sizeof(long));
757
758                 msg = CtdlFetchMessage(msgnum, 0);
759                 if (msg != NULL) {
760                         CtdlFreeMessage(msg);   /* it still exists, so do nothing */
761                 }
762                 else {
763                         eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
764                         if (eptr != NULL) {
765                                 eptr->next = el;
766                                 eptr->ep_keylen = cdbei->len - sizeof(long);
767                                 eptr->ep_key = malloc(cdbei->len);
768                                 memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
769                                 el = eptr;
770                         }
771                         ++purged;
772                 }
773
774                 cdb_free(cdbei);
775
776         }
777
778         /* Phase 2: delete the records */
779         CtdlLogPrintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
780         while (el != NULL) {
781                 cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
782                 free(el->ep_key);
783                 eptr = el->next;
784                 free(el);
785                 el = eptr;
786         }
787
788         CtdlLogPrintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
789         return(purged);
790 }
791
792
793 void *purge_databases(void *args)
794 {
795         int retval;
796         static time_t last_purge = 0;
797         time_t now;
798         struct tm tm;
799         struct CitContext purgerCC;
800
801         CtdlLogPrintf(CTDL_DEBUG, "Auto-purger_thread() initializing\n");
802
803         CtdlFillPrivateContext(&purgerCC, "purger");
804         citthread_setspecific(MyConKey, (void *)&purgerCC );
805
806         while (!CtdlThreadCheckStop()) {
807                 /* Do the auto-purge if the current hour equals the purge hour,
808                  * but not if the operation has already been performed in the
809                  * last twelve hours.  This is usually enough granularity.
810                  */
811                 now = time(NULL);
812                 localtime_r(&now, &tm);
813                 if ((tm.tm_hour != config.c_purge_hour) || ((now - last_purge) < 43200)) {
814                         CtdlThreadSleep(60);
815                         continue;
816                 }
817
818
819                 CtdlLogPrintf(CTDL_INFO, "Auto-purger: starting.\n");
820
821                 if (!CtdlThreadCheckStop())
822                 {
823                         retval = PurgeUsers();
824                         CtdlLogPrintf(CTDL_NOTICE, "Purged %d users.\n", retval);
825                 }
826                 
827                 if (!CtdlThreadCheckStop())
828                 {
829                         PurgeMessages();
830                         CtdlLogPrintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged);
831                 }
832
833                 if (!CtdlThreadCheckStop())
834                 {
835                         retval = PurgeRooms();
836                         CtdlLogPrintf(CTDL_NOTICE, "Expired %d rooms.\n", retval);
837                 }
838
839                 if (!CtdlThreadCheckStop())
840                 {
841                         retval = PurgeVisits();
842                         CtdlLogPrintf(CTDL_NOTICE, "Purged %d visits.\n", retval);
843                 }
844
845                 if (!CtdlThreadCheckStop())
846                 {
847                         retval = PurgeUseTable();
848                         CtdlLogPrintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
849                 }
850
851                 if (!CtdlThreadCheckStop())
852                 {
853                         retval = PurgeEuidIndexTable();
854                         CtdlLogPrintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
855                 }
856
857                 if (!CtdlThreadCheckStop())
858                 {
859                         retval = TDAP_ProcessAdjRefCountQueue();
860                         CtdlLogPrintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
861                 }
862
863                 if (!CtdlThreadCheckStop())
864                 {
865                         CtdlLogPrintf(CTDL_INFO, "Auto-purger: finished.\n");
866                         last_purge = now;       /* So we don't do it again soon */
867                 }
868                 else
869                         CtdlLogPrintf(CTDL_INFO, "Auto-purger: STOPPED.\n");
870
871         }
872         return NULL;
873 }
874 /*****************************************************************************/
875
876
877 void do_fsck_msg(long msgnum, void *userdata) {
878         struct ctdlroomref *ptr;
879
880         ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
881         ptr->next = rr;
882         ptr->msgnum = msgnum;
883         rr = ptr;
884 }
885
886 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
887 {
888         getroom(&CC->room, qrbuf->QRname);
889         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
890 }
891
892 /*
893  * Check message reference counts
894  */
895 void cmd_fsck(char *argbuf) {
896         long msgnum;
897         struct cdbdata *cdbmsg;
898         struct MetaData smi;
899         struct ctdlroomref *ptr;
900         int realcount;
901
902         if (CtdlAccessCheck(ac_aide)) return;
903
904         /* Lame way of checking whether anyone else is doing this now */
905         if (rr != NULL) {
906                 cprintf("%d Another FSCK is already running.\n", ERROR + RESOURCE_BUSY);
907                 return;
908         }
909
910         cprintf("%d Checking message reference counts\n", LISTING_FOLLOWS);
911
912         cprintf("\nThis could take a while.  Please be patient!\n\n");
913         cprintf("Gathering pointers...\n");
914         ForEachRoom(do_fsck_room, NULL);
915
916         get_control();
917         cprintf("Checking message base...\n");
918         for (msgnum = 0L; msgnum <= CitControl.MMhighest; ++msgnum) {
919
920                 cdbmsg = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
921                 if (cdbmsg != NULL) {
922                         cdb_free(cdbmsg);
923                         cprintf("Message %7ld    ", msgnum);
924
925                         GetMetaData(&smi, msgnum);
926                         cprintf("refcount=%-2d   ", smi.meta_refcount);
927
928                         realcount = 0;
929                         for (ptr = rr; ptr != NULL; ptr = ptr->next) {
930                                 if (ptr->msgnum == msgnum) ++realcount;
931                         }
932                         cprintf("realcount=%-2d\n", realcount);
933
934                         if ( (smi.meta_refcount != realcount)
935                            || (realcount == 0) ) {
936                                 AdjRefCount(msgnum, (smi.meta_refcount - realcount));
937                         }
938
939                 }
940
941         }
942
943         cprintf("Freeing memory...\n");
944         while (rr != NULL) {
945                 ptr = rr->next;
946                 free(rr);
947                 rr = ptr;
948         }
949
950         cprintf("Done!\n");
951         cprintf("000\n");
952
953 }
954
955
956
957
958 /*****************************************************************************/
959
960 CTDL_MODULE_INIT(expire)
961 {
962         if (!threading)
963         {
964                 CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
965         }
966         else
967                 CtdlThreadCreate("Auto Purger", CTDLTHREAD_BIGSTACK, purge_databases, NULL);
968         /* return our Subversion id for the Log */
969         return "$Id$";
970 }