]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_expire.c
mk_module_init.sh now tests to see if echo supports -e and -E
[citadel.git] / citadel / serv_expire.c
index 2c7249da495f68ffa70ea17f8d312280c729e1f3..66b46570d54153bd0b202af2b454ca053ceb2b94 100644 (file)
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "room_ops.h"
 #include "policy.h"
 #include "database.h"
@@ -66,6 +64,9 @@
 #include "tools.h"
 
 
+#include "ctdl_module.h"
+
+
 struct PurgeList {
        struct PurgeList *next;
        char name[ROOMNAMELEN]; /* use the larger of username or roomname */
@@ -97,7 +98,13 @@ struct ctdlroomref {
 
 struct UPurgeList {
        struct UPurgeList *next;
-       char up_key[SIZ];
+       char up_key[256];
+};
+
+struct EPurgeList {
+       struct EPurgeList *next;
+       int ep_keylen;
+       char *ep_key;
 };
 
 
@@ -106,6 +113,7 @@ struct PurgeList *RoomPurgeList = NULL;
 struct ValidRoom *ValidRoomList = NULL;
 struct ValidUser *ValidUserList = NULL;
 int messages_purged;
+int users_not_purged;
 
 struct ctdlroomref *rr = NULL;
 
@@ -120,7 +128,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        struct ExpirePolicy epbuf;
        long delnum;
        time_t xtime, now;
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        int a;
         struct cdbdata *cdbfr;
        long *msglist = NULL;
@@ -140,7 +148,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
 
         if (cdbfr != NULL) {
-               msglist = mallok(cdbfr->len);
+               msglist = malloc(cdbfr->len);
                memcpy(msglist, cdbfr->ptr, cdbfr->len);
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
@@ -148,7 +156,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
 
        /* Nothing to do if there aren't any messages */
        if (num_msgs == 0) {
-               if (msglist != NULL) phree(msglist);
+               if (msglist != NULL) free(msglist);
                return;
        }
 
@@ -167,7 +175,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
                for (a=0; a<num_msgs; ++a) {
                        delnum = msglist[a];
 
-                       msg = CtdlFetchMessage(delnum);
+                       msg = CtdlFetchMessage(delnum, 0); /* dont need body */
                        if (msg != NULL) {
                                xtime = atol(msg->cm_fields['T']);
                                CtdlFreeMessage(msg);
@@ -183,7 +191,7 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
                }
        }
 
-       if (msglist != NULL) phree(msglist);
+       if (msglist != NULL) free(msglist);
 }
 
 
@@ -206,7 +214,7 @@ void DoPurgeMessages(FILE *purgelist) {
                if (!strncasecmp(buf, "m=", 2)) {
                        msgnum = atol(&buf[2]);
                        if (msgnum > 0L) {
-                               CtdlDeleteMessages(roomname, msgnum, "");
+                               CtdlDeleteMessages(roomname, &msgnum, 1, "");
                        }
                }
        }
@@ -235,7 +243,7 @@ void PurgeMessages(void) {
 void AddValidUser(struct ctdluser *usbuf, void *data) {
        struct ValidUser *vuptr;
 
-       vuptr = (struct ValidUser *)mallok(sizeof(struct ValidUser));
+       vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
        vuptr->next = ValidUserList;
        vuptr->vu_usernum = usbuf->usernum;
        ValidUserList = vuptr;
@@ -244,7 +252,7 @@ void AddValidUser(struct ctdluser *usbuf, void *data) {
 void AddValidRoom(struct ctdlroom *qrbuf, void *data) {
        struct ValidRoom *vrptr;
 
-       vrptr = (struct ValidRoom *)mallok(sizeof(struct ValidRoom));
+       vrptr = (struct ValidRoom *)malloc(sizeof(struct ValidRoom));
        vrptr->next = ValidRoomList;
        vrptr->vr_roomnum = qrbuf->QRnumber;
        vrptr->vr_roomgen = qrbuf->QRgen;
@@ -293,7 +301,7 @@ void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
        } /* !QR_MAILBOX */
 
        if (do_purge) {
-               pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
+               pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = RoomPurgeList;
                strcpy(pptr->name, qrbuf->QRname);
                RoomPurgeList = pptr;
@@ -323,52 +331,67 @@ int PurgeRooms(void) {
        /* Free the valid user list */
        while (ValidUserList != NULL) {
                vuptr = ValidUserList->next;
-               phree(ValidUserList);
+               free(ValidUserList);
                ValidUserList = vuptr;
        }
 
 
-       transcript = mallok(SIZ);
+       transcript = malloc(SIZ);
        strcpy(transcript, "The following rooms have been auto-purged:\n");
 
        while (RoomPurgeList != NULL) {
                if (getroom(&qrbuf, RoomPurgeList->name) == 0) {
-                       transcript=reallok(transcript, strlen(transcript)+SIZ);
+                       transcript=realloc(transcript, strlen(transcript)+SIZ);
                        snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
                                qrbuf.QRname);
                        delete_room(&qrbuf);
                }
                pptr = RoomPurgeList->next;
-               phree(RoomPurgeList);
+               free(RoomPurgeList);
                RoomPurgeList = pptr;
                ++num_rooms_purged;
        }
 
-       if (num_rooms_purged > 0) aide_message(transcript);
-       phree(transcript);
+       if (num_rooms_purged > 0) aide_message(transcript, "Room Autopurger Message");
+       free(transcript);
 
        lprintf(CTDL_DEBUG, "Purged %d rooms.\n", num_rooms_purged);
        return(num_rooms_purged);
 }
 
 
+/*
+ * Back end function to check user accounts for associated Unix accounts
+ * which no longer exist.  (Only relevant for host auth mode.)
+ */
+void do_uid_user_purge(struct ctdluser *us, void *data) {
+       struct PurgeList *pptr;
+
+       if ((us->uid != (-1)) && (us->uid != CTDLUID)) {
+               if (getpwuid(us->uid) == NULL) {
+                       pptr = (struct PurgeList *)
+                               malloc(sizeof(struct PurgeList));
+                       pptr->next = UserPurgeList;
+                       strcpy(pptr->name, us->fullname);
+                       UserPurgeList = pptr;
+               }
+       }
+       else {
+               ++users_not_purged;
+       }
+}
+
+
+
+/*
+ * Back end function to check user accounts for expiration.
+ */
 void do_user_purge(struct ctdluser *us, void *data) {
        int purge;
        time_t now;
        time_t purge_time;
        struct PurgeList *pptr;
 
-       /* stupid recovery routine to re-create missing mailboxen.
-        * don't enable this.
-       struct ctdlroom qrbuf;
-       char mailboxname[ROOMNAMELEN];
-       MailboxName(mailboxname, us, MAILROOM);
-       create_room(mailboxname, 4, "", 0, 1, 1);
-       if (getroom(&qrbuf, mailboxname) != 0) return;
-       lprintf(CTDL_DEBUG, "Got %s\n", qrbuf.QRname);
-        */
-
-
        /* Set purge time; if the user overrides the system default, use it */
        if (us->USuserpurge > 0) {
                purge_time = ((time_t)us->USuserpurge) * 86400L;
@@ -395,6 +418,10 @@ void do_user_purge(struct ctdluser *us, void *data) {
         */
        if (us->flags & US_PERM) purge = 0;
 
+       /* If the user is an Aide, don't purge him/her/it.
+        */
+       if (us->axlevel == 6) purge = 0;
+
        /* If the access level is 0, the record should already have been
         * deleted, but maybe the user was logged in at the time or something.
         * Delete the record now.
@@ -406,12 +433,20 @@ void do_user_purge(struct ctdluser *us, void *data) {
         */
        if (us->timescalled == 0) purge = 1;
 
+       /* User number 0, as well as any negative user number, is
+        * also impossible.
+        */
+       if (us->usernum < 1L) purge = 1;
+
        if (purge == 1) {
-               pptr = (struct PurgeList *) mallok(sizeof(struct PurgeList));
+               pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = UserPurgeList;
                strcpy(pptr->name, us->fullname);
                UserPurgeList = pptr;
        }
+       else {
+               ++users_not_purged;
+       }
 
 }
 
@@ -423,26 +458,50 @@ int PurgeUsers(void) {
        char *transcript = NULL;
 
        lprintf(CTDL_DEBUG, "PurgeUsers() called\n");
-       if (config.c_userpurge > 0) {
-               ForEachUser(do_user_purge, NULL);
+       users_not_purged = 0;
+
+       if (config.c_auth_mode == 1) {
+               /* host auth mode */
+               ForEachUser(do_uid_user_purge, NULL);
+       }
+       else {
+               /* native auth mode */
+               if (config.c_userpurge > 0) {
+                       ForEachUser(do_user_purge, NULL);
+               }
        }
 
-       transcript = mallok(SIZ);
-       strcpy(transcript, "The following users have been auto-purged:\n");
+       transcript = malloc(SIZ);
+
+       if (users_not_purged == 0) {
+               strcpy(transcript, "The auto-purger was told to purge every user.  It is\n"
+                               "refusing to do this because it usually indicates a problem\n"
+                               "such as an inability to communicate with a name service.\n"
+               );
+               while (UserPurgeList != NULL) {
+                       pptr = UserPurgeList->next;
+                       free(UserPurgeList);
+                       UserPurgeList = pptr;
+                       ++num_users_purged;
+               }
+       }
 
-       while (UserPurgeList != NULL) {
-               transcript=reallok(transcript, strlen(transcript)+SIZ);
-               snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
-                       UserPurgeList->name);
-               purge_user(UserPurgeList->name);
-               pptr = UserPurgeList->next;
-               phree(UserPurgeList);
-               UserPurgeList = pptr;
-               ++num_users_purged;
+       else {
+               strcpy(transcript, "The following users have been auto-purged:\n");
+               while (UserPurgeList != NULL) {
+                       transcript=realloc(transcript, strlen(transcript)+SIZ);
+                       snprintf(&transcript[strlen(transcript)], SIZ, " %s\n",
+                               UserPurgeList->name);
+                       purge_user(UserPurgeList->name);
+                       pptr = UserPurgeList->next;
+                       free(UserPurgeList);
+                       UserPurgeList = pptr;
+                       ++num_users_purged;
+               }
        }
 
-       if (num_users_purged > 0) aide_message(transcript);
-       phree(transcript);
+       if (num_users_purged > 0) aide_message(transcript, "User Purge Message");
+       free(transcript);
 
        lprintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
        return(num_users_purged);
@@ -507,7 +566,7 @@ int PurgeVisits(void) {
                /* Put the record on the purge list if it's dead */
                if ((RoomIsValid==0) || (UserIsValid==0)) {
                        vptr = (struct VPurgeList *)
-                               mallok(sizeof(struct VPurgeList));
+                               malloc(sizeof(struct VPurgeList));
                        vptr->next = VisitPurgeList;
                        vptr->vp_roomnum = vbuf.v_roomnum;
                        vptr->vp_roomgen = vbuf.v_roomgen;
@@ -520,14 +579,14 @@ int PurgeVisits(void) {
        /* Free the valid room/gen combination list */
        while (ValidRoomList != NULL) {
                vrptr = ValidRoomList->next;
-               phree(ValidRoomList);
+               free(ValidRoomList);
                ValidRoomList = vrptr;
        }
 
        /* Free the valid user list */
        while (ValidUserList != NULL) {
                vuptr = ValidUserList->next;
-               phree(ValidUserList);
+               free(ValidUserList);
                ValidUserList = vuptr;
        }
 
@@ -539,7 +598,7 @@ int PurgeVisits(void) {
                                VisitPurgeList->vp_usernum);
                cdb_delete(CDB_VISIT, IndexBuf, IndexLen);
                vptr = VisitPurgeList->next;
-               phree(VisitPurgeList);
+               free(VisitPurgeList);
                VisitPurgeList = vptr;
                ++purged;
        }
@@ -569,10 +628,10 @@ int PurgeUseTable(void) {
                 cdb_free(cdbut);
 
                if ( (time(NULL) - ut.ut_timestamp) > USETABLE_RETAIN ) {
-                       uptr = (struct UPurgeList *) mallok(sizeof(struct UPurgeList));
+                       uptr = (struct UPurgeList *) malloc(sizeof(struct UPurgeList));
                        if (uptr != NULL) {
                                uptr->next = ul;
-                               safestrncpy(uptr->up_key, ut.ut_msgid, SIZ);
+                               safestrncpy(uptr->up_key, ut.ut_msgid, sizeof uptr->up_key);
                                ul = uptr;
                        }
                        ++purged;
@@ -585,7 +644,7 @@ int PurgeUseTable(void) {
        while (ul != NULL) {
                cdb_delete(CDB_USETABLE, ul->up_key, strlen(ul->up_key));
                uptr = ul->next;
-               phree(ul);
+               free(ul);
                ul = uptr;
        }
 
@@ -594,6 +653,61 @@ int PurgeUseTable(void) {
 }
 
 
+
+/*
+ * Purge the EUID Index of old records.
+ *
+ */
+int PurgeEuidIndexTable(void) {
+       int purged = 0;
+       struct cdbdata *cdbei;
+       struct EPurgeList *el = NULL;
+       struct EPurgeList *eptr; 
+       long msgnum;
+       struct CtdlMessage *msg = NULL;
+
+       /* Phase 1: traverse through the table, discovering old records... */
+       lprintf(CTDL_DEBUG, "Purge EUID index: phase 1\n");
+       cdb_rewind(CDB_EUIDINDEX);
+       while(cdbei = cdb_next_item(CDB_EUIDINDEX), cdbei != NULL) {
+
+               memcpy(&msgnum, cdbei->ptr, sizeof(long));
+
+               msg = CtdlFetchMessage(msgnum, 0);
+               if (msg != NULL) {
+                       CtdlFreeMessage(msg);   /* it still exists, so do nothing */
+               }
+               else {
+                       eptr = (struct EPurgeList *) malloc(sizeof(struct EPurgeList));
+                       if (eptr != NULL) {
+                               eptr->next = el;
+                               eptr->ep_keylen = cdbei->len - sizeof(long);
+                               eptr->ep_key = malloc(cdbei->len);
+                               memcpy(eptr->ep_key, &cdbei->ptr[sizeof(long)], eptr->ep_keylen);
+                               el = eptr;
+                       }
+                       ++purged;
+               }
+
+                cdb_free(cdbei);
+
+       }
+
+       /* Phase 2: delete the records */
+       lprintf(CTDL_DEBUG, "Purge euid index: phase 2\n");
+       while (el != NULL) {
+               cdb_delete(CDB_EUIDINDEX, el->ep_key, el->ep_keylen);
+               free(el->ep_key);
+               eptr = el->next;
+               free(el);
+               el = eptr;
+       }
+
+       lprintf(CTDL_DEBUG, "Purge euid index: finished (purged %d records)\n", purged);
+       return(purged);
+}
+
+
 void purge_databases(void) {
        int retval;
        static time_t last_purge = 0;
@@ -605,7 +719,7 @@ void purge_databases(void) {
         * last twelve hours.  This is usually enough granularity.
         */
        now = time(NULL);
-       memcpy(&tm, localtime(&now), sizeof(struct tm));
+       localtime_r(&now, &tm);
        if (tm.tm_hour != config.c_purge_hour) return;
        if ((now - last_purge) < 43200) return;
 
@@ -626,6 +740,12 @@ void purge_databases(void) {
        retval = PurgeUseTable();
        lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval);
 
+       retval = PurgeEuidIndexTable();
+       lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
+
+       retval = TDAP_ProcessAdjRefCountQueue();
+       lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval);
+
        lprintf(CTDL_INFO, "Auto-purger: finished.\n");
 
        last_purge = now;       /* So we don't do it again soon */
@@ -637,7 +757,7 @@ void purge_databases(void) {
 void do_fsck_msg(long msgnum, void *userdata) {
        struct ctdlroomref *ptr;
 
-       ptr = (struct ctdlroomref *)mallok(sizeof(struct ctdlroomref));
+       ptr = (struct ctdlroomref *)malloc(sizeof(struct ctdlroomref));
        ptr->next = rr;
        ptr->msgnum = msgnum;
        rr = ptr;
@@ -646,7 +766,7 @@ void do_fsck_msg(long msgnum, void *userdata) {
 void do_fsck_room(struct ctdlroom *qrbuf, void *data)
 {
        getroom(&CC->room, qrbuf->QRname);
-       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, do_fsck_msg, NULL);
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, do_fsck_msg, NULL);
 }
 
 /*
@@ -693,9 +813,7 @@ void cmd_fsck(char *argbuf) {
 
                        if ( (smi.meta_refcount != realcount)
                           || (realcount == 0) ) {
-                               smi.meta_refcount = realcount;
-                               PutMetaData(&smi);
-                               AdjRefCount(msgnum, 0); /* deletes if needed */
+                               AdjRefCount(msgnum, (smi.meta_refcount - realcount));
                        }
 
                }
@@ -705,7 +823,7 @@ void cmd_fsck(char *argbuf) {
        cprintf("Freeing memory...\n");
        while (rr != NULL) {
                ptr = rr->next;
-               phree(rr);
+               free(rr);
                rr = ptr;
        }
 
@@ -719,9 +837,11 @@ void cmd_fsck(char *argbuf) {
 
 /*****************************************************************************/
 
-char *serv_expire_init(void)
+CTDL_MODULE_INIT(expire)
 {
        CtdlRegisterSessionHook(purge_databases, EVT_TIMER);
        CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts");
+
+       /* return our Subversion id for the Log */
        return "$Id$";
 }