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)  

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