]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/expire/serv_expire.c
Messages in the Local System Configuration room are now
[citadel.git] / citadel / modules / expire / serv_expire.c
index b494d9755495cd513c6ca5856464cd9f2a0a6047..ab82d2828cfa9708d35276f7fcec957d8f1e269f 100644 (file)
  *
  * When using Berkeley DB, there's another reason for the two-phase purge: we
  * don't want the entire thing being done as one huge transaction.
+ *
+ * You'll also notice that we build the in-memory list of records to be deleted
+ * sometimes with a linked list and sometimes with a hash table.  There is no
+ * reason for this aside from the fact that the linked list ones were written
+ * before we had the hash table library available.
  */
 
 
@@ -115,6 +120,7 @@ struct ValidUser *ValidUserList = NULL;
 int messages_purged;
 int users_not_purged;
 char *users_corrupt_msg = NULL;
+char *users_zero_msg = NULL;
 
 struct ctdlroomref *rr = NULL;
 
@@ -146,6 +152,10 @@ void GatherPurgeMessages(struct ctdlroom *qrbuf, void *data) {
        if (epbuf.expire_mode == EXPIRE_NEXTLEVEL) return;
        if (epbuf.expire_mode == EXPIRE_MANUAL) return;
 
+       /* Don't purge messages containing system configuration, dumbass. */
+       if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
+
+       /* Ok, we got this far ... now let's see what's in the room */
         cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
 
         if (cdbfr != NULL) {
@@ -284,6 +294,7 @@ void DoPurgeRooms(struct ctdlroom *qrbuf, void *data) {
                if (qrbuf->QRflags & QR_PERMANENT) return;
                if (qrbuf->QRflags & QR_DIRECTORY) return;
                if (qrbuf->QRflags & QR_NETWORK) return;
+               if (qrbuf->QRflags2 & QR2_SYSTEM) return;
                if (!strcasecmp(qrbuf->QRname, SYSCONFIGROOM)) return;
                if (is_noneditable(qrbuf)) return;
 
@@ -440,30 +451,47 @@ 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
+       /* any negative user number, is
         * also impossible.
         */
-       if (us->usernum < 1L) purge = 1;
+       if (us->usernum < 0L) purge = 1;
+       
+       /** Don't purge user 0. That user is there for the system */
+       if (us->usernum == 0L)
+       {
+               /* FIXME: Temporary log message. Until we do unauth access with user 0 we should
+                * try to get rid of all user 0 occurences. Many will be remnants from old code so
+                * we will need to try and purge them from users data bases.Some will not have names but
+                * those with names should be purged.
+                */
+               CtdlLogPrintf(CTDL_DEBUG, "Auto purger found a user 0 with name \"%s\"\n", us->fullname);
+               // purge = 0;
+       }
        
        /* If the user has no full name entry then we can't purge them
         * since the actual purge can't find them.
         * This shouldn't happen but does somehow.
-        * So we make an Aide message to alert to it but don't add it to the purge list
         */
        if (IsEmptyStr(us->fullname))
        {
-               purge=0;
-               if (users_corrupt_msg == NULL)
+               purge = 0;
+               
+               if (us->usernum > 0L)
                {
-                       users_corrupt_msg = malloc(SIZ);
-                       strcpy(users_corrupt_msg, "The auto-purger found the following user numbers with no name.\n"
-                       "Unfortunately the auto-purger is not yet able to fix this problem.\n"
-                       "This problem is not considered serious since a user with no name can\n"
-                       "not log in.\n");
-               }
+                       purge=0;
+                       if (users_corrupt_msg == NULL)
+                       {
+                               users_corrupt_msg = malloc(SIZ);
+                               strcpy(users_corrupt_msg, "The auto-purger found the following user numbers with no name.\n"
+                               "The system has no way to purge user with no name and should not be able to\n"
+                               "create them either.\n"
+                               "This indicates corruption of the user DB or possibly a bug.\n"
+                               "It may be a good idea to restore your DB from a backup.\n");
+                       }
                
-               users_corrupt_msg=realloc(users_corrupt_msg, strlen(users_corrupt_msg)+SIZ);
-               snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], SIZ, " %ld\n", us->usernum);
+                       users_corrupt_msg=realloc(users_corrupt_msg, strlen(users_corrupt_msg)+30);
+                       snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], 29, " %ld\n", us->usernum);
+               }
        }
 
 
