]> code.citadel.org Git - citadel.git/blob - citadel/serv_expire.c
bc93cb92470603044773227d89eeca20d0543503
[citadel.git] / citadel / serv_expire.c
1 /*
2  * serv_expire.c
3  *
4  * This module handles the expiry of old messages and the purging of old users.
5  *
6  */
7 /* $Id$ */
8
9
10 /*
11  * A brief technical discussion:
12  *
13  * Several of the purge operations found in this module operate in two
14  * stages: the first stage generates a linked list of objects to be deleted,
15  * then the second stage deletes all listed objects from the database.
16  *
17  * At first glance this may seem cumbersome and unnecessary.  The reason it is
18  * implemented in this way is because GDBM (and perhaps some other backends we
19  * may hook into in the future) explicitly do _not_ support the deletion of
20  * records from a file while the file is being traversed.  The delete operation
21  * will succeed, but the traversal is not guaranteed to visit every object if
22  * this is done.  Therefore we utilize the two-stage purge.
23  */
24
25
26 #include "sysdep.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/wait.h>
37 #include <string.h>
38 #include <limits.h>
39 #ifdef HAVE_PTHREAD_H
40 #include <pthread.h>
41 #endif
42 #include "citadel.h"
43 #include "server.h"
44 #include <syslog.h>
45 #include <time.h>
46 #include "sysdep_decls.h"
47 #include "citserver.h"
48 #include "support.h"
49 #include "config.h"
50 #include "dynloader.h"
51 #include "room_ops.h"
52 #include "policy.h"
53 #include "database.h"
54 #include "msgbase.h"
55 #include "user_ops.h"
56 #include "control.h"
57 #include "tools.h"
58
59
60 struct oldvisit {
61         char v_roomname[ROOMNAMELEN];
62         long v_generation;
63         long v_lastseen;
64         unsigned int v_flags;
65         };
66
67 struct PurgeList {
68         struct PurgeList *next;
69         char name[ROOMNAMELEN]; /* use the larger of username or roomname */
70         };
71
72 struct VPurgeList {
73         struct VPurgeList *next;
74         long vp_roomnum;
75         long vp_roomgen;
76         long vp_usernum;
77         };
78
79 struct ValidRoom {
80         struct ValidRoom *next;
81         long vr_roomnum;
82         long vr_roomgen;
83         };
84
85 struct ValidUser {
86         struct ValidUser *next;
87         long vu_usernum;
88         };
89
90 struct PurgeList *UserPurgeList = NULL;
91 struct PurgeList *RoomPurgeList = NULL;
92 struct ValidRoom *ValidRoomList = NULL;
93 struct ValidUser *ValidUserList = NULL;
94 int messages_purged;
95
96 extern struct CitContext *ContextList;
97
98 #define MODULE_NAME     "Expire old messages, users, rooms"
99 #define MODULE_AUTHOR   "Art Cancro"
100 #define MODULE_EMAIL    "ajc@uncnsrd.mt-kisco.ny.us"
101 #define MAJOR_VERSION   1
102 #define MINOR_VERSION   0
103
104 static struct DLModule_Info info = {
105         MODULE_NAME,
106         MODULE_AUTHOR,
107         MODULE_EMAIL,
108         MAJOR_VERSION,
109         MINOR_VERSION
110         };
111
112 void DoPurgeMessages(struct quickroom *qrbuf) {
113         struct ExpirePolicy epbuf;
114         long delnum;
115         time_t xtime, now;
116         char msgid[64];
117         int a;
118
119         time(&now);
120         GetExpirePolicy(&epbuf, qrbuf);
121         
122         /*
123         lprintf(9, "ExpirePolicy for <%s> is <%d> <%d>\n",
124                 qrbuf->QRname, epbuf.expire_mode, epbuf.expire_value);
125         */
126
127         /* If the room is set to never expire messages ... do nothing */
128         if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
129         if (epbuf.expire_mode == EXPIRE_MANUAL) return;
130
131         begin_critical_section(S_QUICKROOM);
132         get_msglist(qrbuf);
133         
134         /* Nothing to do if there aren't any messages */
135         if (CC->num_msgs == 0) {
136                 end_critical_section(S_QUICKROOM);
137                 return;
138                 }
139
140         /* If the room is set to expire by count, do that */
141         if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
142                 while (CC->num_msgs > epbuf.expire_value) {
143                         delnum = MessageFromList(0);
144                         lprintf(5, "Expiring message %ld\n", delnum);
145                         AdjRefCount(delnum, -1); 
146                         memcpy(&CC->msglist[0], &CC->msglist[1],
147                                 (sizeof(long)*(CC->num_msgs - 1)));
148                         CC->num_msgs = CC->num_msgs - 1;
149                         ++messages_purged;
150                         }
151                 }
152
153         /* If the room is set to expire by age... */
154         if (epbuf.expire_mode == EXPIRE_AGE) {
155                 for (a=0; a<(CC->num_msgs); ++a) {
156                         delnum = MessageFromList(a);
157                         sprintf(msgid, "%ld", delnum);
158                         xtime = output_message(msgid, MT_DATE, 0);
159
160                         if ((xtime > 0L)
161                            && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
162                                 lprintf(5, "Expiring message %ld\n", delnum);
163                                 AdjRefCount(delnum, -1); 
164                                 SetMessageInList(a, 0L);
165                                 ++messages_purged;
166                                 }
167                         }
168                 }
169         CC->num_msgs = sort_msglist(CC->msglist, CC->num_msgs);
170         put_msglist(qrbuf);
171         end_critical_section(S_QUICKROOM);
172         }
173
174 void PurgeMessages(void) {
175         lprintf(5, "PurgeMessages() called\n");
176         messages_purged = 0;
177         ForEachRoom(DoPurgeMessages);
178 }
179
180
181 void AddValidUser(struct usersupp *usbuf) {
182         struct ValidUser *vuptr;
183
184         vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
185         vuptr->next = ValidUserList;
186         vuptr->vu_usernum = usbuf->usernum;
187         ValidUserList = vuptr;
188         }
189
190 void AddValidRoom(struct quickroom *qrbuf) {
191         struct ValidRoom *vrptr;
192
193         vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
194         vrptr->next = ValidRoomList;
195         vrptr->vr_roomnum = qrbuf->QRnumber;
196         vrptr->vr_roomgen = qrbuf->QRgen;
197         ValidRoomList = vrptr;
198         }
199
200 void DoPurgeRooms(struct quickroom *qrbuf) {
201         time_t age, purge_secs;
202         struct PurgeList *pptr;
203         struct ValidUser *vuptr;
204         int do_purge = 0;
205
206         /* For mailbox rooms, there's only one purging rule: if the user who
207          * owns the room still exists, we keep the room; otherwise, we purge
208          * it.  Bypass any other rules.
209          */
210         if (qrbuf->QRflags & QR_MAILBOX) {
211                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
212                         if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
213                                 do_purge = 0;
214                                 goto BYPASS;
215                         }
216                 }
217                 /* user not found */
218                 do_purge = 1;
219                 goto BYPASS;
220         }
221
222         /* Any of these attributes render a room non-purgable */
223         if (qrbuf->QRflags & QR_PERMANENT) return;
224         if (qrbuf->QRflags & QR_DIRECTORY) return;
225         if (qrbuf->QRflags & QR_NETWORK) return;
226         if (is_noneditable(qrbuf)) return;
227
228         /* If we don't know the modification date, be safe and don't purge */
229         if (qrbuf->QRmtime <= (time_t)0) return;
230
231         /* If no room purge time is set, be safe and don't purge */
232         if (config.c_roompurge < 0) return;
233
234         /* Otherwise, check the date of last modification */
235         age = time(NULL) - (qrbuf->QRmtime);
236         purge_secs = (time_t)config.c_roompurge * (time_t)86400;
237         if (purge_secs <= (time_t)0) return;
238         lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
239         if (age > purge_secs) do_purge = 1;
240
241 BYPASS: if (do_purge) {
242                 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
243                 pptr->next = RoomPurgeList;
244                 strcpy(pptr->name, qrbuf->QRname);
245                 RoomPurgeList = pptr;
246         }
247
248 }
249         
250
251
252 int PurgeRooms(void) {
253         struct PurgeList *pptr;
254         int num_rooms_purged = 0;
255         struct quickroom qrbuf;
256         struct ValidUser *vuptr;
257         char *transcript = NULL;
258
259         lprintf(5, "PurgeRooms() called\n");
260
261
262         /* Load up a table full of valid user numbers so we can delete
263          * user-owned rooms for users who no longer exist */
264         ForEachUser(AddValidUser);
265
266         /* Then cycle through the room file */
267         ForEachRoom(DoPurgeRooms);
268
269         /* Free the valid user list */
270         while (ValidUserList != NULL) {
271                 vuptr = ValidUserList->next;
272                 phree(ValidUserList);
273                 ValidUserList = vuptr;
274                 }
275
276
277         transcript = mallok(256);
278         strcpy(transcript, "The following rooms have been auto-purged:\n");
279
280         while (RoomPurgeList != NULL) {
281                 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
282                         transcript=reallok(transcript, strlen(transcript)+256);
283                         sprintf(&transcript[strlen(transcript)], " %s\n",
284                                 qrbuf.QRname);
285                         delete_room(&qrbuf);
286                         }
287                 pptr = RoomPurgeList->next;
288                 phree(RoomPurgeList);
289                 RoomPurgeList = pptr;
290                 ++num_rooms_purged;
291                 }
292
293         if (num_rooms_purged > 0) aide_message(transcript);
294         phree(transcript);
295
296         lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
297         return(num_rooms_purged);
298         }
299
300
301 void do_user_purge(struct usersupp *us) {
302         int purge;
303         time_t now;
304         time_t purge_time;
305         struct PurgeList *pptr;
306
307         /* Set purge time; if the user overrides the system default, use it */
308         if (us->USuserpurge > 0) {
309                 purge_time = ((time_t)us->USuserpurge) * 86400L;
310                 }
311         else {
312                 purge_time = ((time_t)config.c_userpurge) * 86400L;
313                 }
314
315         /* The default rule is to not purge. */
316         purge = 0;
317
318         /* If the user hasn't called in two months, his/her account
319          * has expired, so purge the record.
320          */
321         now = time(NULL);
322         if ((now - us->lastcall) > purge_time) purge = 1;
323
324         /* If the user set his/her password to 'deleteme', he/she
325          * wishes to be deleted, so purge the record.
326          */
327         if (!strcasecmp(us->password, "deleteme")) purge = 1;
328
329         /* If the record is marked as permanent, don't purge it.
330          */
331         if (us->flags & US_PERM) purge = 0;
332
333         /* If the access level is 0, the record should already have been
334          * deleted, but maybe the user was logged in at the time or something.
335          * Delete the record now.
336          */
337         if (us->axlevel == 0) purge = 1;
338
339         /* 0 calls is impossible.  If there are 0 calls, it must
340          * be a corrupted record, so purge it.
341          */
342         if (us->timescalled == 0) purge = 1;
343
344         if (purge == 1) {
345                 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
346                 pptr->next = UserPurgeList;
347                 strcpy(pptr->name, us->fullname);
348                 UserPurgeList = pptr;
349                 }
350
351         }
352
353
354
355 int PurgeUsers(void) {
356         struct PurgeList *pptr;
357         int num_users_purged = 0;
358         char *transcript = NULL;
359
360         lprintf(5, "PurgeUsers() called\n");
361         if (config.c_userpurge > 0) {
362                 ForEachUser(do_user_purge);
363                 }
364
365         transcript = mallok(256);
366         strcpy(transcript, "The following users have been auto-purged:\n");
367
368         while (UserPurgeList != NULL) {
369                 transcript=reallok(transcript, strlen(transcript)+256);
370                 sprintf(&transcript[strlen(transcript)], " %s\n",
371                         UserPurgeList->name);
372                 purge_user(UserPurgeList->name);
373                 pptr = UserPurgeList->next;
374                 phree(UserPurgeList);
375                 UserPurgeList = pptr;
376                 ++num_users_purged;
377                 }
378
379         if (num_users_purged > 0) aide_message(transcript);
380         phree(transcript);
381
382         lprintf(5, "Purged %d users.\n", num_users_purged);
383         return(num_users_purged);
384         }
385
386
387 /*
388  * Purge visits
389  *
390  * This is a really cumbersome "garbage collection" function.  We have to
391  * delete visits which refer to rooms and/or users which no longer exist.  In
392  * order to prevent endless traversals of the room and user files, we first
393  * build linked lists of rooms and users which _do_ exist on the system, then
394  * traverse the visit file, checking each record against those two lists and
395  * purging the ones that do not have a match on _both_ lists.  (Remember, if
396  * either the room or user being referred to is no longer on the system, the
397  * record is completely useless.)
398  */
399 int PurgeVisits(void) {
400         struct cdbdata *cdbvisit;
401         struct visit vbuf;
402         struct VPurgeList *VisitPurgeList = NULL;
403         struct VPurgeList *vptr;
404         int purged = 0;
405         char IndexBuf[32];
406         int IndexLen;
407         struct ValidRoom *vrptr;
408         struct ValidUser *vuptr;
409         int RoomIsValid, UserIsValid;
410
411         /* First, load up a table full of valid room/gen combinations */
412         ForEachRoom(AddValidRoom);
413
414         /* Then load up a table full of valid user numbers */
415         ForEachUser(AddValidUser);
416
417         /* Now traverse through the visits, purging irrelevant records... */
418         cdb_rewind(CDB_VISIT);
419         while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
420                 memset(&vbuf, 0, sizeof(struct visit));
421                 memcpy(&vbuf, cdbvisit->ptr,
422                         ( (cdbvisit->len > sizeof(struct visit)) ?
423                         sizeof(struct visit) : cdbvisit->len) );
424                 cdb_free(cdbvisit);
425
426                 RoomIsValid = 0;
427                 UserIsValid = 0;
428
429                 /* Check to see if the room exists */
430                 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
431                         if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
432                              && (vrptr->vr_roomgen==vbuf.v_roomgen))
433                                 RoomIsValid = 1;
434                         }
435
436                 /* Check to see if the user exists */
437                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
438                         if (vuptr->vu_usernum == vbuf.v_usernum)
439                                 UserIsValid = 1;
440                         }
441
442                 /* Put the record on the purge list if it's dead */
443                 if ((RoomIsValid==0) || (UserIsValid==0)) {
444                         vptr = (struct VPurgeList *)
445                                 mallok(sizeof(struct VPurgeList));
446                         vptr->next = VisitPurgeList;
447                         vptr->vp_roomnum = vbuf.v_roomnum;
448                         vptr->vp_roomgen = vbuf.v_roomgen;
449                         vptr->vp_usernum = vbuf.v_usernum;
450                         VisitPurgeList = vptr;
451                         }
452
453                 }
454
455         /* Free the valid room/gen combination list */
456         while (ValidRoomList != NULL) {
457                 vrptr = ValidRoomList->next;
458                 phree(ValidRoomList);
459                 ValidRoomList = vrptr;
460                 }
461
462         /* Free the valid user list */
463         while (ValidUserList != NULL) {
464                 vuptr = ValidUserList->next;
465                 phree(ValidUserList);
466                 ValidUserList = vuptr;
467                 }
468
469         /* Now delete every visit on the purged list */
470         while (VisitPurgeList != NULL) {
471                 IndexLen = GenerateRelationshipIndex(IndexBuf,
472                                 VisitPurgeList->vp_roomnum,
473                                 VisitPurgeList->vp_roomgen,
474                                 VisitPurgeList->vp_usernum);
475                 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
476                 vptr = VisitPurgeList->next;
477                 phree(VisitPurgeList);
478                 VisitPurgeList = vptr;
479                 ++purged;
480                 }
481         
482         return(purged);
483         }
484
485
486 void cmd_expi(char *argbuf) {
487         char cmd[256];
488         int retval;
489
490
491         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
492                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
493                 return;
494                 }
495
496         if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
497                 cprintf("%d Higher access required.\n",
498                         ERROR+HIGHER_ACCESS_REQUIRED);
499                 return;
500                 }
501
502         extract(cmd, argbuf, 0);
503         if (!strcasecmp(cmd, "users")) {
504                 retval = PurgeUsers();
505                 cprintf("%d Purged %d users.\n", OK, retval);
506                 return;
507                 }
508         else if (!strcasecmp(cmd, "messages")) {
509                 PurgeMessages();
510                 cprintf("%d Expired %d messages.\n", OK, messages_purged);
511                 return;
512                 }
513         else if (!strcasecmp(cmd, "rooms")) {
514                 retval = PurgeRooms();
515                 cprintf("%d Expired %d rooms.\n", OK, retval);
516                 return;
517                 }
518         else if (!strcasecmp(cmd, "visits")) {
519                 retval = PurgeVisits();
520                 cprintf("%d Purged %d visits.\n", OK, retval);
521                 }
522         else if (!strcasecmp(cmd, "defrag")) {
523                 defrag_databases();
524                 cprintf("%d Defragmented the databases.\n", OK);
525                 }
526         else {
527                 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
528                 return;
529                 }
530         }
531
532
533
534 struct DLModule_Info *Dynamic_Module_Init(void)
535 {
536    CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
537    return &info;
538 }