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