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