@@ -540,6 +568,12 @@ int PurgeUsers(void) {
                users_corrupt_msg = NULL;
        }
        
+       if(users_zero_msg)
+       {
+               aide_message(users_zero_msg, "User Zero Message");
+               free (users_zero_msg);
+               users_zero_msg = NULL;
+       }
                
        CtdlLogPrintf(CTDL_DEBUG, "Purged %d users.\n", num_users_purged);
        return(num_users_purged);
@@ -751,6 +785,57 @@ int PurgeEuidIndexTable(void) {
 }
 
 
+
+/*
+ * Purge OpenID assocations for missing users (theoretically this will never delete anything)
+ */
+int PurgeStaleOpenIDassociations(void) {
+       struct cdbdata *cdboi;
+       struct ctdluser usbuf;
+       HashList *keys = NULL;
+       HashPos *HashPos;
+       char *deleteme = NULL;
+       long len;
+       void *Value;
+       char *Key;
+       int num_deleted = 0;
+
+       keys = NewHash(1, NULL);
+       if (!keys) return(0);
+
+
+       cdb_rewind(CDB_OPENID);
+       while (cdboi = cdb_next_item(CDB_OPENID), cdboi != NULL) {
+               if (cdboi->len > sizeof(long)) {
+                       long usernum;
+                       usernum = ((long)*(cdboi->ptr));
+                       if (getuserbynumber(&usbuf, usernum) != 0) {
+                               deleteme = strdup(cdboi->ptr + sizeof(long)),
+                               Put(keys, deleteme, strlen(deleteme), deleteme, generic_free_handler);
+                       }
+               }
+               cdb_free(cdboi);
+       }
+
+       /* Go through the hash list, deleting keys we stored in it */
+
+       HashPos = GetNewHashPos();
+       while (GetNextHashPos(keys, HashPos, &len, &Key, &Value)!=0)
+       {
+               CtdlLogPrintf(CTDL_DEBUG, "Deleting associated OpenID <%s>\n", Value);
+               cdb_delete(CDB_OPENID, Value, strlen(Value));
+               /* note: don't free(Value) -- deleting the hash list will handle this for us */
+               ++num_deleted;
+       }
+       DeleteHashPos(&HashPos);
+       DeleteHash(&keys);
+       return num_deleted;
+}
+
+
+
+
+
 void *purge_databases(void *args)
 {
         int retval;
@@ -761,10 +846,8 @@ void *purge_databases(void *args)
 
        CtdlLogPrintf(CTDL_DEBUG, "Auto-purger_thread() initializing\n");
 
-       memset(&purgerCC, 0, sizeof(struct CitContext));
-       purgerCC.internal_pgm = 1;
-       purgerCC.cs_pid = 0;
-       pthread_setspecific(MyConKey, (void *)&purgerCC );
+       CtdlFillSystemContext(&purgerCC, "purger");
+       citthread_setspecific(MyConKey, (void *)&purgerCC );
 
         while (!CtdlThreadCheckStop()) {
                 /* Do the auto-purge if the current hour equals the purge hour,
@@ -817,6 +900,12 @@ void *purge_databases(void *args)
                        CtdlLogPrintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval);
                }
 
+               if (!CtdlThreadCheckStop())
+               {
+                       retval = PurgeStaleOpenIDassociations();
+                       CtdlLogPrintf(CTDL_NOTICE, "Purged %d stale OpenID associations.\n", retval);
+               }
+
                if (!CtdlThreadCheckStop())
                {
                        retval = TDAP_ProcessAdjRefCountQueue();