citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
serv_expire.c
Go to the documentation of this file.
1// This module handles the expiry of old messages and the purging of old users.
2//
3// You might also see this module affectionately referred to as TDAP (The Dreaded Auto-Purger).
4//
5// Copyright (c) 1988-2022 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
6//
7// This program is open source software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License as published
9// by the Free Software Foundation; either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17
18#include "../../sysdep.h"
19#include <stdlib.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <fcntl.h>
23#include <signal.h>
24#include <pwd.h>
25#include <errno.h>
26#include <sys/types.h>
27#include <time.h>
28#include <sys/wait.h>
29#include <string.h>
30#include <limits.h>
31#include <libcitadel.h>
32#include "../../citadel.h"
33#include "../../server.h"
34#include "../../citserver.h"
35#include "../../support.h"
36#include "../../config.h"
37#include "policy.h"
38#include "../../database.h"
39#include "../../msgbase.h"
40#include "../../user_ops.h"
41#include "../../control.h"
42#include "../../threads.h"
43#include "../../context.h"
44
45#include "../../ctdl_module.h"
46
47
48struct PurgeList {
49 struct PurgeList *next;
50 char name[ROOMNAMELEN]; // use the larger of username or roomname
51};
52
53struct VPurgeList {
58};
59
60struct ValidRoom {
61 struct ValidRoom *next;
64};
65
66struct ValidUser {
67 struct ValidUser *next;
69};
70
73 long msgnum;
74};
75
76struct UPurgeList {
78 char up_key[256];
79};
80
81struct EPurgeList {
84 char *ep_key;
85};
86
87
88struct PurgeList *UserPurgeList = NULL;
89struct PurgeList *RoomPurgeList = NULL;
90struct ValidRoom *ValidRoomList = NULL;
91struct ValidUser *ValidUserList = NULL;
94char *users_corrupt_msg = NULL;
95char *users_zero_msg = NULL;
96struct ctdlroomref *rr = NULL;
97int force_purge_now = 0; // set to nonzero to force a run right now
98
99
100// First phase of message purge -- gather the locations of messages which
101// qualify for purging and write them to a temp file.
102void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
103 struct ExpirePolicy epbuf;
104 long delnum;
105 time_t xtime, now;
106 struct CtdlMessage *msg = NULL;
107 int a;
108 struct cdbdata *cdbfr;
109 long *msglist = NULL;
110 int num_msgs = 0;
111 FILE *purgelist;
112
113 purgelist = (FILE *)data;
114 fprintf(purgelist, "r=%s\n", qrbuf->QRname);
115
116 time(&now);
117 GetExpirePolicy(&epbuf, qrbuf);
118
119 // If the room is set to never expire messages ... do nothing
120 if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
121 if (epbuf.expire_mode == EXPIRE_MANUAL) return;
122
123 // Don't purge messages containing system configuration, dumbass.
124 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
125
126 // Ok, we got this far ... now let's see what's in the room.
127 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
128
129 if (cdbfr != NULL) {
130 msglist = malloc(cdbfr->len);
131 memcpy(msglist, cdbfr->ptr, cdbfr->len);
132 num_msgs = cdbfr->len / sizeof(long);
133 cdb_free(cdbfr);
134 }
135
136 // Nothing to do if there aren't any messages
137 if (num_msgs == 0) {
138 if (msglist != NULL) free(msglist);
139 return;
140 }
141
142 // If the room is set to expire by count, do that.
143 if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
144 if (num_msgs > epbuf.expire_value) {
145 for (a=0; a<(num_msgs - epbuf.expire_value); ++a) {
146 fprintf(purgelist, "m=%ld\n", msglist[a]);
148 }
149 }
150 }
151
152 // If the room is set to expire by age...
153 if (epbuf.expire_mode == EXPIRE_AGE) {
154 for (a=0; a<num_msgs; ++a) {
155 delnum = msglist[a];
156
157 msg = CtdlFetchMessage(delnum, 0); // don't need the body
158 if (msg != NULL) {
159 xtime = atol(msg->cm_fields[eTimestamp]);
160 CM_Free(msg);
161 }
162 else {
163 xtime = 0L;
164 }
165
166 if ((xtime > 0L) && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
167 fprintf(purgelist, "m=%ld\n", delnum);
169 }
170 }
171 }
172
173 if (msglist != NULL) free(msglist);
174}
175
176
177// Second phase of message purge -- read list of msgs from temp file and delete them.
178void DoPurgeMessages(FILE *purgelist) {
179 char roomname[ROOMNAMELEN];
180 long msgnum;
181 char buf[SIZ];
182
183 rewind(purgelist);
184 strcpy(roomname, "nonexistent room ___ ___");
185 while (fgets(buf, sizeof buf, purgelist) != NULL) {
186 buf[strlen(buf)-1]=0;
187 if (!strncasecmp(buf, "r=", 2)) {
188 strcpy(roomname, &buf[2]);
189 }
190 if (!strncasecmp(buf, "m=", 2)) {
191 msgnum = atol(&buf[2]);
192 if (msgnum > 0L) {
193 CtdlDeleteMessages(roomname, &msgnum, 1, "");
194 }
195 }
196 }
197}
198
199
200void PurgeMessages(void) {
201 FILE *purgelist;
202
203 syslog(LOG_DEBUG, "PurgeMessages() called");
204 messages_purged = 0;
205
206 purgelist = tmpfile();
207 if (purgelist == NULL) {
208 syslog(LOG_CRIT, "Can't create purgelist temp file: %s", strerror(errno));
209 return;
210 }
211
212 CtdlForEachRoom(GatherPurgeMessages, (void *)purgelist );
213 DoPurgeMessages(purgelist);
214 fclose(purgelist);
215}
216
217
218void AddValidUser(char *username, void *data) {
219 struct ValidUser *vuptr;
220 struct ctdluser usbuf;
221
222 if (CtdlGetUser(&usbuf, username) != 0) {
223 return;
224 }
225
226 vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
227 vuptr->next = ValidUserList;
228 vuptr->vu_usernum = usbuf.usernum;
229 ValidUserList = vuptr;
230}
231
232
233void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
234 struct ValidRoom *vrptr;
235
236 vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
237 vrptr->next = ValidRoomList;
238 vrptr->vr_roomnum = qrbuf->QRnumber;
239 vrptr->vr_roomgen = qrbuf->QRgen;
240 ValidRoomList = vrptr;
241}
242
243
244void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
245 time_t age, purge_secs;
246 struct PurgeList *pptr;
247 struct ValidUser *vuptr;
248 int do_purge = 0;
249
250 // For mailbox rooms, there's only one purging rule: if the user who
251 // owns the room still exists, we keep the room; otherwise, we purge
252 // it. Bypass any other rules.
253 if (qrbuf->QRflags & QR_MAILBOX) {
254 // if user not found, do_purge will be 1
255 do_purge = 1;
256 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
257 if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
258 do_purge = 0;
259 }
260 }
261 }
262 else {
263 // Any of these attributes render a room non-purgable
264 if (qrbuf->QRflags & QR_PERMANENT) return;
265 if (qrbuf->QRflags & QR_DIRECTORY) return;
266 if (qrbuf->QRflags2 & QR2_SYSTEM) return;
267 if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
268 if (CtdlIsNonEditable(qrbuf)) return;
269
270 // If we don't know the modification date, be safe and don't purge
271 if (qrbuf->QRmtime <= (time_t)0) return;
272
273 // If no room purge time is set, be safe and don't purge
274 if (CtdlGetConfigLong("c_roompurge") < 0) return;
275
276 // Otherwise, check the date of last modification
277 age = time(NULL) - (qrbuf->QRmtime);
278 purge_secs = CtdlGetConfigLong("c_roompurge") * 86400;
279 if (purge_secs <= (time_t)0) return;
280 syslog(LOG_DEBUG, "<%s> is <%ld> seconds old", qrbuf->QRname, (long)age);
281 if (age > purge_secs) do_purge = 1;
282 } // !QR_MAILBOX
283
284 if (do_purge) {
285 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
286 pptr->next = RoomPurgeList;
287 strcpy(pptr->name, qrbuf->QRname);
288 RoomPurgeList = pptr;
289 }
290
291}
292
293
294int PurgeRooms(void) {
295 struct PurgeList *pptr;
296 int num_rooms_purged = 0;
297 struct ctdlroom qrbuf;
298 struct ValidUser *vuptr;
299 char *transcript = NULL;
300
301 syslog(LOG_DEBUG, "PurgeRooms() called");
302
303 // Load up a table full of valid user numbers so we can delete
304 // user-owned rooms for users who no longer exist
306
307 // Then cycle through the room file
309
310 // Free the valid user list
311 while (ValidUserList != NULL) {
312 vuptr = ValidUserList->next;
314 ValidUserList = vuptr;
315 }
316
317 transcript = malloc(SIZ);
318 strcpy(transcript, "The following rooms have been auto-purged:\n");
319
320 while (RoomPurgeList != NULL) {
321 if (CtdlGetRoom(&qrbuf, RoomPurgeList->name) == 0) {
322 transcript=realloc(transcript, strlen(transcript)+SIZ);
323 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n", qrbuf.QRname);
325 ++num_rooms_purged;
326 }
327 pptr = RoomPurgeList->next;
329 RoomPurgeList = pptr;
330 }
331
332 if (num_rooms_purged > 0) CtdlAideMessage(transcript, "Room Autopurger Message");
333 free(transcript);
334
335 syslog(LOG_DEBUG, "Purged %d rooms.", num_rooms_purged);
336 return(num_rooms_purged);
337}
338
339
340// Back end function to check user accounts for expiration.
341void do_user_purge(char *username, void *data) {
342 int purge;
343 time_t now;
344 time_t purge_time;
345 struct PurgeList *pptr;
346 struct ctdluser us;
347
348 if (CtdlGetUser(&us, username) != 0) {
349 return;
350 }
351
352 // Set purge time; if the user overrides the system default, use it
353 if (us.USuserpurge > 0) {
354 purge_time = ((time_t)us.USuserpurge) * 86400;
355 }
356 else {
357 purge_time = CtdlGetConfigLong("c_userpurge") * 86400;
358 }
359
360 // The default rule is to not purge.
361 purge = 0;
362
363 // If the user has not logged in for the configured amount of time, expire the account.
364 if (CtdlGetConfigLong("c_userpurge") > 0) {
365 now = time(NULL);
366 if ((now - us.lastcall) > purge_time) purge = 1;
367 }
368
369 // If the account is marked as permanent, don't purge it.
370 if (us.flags & US_PERM) purge = 0;
371
372 // If the account is an administrator, don't purge it.
373 if (us.axlevel == 6) purge = 0;
374
375 // If the access level is 0, the record should already have been
376 // deleted, but maybe the user was logged in at the time or something.
377 // Delete the record now.
378 if (us.axlevel == 0) purge = 1;
379
380 // If the user set his/her password to 'deleteme', he/she
381 // wishes to be deleted, so purge the record.
382 // Moved this lower down so that aides and permanent users get purged if they ask to be.
383 if (!strcasecmp(us.password, "deleteme")) purge = 1;
384
385 // Fewer than zero calls is impossible, indicating a corrupted record.
386 if (us.timescalled < 0) purge = 1;
387
388 // any negative user number, is also impossible.
389 if (us.usernum < 0L) purge = 1;
390
391 // Don't purge user 0. That user is there for the system
392 if (us.usernum == 0L) purge = 0;
393
394 // If the user has no full name entry then we can't purge them since the actual purge can't find them.
395 // This shouldn't happen but does somehow.
396 if (IsEmptyStr(us.fullname)) {
397 purge = 0;
398
399 if (us.usernum > 0L) {
400 purge=0;
401 if (users_corrupt_msg == NULL) {
403 strcpy(users_corrupt_msg,
404 "The auto-purger found the following user numbers with no name.\n"
405 "The system has no way to purge a user with no name,"
406 " and should not be able to create them either.\n"
407 "This indicates corruption of the user DB or possibly a bug.\n"
408 "It may be a good idea to restore your DB from a backup.\n"
409 );
410 }
411
413 snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], 29, " %ld\n", us.usernum);
414 }
415 }
416
417 if (purge == 1) {
418 pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
419 pptr->next = UserPurgeList;
420 strcpy(pptr->name, us.fullname);
421 UserPurgeList = pptr;
422 }
423 else {
425 }
426
427}
428
429
430int PurgeUsers(void) {
431 struct PurgeList *pptr;
432 int num_users_purged = 0;
433 char *transcript = NULL;
434
435 syslog(LOG_DEBUG, "PurgeUsers() called");
437
438 switch(CtdlGetConfigInt("c_auth_mode")) {
439 case AUTHMODE_NATIVE:
441 break;
442 default:
443 syslog(LOG_DEBUG, "User purge for auth mode %d is not implemented.", CtdlGetConfigInt("c_auth_mode"));
444 break;
445 }
446
447 transcript = malloc(SIZ);
448
449 if (users_not_purged == 0) {
450 strcpy(transcript, "The auto-purger was told to purge every user. It is\n"
451 "refusing to do this because it usually indicates a problem\n"
452 "such as an inability to communicate with a name service.\n"
453 );
454 while (UserPurgeList != NULL) {
455 pptr = UserPurgeList->next;
457 UserPurgeList = pptr;
458 ++num_users_purged;
459 }
460 }
461
462 else {
463 strcpy(transcript, "The following users have been auto-purged:\n");
464 while (UserPurgeList != NULL) {
465 transcript=realloc(transcript, strlen(transcript)+SIZ);
466 snprintf(&transcript[strlen(transcript)], SIZ, " %s\n", UserPurgeList->name);
468 pptr = UserPurgeList->next;
470 UserPurgeList = pptr;
471 ++num_users_purged;
472 }
473 }
474
475 if (num_users_purged > 0) CtdlAideMessage(transcript, "User Purge Message");
476 free(transcript);
477
478 if (users_corrupt_msg) {
479 CtdlAideMessage(users_corrupt_msg, "User Corruption Message");
481 users_corrupt_msg = NULL;
482 }
483
484 if(users_zero_msg) {
485 CtdlAideMessage(users_zero_msg, "User Zero Message");
487 users_zero_msg = NULL;
488 }
489
490 syslog(LOG_DEBUG, "Purged %d users.", num_users_purged);
491 return(num_users_purged);
492}
493
494
495// Purge visits
496//
497// This is a really cumbersome "garbage collection" function. We have to
498// delete visits which refer to rooms and/or users which no longer exist. In
499// order to prevent endless traversals of the room and user files, we first
500// build linked lists of rooms and users which _do_ exist on the system, then
501// traverse the visit file, checking each record against those two lists and
502// purging the ones that do not have a match on _both_ lists. (Remember, if
503// either the room or user being referred to is no longer on the system, the
504// record is useless and should be removed.)
505//
506int PurgeVisits(void) {
507 struct cdbdata *cdbvisit;
508 visit vbuf;
509 struct VPurgeList *VisitPurgeList = NULL;
510 struct VPurgeList *vptr;
511 int purged = 0;
512 char IndexBuf[32];
513 int IndexLen;
514 struct ValidRoom *vrptr;
515 struct ValidUser *vuptr;
516 int RoomIsValid, UserIsValid;
517
518 // First, load up a table full of valid room/gen combinations
520
521 // Then load up a table full of valid user numbers
523
524 // Now traverse through the visits, purging irrelevant records...
526 while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
527 memset(&vbuf, 0, sizeof(visit));
528 memcpy(&vbuf, cdbvisit->ptr,
529 ( (cdbvisit->len > sizeof(visit)) ?
530 sizeof(visit) : cdbvisit->len) );
531 cdb_free(cdbvisit);
532
533 RoomIsValid = 0;
534 UserIsValid = 0;
535
536 // Check to see if the room exists
537 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
538 if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
539 && (vrptr->vr_roomgen==vbuf.v_roomgen))
540 RoomIsValid = 1;
541 }
542
543 // Check to see if the user exists
544 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
545 if (vuptr->vu_usernum == vbuf.v_usernum)
546 UserIsValid = 1;
547 }
548
549 // Put the record on the purge list if it's dead
550 if ((RoomIsValid==0) || (UserIsValid==0)) {
551 vptr = (struct VPurgeList *)
552 malloc(sizeof(struct VPurgeList));
553 vptr->next = VisitPurgeList;
554 vptr->vp_roomnum = vbuf.v_roomnum;
555 vptr->vp_roomgen = vbuf.v_roomgen;
556 vptr->vp_usernum = vbuf.v_usernum;
557 VisitPurgeList = vptr;
558 }
559
560 }
561
562 // Free the valid room/gen combination list
563 while (ValidRoomList != NULL) {
564 vrptr = ValidRoomList->next;
566 ValidRoomList = vrptr;
567 }
568
569 // Free the valid user list
570 while (ValidUserList != NULL) {
571 vuptr = ValidUserList->next;
573 ValidUserList = vuptr;
574 }
575
576 // Now delete every visit on the purged list
577 while (VisitPurgeList != NULL) {
578 IndexLen = GenerateRelationshipIndex(IndexBuf,
579 VisitPurgeList->vp_roomnum,
580 VisitPurgeList->vp_roomgen,
581 VisitPurgeList->vp_usernum);
582 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
583 vptr = VisitPurgeList->next;
584 free(VisitPurgeList);
585 VisitPurgeList = vptr;
586 ++purged;
587 }
588
589 return(purged);
590}
591
592
593// Purge the use table of old entries.
594int PurgeUseTable(StrBuf *ErrMsg) {
595 int purged = 0;
596 struct cdbdata *cdbut;
597 struct UseTable ut;
598 struct UPurgeList *ul = NULL;
599 struct UPurgeList *uptr;
600
601 // Phase 1: traverse through the table, discovering old records...
602
603 syslog(LOG_DEBUG, "Purge use table: phase 1");
605 while(cdbut = cdb_next_item(CDB_USETABLE), cdbut != NULL) {
606 if (cdbut->len > sizeof(struct UseTable))
607 memcpy(&ut, cdbut->ptr, sizeof(struct UseTable));
608 else {
609 memset(&ut, 0, sizeof(struct UseTable));
610 memcpy(&ut, cdbut->ptr, cdbut->len);
611 }
612 cdb_free(cdbut);
613
614 if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
615 uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
616 if (uptr != NULL) {
617 uptr->next = ul;
618 safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
619 ul = uptr;
620 }
621 ++purged;
622 }
623
624 }
625
626 // Phase 2: delete the records
627 syslog(LOG_DEBUG, "Purge use table: phase 2");
628 while (ul != NULL) {
629 cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
630 uptr = ul->next;
631 free(ul);
632 ul = uptr;
633 }
634
635 syslog(LOG_DEBUG, "Purge use table: finished (purged %d records)", purged);
636 return(purged);
637}
638
639
640// Purge the EUID Index of old records.
642 int purged = 0;
643 struct cdbdata *cdbei;
644 struct EPurgeList *el = NULL;
645 struct EPurgeList *eptr;
646 long msgnum;
647 struct CtdlMessage *msg = NULL;
648
649 // Phase 1: traverse through the table, discovering old records...
650 syslog(LOG_DEBUG, "Purge EUID index: phase 1");
652 while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
653
654 memcpy(&msgnum, cdbei->ptr, sizeof(long));
655
656 msg = CtdlFetchMessage(msgnum, 0);
657 if (msg != NULL) {
658 CM_Free(msg); // it still exists, so do nothing
659 }
660 else {
661 eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
662 if (eptr != NULL) {
663 eptr->next = el;
664 eptr->ep_keylen = cdbei->len - sizeof(long);
665 eptr->ep_key = malloc(cdbei->len);
666 memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
667 el = eptr;
668 }
669 ++purged;
670 }
671
672 cdb_free(cdbei);
673
674 }
675
676 // Phase 2: delete the records
677 syslog(LOG_DEBUG, "Purge euid index: phase 2");
678 while (el != NULL) {
680 free(el->ep_key);
681 eptr = el->next;
682 free(el);
683 el = eptr;
684 }
685
686 syslog(LOG_DEBUG, "Purge euid index: finished (purged %d records)", purged);
687 return(purged);
688}
689
690
691// Purge external auth assocations for missing users (theoretically this will never delete anything)
693 struct cdbdata *cdboi;
694 struct ctdluser usbuf;
695 HashList *keys = NULL;
696 HashPos *HashPos;
697 char *deleteme = NULL;
698 long len;
699 void *Value;
700 const char *Key;
701 int num_deleted = 0;
702 long usernum = 0L;
703
704 keys = NewHash(1, NULL);
705 if (!keys) return(0);
706
708 while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
709 if (cdboi->len > sizeof(long)) {
710 memcpy(&usernum, cdboi->ptr, sizeof(long));
711 if (CtdlGetUserByNumber(&usbuf, usernum) != 0) {
712 deleteme = strdup(cdboi->ptr + sizeof(long)),
713 Put(keys, deleteme, strlen(deleteme), deleteme, NULL);
714 }
715 }
716 cdb_free(cdboi);
717 }
718
719 // Go through the hash list, deleting keys we stored in it
720
721 HashPos = GetNewHashPos(keys, 0);
722 while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0) {
723 syslog(LOG_DEBUG, "Deleting associated external authenticator <%s>", (char*)Value);
724 cdb_delete(CDB_EXTAUTH, Value, strlen(Value));
725 // note: don't free(Value) -- deleting the hash list will handle this for us
726 ++num_deleted;
727 }
728 DeleteHashPos(&HashPos);
729 DeleteHash(&keys);
730 return num_deleted;
731}
732
733
734void purge_databases(void) {
735 int retval;
736 static time_t last_purge = 0;
737 time_t now;
738 struct tm tm;
739
740 // Do the auto-purge if the current hour equals the purge hour,
741 // but not if the operation has already been performed in the
742 // last twelve hours. This is usually enough granularity.
743 now = time(NULL);
744 localtime_r(&now, &tm);
745 if (((tm.tm_hour != CtdlGetConfigInt("c_purge_hour")) || ((now - last_purge) < 43200)) && (force_purge_now == 0)) {
746 return;
747 }
748
749 syslog(LOG_INFO, "Auto-purger: starting.");
750
752 retval = PurgeUsers();
753 syslog(LOG_NOTICE, "Purged %d users.", retval);
754 }
755
758 syslog(LOG_NOTICE, "Expired %d messages.", messages_purged);
759 }
760
762 retval = PurgeRooms();
763 syslog(LOG_NOTICE, "Expired %d rooms.", retval);
764 }
765
767 retval = PurgeVisits();
768 syslog(LOG_NOTICE, "Purged %d visits.", retval);
769 }
770
772 StrBuf *ErrMsg;
773 ErrMsg = NewStrBuf();
774 retval = PurgeUseTable(ErrMsg);
775 syslog(LOG_NOTICE, "Purged %d entries from the use table.", retval);
776 FreeStrBuf(&ErrMsg);
777 }
778
780 retval = PurgeEuidIndexTable();
781 syslog(LOG_NOTICE, "Purged %d entries from the EUID index.", retval);
782 }
783
786 syslog(LOG_NOTICE, "Purged %d stale external auth associations.", retval);
787 }
788
789 //if (!server_shutting_down) {
790 // FIXME this is where we could do a non-interactive delete of zero-refcount messages
791 //}
792
793 if ( (!server_shutting_down) && (CtdlGetConfigInt("c_shrink_db_files") != 0) ) {
794 cdb_compact(); // Shrink the DB files on disk
795 }
796
798 syslog(LOG_INFO, "Auto-purger: finished.");
799 last_purge = now; // So we don't do it again soon
800 force_purge_now = 0;
801 }
802 else {
803 syslog(LOG_INFO, "Auto-purger: STOPPED.");
804 }
805}
806
807
808// Manually initiate a run of The Dreaded Auto-Purger (tm)
809void cmd_tdap(char *argbuf) {
810 if (CtdlAccessCheck(ac_aide)) return;
811 force_purge_now = 1;
812 cprintf("%d Manually initiating a purger run now.\n", CIT_OK);
813}
814
815
816// Initialization function, called from modules_init.c
818 if (!threading) {
819 CtdlRegisterProtoHook(cmd_tdap, "TDAP", "Manually initiate auto-purger");
820 CtdlRegisterProtoHook(cmd_gpex, "GPEX", "Get expire policy");
821 CtdlRegisterProtoHook(cmd_spex, "SPEX", "Set expire policy");
823 }
824
825 // return our module name for the log
826 return "expire";
827}
#define ROOMNAMELEN
Definition: citadel.h:51
#define EXPIRE_NUMMSGS
Definition: citadel.h:64
#define EXPIRE_MANUAL
Definition: citadel.h:63
#define EXPIRE_AGE
Definition: citadel.h:65
#define AUTHMODE_NATIVE
Definition: citadel.h:147
#define EXPIRE_NEXTLEVEL
Definition: citadel.h:62
int CtdlGetConfigInt(char *key)
Definition: config.c:390
long CtdlGetConfigLong(char *key)
Definition: config.c:398
time_t last_purge
Definition: context.c:22
int CtdlAccessCheck(int)
Definition: user_ops.c:334
int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
Definition: room_ops.c:309
int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
Definition: user_ops.c:398
void CtdlRegisterSessionHook(void(*fcn_ptr)(void), int EventType, int Priority)
#define CtdlAideMessage(TEXT, SUBJECT)
Definition: ctdl_module.h:61
int threading
Definition: modules_init.c:24
void CtdlDeleteRoom(struct ctdlroom *qrbuf)
Definition: room_ops.c:970
void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
Definition: room_ops.c:542
#define PRIO_CLEANUP
Definition: ctdl_module.h:78
@ ac_aide
Definition: ctdl_module.h:249
int CtdlIsNonEditable(struct ctdlroom *qrbuf)
Definition: room_ops.c:608
int CtdlGetUser(struct ctdluser *usbuf, char *name)
Definition: user_ops.c:64
void CtdlRegisterProtoHook(void(*handler)(char *), char *cmd, char *desc)
void cdb_free(struct cdbdata *cdb)
Definition: database.c:604
struct cdbdata * cdb_next_item(int cdb)
Definition: database.c:650
struct cdbdata * cdb_fetch(int cdb, const void *key, int keylen)
Definition: database.c:541
int cdb_delete(int cdb, void *key, int keylen)
Definition: database.c:477
void cdb_rewind(int cdb)
Definition: database.c:626
void cdb_compact(void)
Definition: database.c:759
void cmd_gpex(char *argbuf)
Definition: expire_policy.c:79
void cmd_spex(char *argbuf)
void GetExpirePolicy(struct ExpirePolicy *epbuf, struct ctdlroom *qrbuf)
Definition: expire_policy.c:39
#define CIT_OK
Definition: ipcdef.h:6
#define QR_DIRECTORY
Definition: ipcdef.h:44
#define QR_MAILBOX
Definition: ipcdef.h:53
#define QR_PERMANENT
Definition: ipcdef.h:39
#define US_PERM
Definition: ipcdef.h:63
#define QR2_SYSTEM
Definition: ipcdef.h:55
void CM_Free(struct CtdlMessage *msg)
Definition: msgbase.c:305
struct CtdlMessage * CtdlFetchMessage(long msgnum, int with_body)
Definition: msgbase.c:1128
int CtdlDeleteMessages(const char *room_name, long *dmsgnums, int num_dmsgnums, char *content_type)
Definition: msgbase.c:3229
void * malloc(unsigned)
void free(void *)
void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data)
Definition: serv_expire.c:102
int PurgeUseTable(StrBuf *ErrMsg)
Definition: serv_expire.c:594
int PurgeStaleExtAuthAssociations(void)
Definition: serv_expire.c:692
char * ctdl_module_init_expire(void)
Definition: serv_expire.c:817
struct ValidRoom * ValidRoomList
Definition: serv_expire.c:90
struct PurgeList * RoomPurgeList
Definition: serv_expire.c:89
int PurgeUsers(void)
Definition: serv_expire.c:430
int PurgeEuidIndexTable(void)
Definition: serv_expire.c:641
char * users_zero_msg
Definition: serv_expire.c:95
void DoPurgeRooms(struct ctdlroom *qrbuf, void *data)
Definition: serv_expire.c:244
void purge_databases(void)
Definition: serv_expire.c:734
struct ctdlroomref * rr
Definition: serv_expire.c:96
struct PurgeList * UserPurgeList
Definition: serv_expire.c:88
int force_purge_now
Definition: serv_expire.c:97
int PurgeRooms(void)
Definition: serv_expire.c:294
void cmd_tdap(char *argbuf)
Definition: serv_expire.c:809
int messages_purged
Definition: serv_expire.c:92
char * users_corrupt_msg
Definition: serv_expire.c:94
void do_user_purge(char *username, void *data)
Definition: serv_expire.c:341
int users_not_purged
Definition: serv_expire.c:93
void DoPurgeMessages(FILE *purgelist)
Definition: serv_expire.c:178
void AddValidRoom(struct ctdlroom *qrbuf, void *data)
Definition: serv_expire.c:233
struct ValidUser * ValidUserList
Definition: serv_expire.c:91
void PurgeMessages(void)
Definition: serv_expire.c:200
void AddValidUser(char *username, void *data)
Definition: serv_expire.c:218
int PurgeVisits(void)
Definition: serv_expire.c:506
struct ctdlroom qrbuf
Definition: serv_migrate.c:497
visit vbuf
Definition: serv_migrate.c:503
struct ctdluser usbuf
Definition: serv_migrate.c:496
@ eTimestamp
Definition: server.h:319
@ CDB_VISIT
Definition: server.h:190
@ CDB_EUIDINDEX
Definition: server.h:195
@ CDB_USETABLE
Definition: server.h:192
@ CDB_EXTAUTH
Definition: server.h:197
@ CDB_MSGLISTS
Definition: server.h:189
#define EVT_TIMER
Definition: server.h:224
char * cm_fields[256]
Definition: server.h:37
int ep_keylen
Definition: serv_expire.c:83
char * ep_key
Definition: serv_expire.c:84
struct EPurgeList * next
Definition: serv_expire.c:82
int expire_mode
Definition: citadel.h:58
int expire_value
Definition: citadel.h:59
char name[ROOMNAMELEN]
Definition: serv_expire.c:50
struct PurgeList * next
Definition: serv_expire.c:49
struct UPurgeList * next
Definition: serv_expire.c:77
char up_key[256]
Definition: serv_expire.c:78
char ut_msgid[4096]
Definition: server.h:298
time_t ut_timestamp
Definition: server.h:299
long vp_roomnum
Definition: serv_expire.c:55
struct VPurgeList * next
Definition: serv_expire.c:54
long vp_roomgen
Definition: serv_expire.c:56
long vp_usernum
Definition: serv_expire.c:57
struct ValidRoom * next
Definition: serv_expire.c:61
long vr_roomgen
Definition: serv_expire.c:63
long vr_roomnum
Definition: serv_expire.c:62
long vu_usernum
Definition: serv_expire.c:68
struct ValidUser * next
Definition: serv_expire.c:67
long v_roomgen
Definition: server.h:247
long v_roomnum
Definition: server.h:246
long v_usernum
Definition: server.h:248
size_t len
Definition: server.h:203
char * ptr
Definition: server.h:204
unsigned QRflags2
Definition: citadel.h:107
time_t QRmtime
Definition: citadel.h:103
char QRname[128]
Definition: citadel.h:94
long QRnumber
Definition: citadel.h:105
unsigned QRflags
Definition: citadel.h:99
time_t QRgen
Definition: citadel.h:98
struct ctdlroomref * next
Definition: serv_expire.c:72
unsigned flags
Definition: citadel.h:73
cit_uint8_t axlevel
Definition: citadel.h:76
char fullname[64]
Definition: citadel.h:80
time_t lastcall
Definition: citadel.h:78
long timescalled
Definition: citadel.h:74
long usernum
Definition: citadel.h:77
char password[32]
Definition: citadel.h:72
int USuserpurge
Definition: citadel.h:79
#define USETABLE_RETAIN
Definition: sysconfig.h:79
#define SIZ
Definition: sysconfig.h:33
#define SYSCONFIGROOM
Definition: sysconfig.h:72
void cprintf(const char *format,...)
Definition: sysdep.c:369
int server_shutting_down
Definition: threads.c:26
void ForEachUser(void(*CallBack)(char *, void *out_data), void *in_data)
Definition: user_ops.c:1077
int purge_user(char pname[])
Definition: user_ops.c:860
int GenerateRelationshipIndex(char *IndexBuf, long RoomID, long RoomGen, long UserID)
Definition: user_ops.c:228