]> code.citadel.org Git - citadel.git/blob - citadel/serv_expire.c
* Added a generic (void *) parameter to the ForEachUser() and ForEachRoom()
[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  * $Id$
7  */
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 #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 void DoPurgeMessages(struct quickroom *qrbuf, void *data) {
96         struct ExpirePolicy epbuf;
97         long delnum;
98         time_t xtime, now;
99         struct CtdlMessage *msg;
100         int a;
101         struct cdbdata *cdbfr;
102         long *msglist = NULL;
103         int num_msgs = 0;
104
105         time(&now);
106         GetExpirePolicy(&epbuf, qrbuf);
107         
108         /* If the room is set to never expire messages ... do nothing */
109         if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
110         if (epbuf.expire_mode == EXPIRE_MANUAL) return;
111
112         begin_critical_section(S_QUICKROOM);
113         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
114
115         if (cdbfr != NULL) {
116                 msglist = mallok(cdbfr->len);
117                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
118                 num_msgs = cdbfr->len / sizeof(long);
119                 cdb_free(cdbfr);
120         }
121         
122         /* Nothing to do if there aren't any messages */
123         if (num_msgs == 0) {
124                 end_critical_section(S_QUICKROOM);
125                 return;
126         }
127
128         /* If the room is set to expire by count, do that */
129         if (epbuf.expire_mode == EXPIRE_NUMMSGS) {
130                 while (num_msgs > epbuf.expire_value) {
131                         delnum = msglist[0];
132                         lprintf(5, "Expiring message %ld\n", delnum);
133                         AdjRefCount(delnum, -1); 
134                         memcpy(&msglist[0], &msglist[1],
135                                 (sizeof(long)*(num_msgs - 1)));
136                         --num_msgs;
137                         ++messages_purged;
138                 }
139         }
140
141         /* If the room is set to expire by age... */
142         if (epbuf.expire_mode == EXPIRE_AGE) {
143                 for (a=0; a<num_msgs; ++a) {
144                         delnum = msglist[a];
145
146                         msg = CtdlFetchMessage(delnum);
147                         if (msg != NULL) {
148                                 xtime = atol(msg->cm_fields['T']);
149                                 CtdlFreeMessage(msg);
150                         } else {
151                                 xtime = 0L;
152                         }
153
154                         if ((xtime > 0L)
155                            && (now - xtime > (time_t)(epbuf.expire_value * 86400L))) {
156                                 lprintf(5, "Expiring message %ld\n", delnum);
157                                 AdjRefCount(delnum, -1); 
158                                 msglist[a] = 0L;
159                                 ++messages_purged;
160                         }
161                 }
162         }
163
164         if (num_msgs > 0) {
165                 num_msgs = sort_msglist(msglist, num_msgs);
166         }
167         
168         cdb_store(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long),
169                 msglist, (num_msgs * sizeof(long)) );
170
171         if (msglist != NULL) phree(msglist);
172
173         end_critical_section(S_QUICKROOM);
174 }
175
176 void PurgeMessages(void) {
177         lprintf(5, "PurgeMessages() called\n");
178         messages_purged = 0;
179         ForEachRoom(DoPurgeMessages, NULL);
180 }
181
182
183 void AddValidUser(struct usersupp *usbuf, void *data) {
184         struct ValidUser *vuptr;
185
186         vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
187         vuptr->next = ValidUserList;
188         vuptr->vu_usernum = usbuf->usernum;
189         ValidUserList = vuptr;
190 }
191
192 void AddValidRoom(struct quickroom *qrbuf, void *data) {
193         struct ValidRoom *vrptr;
194
195         vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
196         vrptr->next = ValidRoomList;
197         vrptr->vr_roomnum = qrbuf->QRnumber;
198         vrptr->vr_roomgen = qrbuf->QRgen;
199         ValidRoomList = vrptr;
200 }
201
202 void DoPurgeRooms(struct quickroom *qrbuf, void *data) {
203         time_t age, purge_secs;
204         struct PurgeList *pptr;
205         struct ValidUser *vuptr;
206         int do_purge = 0;
207
208         /* For mailbox rooms, there's only one purging rule: if the user who
209          * owns the room still exists, we keep the room; otherwise, we purge
210          * it.  Bypass any other rules.
211          */
212         if (qrbuf->QRflags & QR_MAILBOX) {
213                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
214                         if (vuptr->vu_usernum == atol(qrbuf->QRname)) {
215                                 do_purge = 0;
216                                 goto BYPASS;
217                         }
218                 }
219                 /* user not found */
220                 do_purge = 1;
221                 goto BYPASS;
222         }
223
224         /* Any of these attributes render a room non-purgable */
225         if (qrbuf->QRflags & QR_PERMANENT) return;
226         if (qrbuf->QRflags & QR_DIRECTORY) return;
227         if (qrbuf->QRflags & QR_NETWORK) return;
228         if (is_noneditable(qrbuf)) return;
229
230         /* If we don't know the modification date, be safe and don't purge */
231         if (qrbuf->QRmtime <= (time_t)0) return;
232
233         /* If no room purge time is set, be safe and don't purge */
234         if (config.c_roompurge < 0) return;
235
236         /* Otherwise, check the date of last modification */
237         age = time(NULL) - (qrbuf->QRmtime);
238         purge_secs = (time_t)config.c_roompurge * (time_t)86400;
239         if (purge_secs <= (time_t)0) return;
240         lprintf(9, "<%s> is <%ld> seconds old\n", qrbuf->QRname, age);
241         if (age > purge_secs) do_purge = 1;
242
243 BYPASS: if (do_purge) {
244                 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
245                 pptr->next = RoomPurgeList;
246                 strcpy(pptr->name, qrbuf->QRname);
247                 RoomPurgeList = pptr;
248         }
249
250 }
251         
252
253
254 int PurgeRooms(void) {
255         struct PurgeList *pptr;
256         int num_rooms_purged = 0;
257         struct quickroom qrbuf;
258         struct ValidUser *vuptr;
259         char *transcript = NULL;
260
261         lprintf(5, "PurgeRooms() called\n");
262
263
264         /* Load up a table full of valid user numbers so we can delete
265          * user-owned rooms for users who no longer exist */
266         ForEachUser(AddValidUser, NULL);
267
268         /* Then cycle through the room file */
269         ForEachRoom(DoPurgeRooms, NULL);
270
271         /* Free the valid user list */
272         while (ValidUserList != NULL) {
273                 vuptr = ValidUserList->next;
274                 phree(ValidUserList);
275                 ValidUserList = vuptr;
276         }
277
278
279         transcript = mallok(256);
280         strcpy(transcript, "The following rooms have been auto-purged:\n");
281
282         while (RoomPurgeList != NULL) {
283                 if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
284                         transcript=reallok(transcript, strlen(transcript)+256);
285                         sprintf(&transcript[strlen(transcript)], " %s\n",
286                                 qrbuf.QRname);
287                         delete_room(&qrbuf);
288                 }
289                 pptr = RoomPurgeList->next;
290                 phree(RoomPurgeList);
291                 RoomPurgeList = pptr;
292                 ++num_rooms_purged;
293         }
294
295         if (num_rooms_purged > 0) aide_message(transcript);
296         phree(transcript);
297
298         lprintf(5, "Purged %d rooms.\n", num_rooms_purged);
299         return(num_rooms_purged);
300 }
301
302
303 void do_user_purge(struct usersupp *us, void *data) {
304         int purge;
305         time_t now;
306         time_t purge_time;
307         struct PurgeList *pptr;
308
309         /* Set purge time; if the user overrides the system default, use it */
310         if (us->USuserpurge > 0) {
311                 purge_time = ((time_t)us->USuserpurge) * 86400L;
312         }
313         else {
314                 purge_time = ((time_t)config.c_userpurge) * 86400L;
315         }
316
317         /* The default rule is to not purge. */
318         purge = 0;
319
320         /* If the user hasn't called in two months, his/her account
321          * has expired, so purge the record.
322          */
323         now = time(NULL);
324         if ((now - us->lastcall) > purge_time) purge = 1;
325
326         /* If the user set his/her password to 'deleteme', he/she
327          * wishes to be deleted, so purge the record.
328          */
329         if (!strcasecmp(us->password, "deleteme")) purge = 1;
330
331         /* If the record is marked as permanent, don't purge it.
332          */
333         if (us->flags & US_PERM) purge = 0;
334
335         /* If the access level is 0, the record should already have been
336          * deleted, but maybe the user was logged in at the time or something.
337          * Delete the record now.
338          */
339         if (us->axlevel == 0) purge = 1;
340
341         /* 0 calls is impossible.  If there are 0 calls, it must
342          * be a corrupted record, so purge it.
343          */
344         if (us->timescalled == 0) purge = 1;
345
346         if (purge == 1) {
347                 pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
348                 pptr->next = UserPurgeList;
349                 strcpy(pptr->name, us->fullname);
350                 UserPurgeList = pptr;
351         }
352
353 }
354
355
356
357 int PurgeUsers(void) {
358         struct PurgeList *pptr;
359         int num_users_purged = 0;
360         char *transcript = NULL;
361
362         lprintf(5, "PurgeUsers() called\n");
363         if (config.c_userpurge > 0) {
364                 ForEachUser(do_user_purge, NULL);
365         }
366
367         transcript = mallok(256);
368         strcpy(transcript, "The following users have been auto-purged:\n");
369
370         while (UserPurgeList != NULL) {
371                 transcript=reallok(transcript, strlen(transcript)+256);
372                 sprintf(&transcript[strlen(transcript)], " %s\n",
373                         UserPurgeList->name);
374                 purge_user(UserPurgeList->name);
375                 pptr = UserPurgeList->next;
376                 phree(UserPurgeList);
377                 UserPurgeList = pptr;
378                 ++num_users_purged;
379         }
380
381         if (num_users_purged > 0) aide_message(transcript);
382         phree(transcript);
383
384         lprintf(5, "Purged %d users.\n", num_users_purged);
385         return(num_users_purged);
386 }
387
388
389 /*
390  * Purge visits
391  *
392  * This is a really cumbersome "garbage collection" function.  We have to
393  * delete visits which refer to rooms and/or users which no longer exist.  In
394  * order to prevent endless traversals of the room and user files, we first
395  * build linked lists of rooms and users which _do_ exist on the system, then
396  * traverse the visit file, checking each record against those two lists and
397  * purging the ones that do not have a match on _both_ lists.  (Remember, if
398  * either the room or user being referred to is no longer on the system, the
399  * record is completely useless.)
400  */
401 int PurgeVisits(void) {
402         struct cdbdata *cdbvisit;
403         struct visit vbuf;
404         struct VPurgeList *VisitPurgeList = NULL;
405         struct VPurgeList *vptr;
406         int purged = 0;
407         char IndexBuf[32];
408         int IndexLen;
409         struct ValidRoom *vrptr;
410         struct ValidUser *vuptr;
411         int RoomIsValid, UserIsValid;
412
413         /* First, load up a table full of valid room/gen combinations */
414         ForEachRoom(AddValidRoom, NULL);
415
416         /* Then load up a table full of valid user numbers */
417         ForEachUser(AddValidUser, NULL);
418
419         /* Now traverse through the visits, purging irrelevant records... */
420         cdb_rewind(CDB_VISIT);
421         while(cdbvisit = cdb_next_item(CDB_VISIT), cdbvisit != NULL) {
422                 memset(&vbuf, 0, sizeof(struct visit));
423                 memcpy(&vbuf, cdbvisit->ptr,
424                         ( (cdbvisit->len > sizeof(struct visit)) ?
425                         sizeof(struct visit) : cdbvisit->len) );
426                 cdb_free(cdbvisit);
427
428                 RoomIsValid = 0;
429                 UserIsValid = 0;
430
431                 /* Check to see if the room exists */
432                 for (vrptr=ValidRoomList; vrptr!=NULL; vrptr=vrptr->next) {
433                         if ( (vrptr->vr_roomnum==vbuf.v_roomnum)
434                              && (vrptr->vr_roomgen==vbuf.v_roomgen))
435                                 RoomIsValid = 1;
436                 }
437
438                 /* Check to see if the user exists */
439                 for (vuptr=ValidUserList; vuptr!=NULL; vuptr=vuptr->next) {
440                         if (vuptr->vu_usernum == vbuf.v_usernum)
441                                 UserIsValid = 1;
442                 }
443
444                 /* Put the record on the purge list if it's dead */
445                 if ((RoomIsValid==0) || (UserIsValid==0)) {
446                         vptr = (struct VPurgeList *)
447                                 mallok(sizeof(struct VPurgeList));
448                         vptr->next = VisitPurgeList;
449                         vptr->vp_roomnum = vbuf.v_roomnum;
450                         vptr->vp_roomgen = vbuf.v_roomgen;
451                         vptr->vp_usernum = vbuf.v_usernum;
452                         VisitPurgeList = vptr;
453                 }
454
455         }
456
457         /* Free the valid room/gen combination list */
458         while (ValidRoomList != NULL) {
459                 vrptr = ValidRoomList->next;
460                 phree(ValidRoomList);
461                 ValidRoomList = vrptr;
462         }
463
464         /* Free the valid user list */
465         while (ValidUserList != NULL) {
466                 vuptr = ValidUserList->next;
467                 phree(ValidUserList);
468                 ValidUserList = vuptr;
469         }
470
471         /* Now delete every visit on the purged list */
472         while (VisitPurgeList != NULL) {
473                 IndexLen = GenerateRelationshipIndex(IndexBuf,
474                                 VisitPurgeList->vp_roomnum,
475                                 VisitPurgeList->vp_roomgen,
476                                 VisitPurgeList->vp_usernum);
477                 cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
478                 vptr = VisitPurgeList->next;
479                 phree(VisitPurgeList);
480                 VisitPurgeList = vptr;
481                 ++purged;
482         }
483         
484         return(purged);
485 }
486
487
488 void cmd_expi(char *argbuf) {
489         char cmd[256];
490         int retval;
491
492
493         if ((!(CC->logged_in))&&(!(CC->internal_pgm))) {
494                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
495                 return;
496         }
497
498         if ((!is_room_aide()) && (!(CC->internal_pgm)) ) {
499                 cprintf("%d Higher access required.\n",
500                         ERROR+HIGHER_ACCESS_REQUIRED);
501                 return;
502         }
503
504         extract(cmd, argbuf, 0);
505         if (!strcasecmp(cmd, "users")) {
506                 retval = PurgeUsers();
507                 cprintf("%d Purged %d users.\n", OK, retval);
508                 return;
509         }
510         else if (!strcasecmp(cmd, "messages")) {
511                 PurgeMessages();
512                 cprintf("%d Expired %d messages.\n", OK, messages_purged);
513                 return;
514         }
515         else if (!strcasecmp(cmd, "rooms")) {
516                 retval = PurgeRooms();
517                 cprintf("%d Expired %d rooms.\n", OK, retval);
518                 return;
519         }
520         else if (!strcasecmp(cmd, "visits")) {
521                 retval = PurgeVisits();
522                 cprintf("%d Purged %d visits.\n", OK, retval);
523         }
524         else if (!strcasecmp(cmd, "defrag")) {
525                 defrag_databases();
526                 cprintf("%d Defragmented the databases.\n", OK);
527         }
528         else {
529                 cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
530                 return;
531         }
532 }
533
534
535
536 char *Dynamic_Module_Init(void)
537 {
538         CtdlRegisterProtoHook(cmd_expi, "EXPI", "Expire old system objects");
539         return "$Id$";
540 }