Modified the behavior of ForEachUser() to do the two phase load/perform cycle
authorArt Cancro <ajc@citadel.org>
Mon, 5 Aug 2019 02:44:34 +0000 (22:44 -0400)
committerArt Cancro <ajc@citadel.org>
Mon, 5 Aug 2019 02:44:34 +0000 (22:44 -0400)
as part of the function, since so many callbacks were implementing this anyway.

citadel/citserver.h
citadel/control.c
citadel/internet_addressing.c
citadel/modules/ctdlproto/serv_user.c
citadel/modules/expire/serv_expire.c
citadel/modules/migrate/serv_migrate.c
citadel/modules/upgrade/serv_upgrade.c
citadel/user_ops.c
citadel/user_ops.h

index e292ab7a8f1cfb5fa9c37ed87c528f86eb0d9981..adfe476c1866afcd8658947fe7c93923c70eda7c 100644 (file)
@@ -30,7 +30,7 @@ struct RoomProcList {
 };
 struct UserProcList {
        struct UserProcList *next;
-       char user[64];
+       char user[USERNAME_SIZE];
 };
 
 #define CTDLUSERIP      (IsEmptyStr(CC->cs_addr) ?  CC->cs_clientinfo: CC->cs_addr)
index 6f3a1d7ec447bd53b2a92c933db4e79bb9b65c2c..3463de80b3baf0e6e817e1b96d62c9daf52e3297 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This module handles states which are global to the entire server.
  *
- * Copyright (c) 1987-2018 by the citadel.org team
+ * Copyright (c) 1987-2019 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 3.
@@ -96,14 +96,19 @@ void control_find_highest(struct ctdlroom *qrbuf, void *data)
 /*
  * Callback to get highest user number.
  */
-void control_find_user(struct ctdluser *EachUser, void *out_data)
-{
-       if (EachUser->usernum > CtdlGetConfigLong("MMnextuser")) {
+void control_find_user(char *username, void *out_data) {
+       struct ctdluser EachUser;
+
+       if (CtdlGetUser(&EachUser, username) != 0) {
+               return;
+       }
+
+       if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
                syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
-                       EachUser->usernum, CtdlGetConfigLong("MMnextuser"), EachUser->fullname
+                       EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
                );
                if (!sanity_diag_mode) {
-                       CtdlSetConfigLong("MMnextuser", EachUser->usernum);
+                       CtdlSetConfigLong("MMnextuser", EachUser.usernum);
                }
        }
 }
index 33d70451a6f36406c36056d8b91954ce0e728f4e..416586bedeb0e9bd35503e1715aca1dc661737c9 100644 (file)
@@ -2,7 +2,7 @@
  * This file contains functions which handle the mapping of Internet addresses
  * to users on the Citadel system.
  *
- * Copyright (c) 1987-2018 by the citadel.org team
+ * Copyright (c) 1987-2019 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 3.
@@ -1588,58 +1588,23 @@ char *harvest_collected_addresses(struct CtdlMessage *msg) {
 
 /*
  * Helper function for CtdlRebuildDirectoryIndex()
- *
- * Call this function as a ForEachUser backend in order to queue up
- * user names, or call it with a null user to make it do the processing.
- * This allows us to maintain the list as a static instead of passing
- * pointers around.
  */
-void CtdlRebuildDirectoryIndex_backend(struct ctdluser *usbuf, void *data) {
-
-       struct crdib {
-               char name[64];
-               char emails[512];
-       };
-
-       static struct crdib *e = NULL;
-       static int num_e = 0;
-       static int alloc_e = 0;
+void CtdlRebuildDirectoryIndex_backend(char *username, void *data) {
 
-       /* this is the calling mode where we add a user */
+       int j = 0;
+       struct ctdluser usbuf;
 
-       if (usbuf != NULL) {
-               if (num_e >= alloc_e) {
-                       if (alloc_e == 0) {
-                               alloc_e = 100;
-                               e = malloc(sizeof(struct crdib) * alloc_e);
-                       }
-                       else {
-                               alloc_e *= 2;
-                               e = realloc(e, (sizeof(struct crdib) * alloc_e));
-                       }
-               }
-               strcpy(e[num_e].name, usbuf->fullname);
-               strcpy(e[num_e].emails, usbuf->emailaddrs);
-               ++num_e;
+       if (CtdlGetUser(&usbuf, username) != 0) {
                return;
        }
 
-       /* this is the calling mode where we do the processing */
-
-       int i, j;
-       for (i=0; i<num_e; ++i) {
-               if ( (!IsEmptyStr(e[i].name)) && (!IsEmptyStr(e[i].emails)) ) {
-                       for (j=0; j<num_tokens(e[i].emails, '|'); ++j) {
-                               char one_email[512];
-                               extract_token(one_email, e[i].emails, j, '|', sizeof one_email);
-                               CtdlDirectoryAddUser(one_email, e[i].name);
-                       }
+       if ( (!IsEmptyStr(usbuf.fullname)) && (!IsEmptyStr(usbuf.emailaddrs)) ) {
+               for (j=0; j<num_tokens(usbuf.emailaddrs, '|'); ++j) {
+                       char one_email[512];
+                       extract_token(one_email, usbuf.emailaddrs, j, '|', sizeof one_email);
+                       CtdlDirectoryAddUser(one_email, usbuf.fullname);
                }
        }
-       free(e);
-       num_e = 0;
-       alloc_e = 0;
-       return;
 }
 
 
@@ -1650,7 +1615,6 @@ void CtdlRebuildDirectoryIndex(void) {
        syslog(LOG_INFO, "internet_addressing: rebuilding email address directory index");
        cdb_trunc(CDB_DIRECTORY);
        ForEachUser(CtdlRebuildDirectoryIndex_backend, NULL);
-       CtdlRebuildDirectoryIndex_backend(NULL, NULL);
 }
 
 
index 48799de77d955c73e92bae7599d2c4affd8f1c0b..73465ab2647708275fcd3f45c3ab7ba039f2e67c 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  * Server functions which perform operations on user objects.
  *
- * Copyright (c) 1987-2018 by the citadel.org team
+ * Copyright (c) 1987-2019 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License, version 3.
@@ -484,6 +484,40 @@ void cmd_vali(char *v_args)
        cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
 }
 
+
+/*
+ * List one user (this works with cmd_list)
+ */
+void ListThisUser(char *username, void *data)
+{
+       char *searchstring;
+       struct ctdluser usbuf;
+
+       if (CtdlGetUser(&usbuf, username) != 0) {
+               return;
+       }
+
+       searchstring = (char *)data;
+       if (bmstrcasestr(usbuf.fullname, searchstring) == NULL) {
+               return;
+       }
+
+       if (usbuf.axlevel > AxDeleted) {
+               if ((CC->user.axlevel >= AxAideU)
+                   || ((usbuf.flags & US_UNLISTED) == 0)
+                   || ((CC->internal_pgm))) {
+                       cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
+                               usbuf.fullname,
+                               usbuf.axlevel,
+                               usbuf.usernum,
+                               (long)usbuf.lastcall,
+                               usbuf.timescalled,
+                               usbuf.posted);
+               }
+       }
+}
+
+
 /* 
  *  List users (searchstring may be empty to list all users)
  */
index 879975496dc942626a31e90c2692c82e4afe71ce..09e9eab1093fada7ae5418c40d64024448c755b8 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * This module handles the expiry of old messages and the purging of old users.
  *
- * You might also see this module affectionately referred to as the DAP (the Dreaded Auto-Purger).
+ * You might also see this module affectionately referred to as TDAP (The Dreaded Auto-Purger).
  *
- * Copyright (c) 1988-2018 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
+ * Copyright (c) 1988-2019 by citadel.org (Art Cancro, Wilifried Goesgens, and others)
  *
  * This program is open source software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as published
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * A brief technical discussion:
- *
- * Several of the purge operations found in this module operate in two
- * stages: the first stage generates a linked list of objects to be deleted,
- * then the second stage deletes all listed objects from the database.
- *
- * At first glance this may seem cumbersome and unnecessary.  The reason it is
- * implemented in this way is because Berkeley DB, and possibly other backends
- * we may hook into in the future, explicitly do _not_ support the deletion of
- * records from a file while the file is being traversed.  The delete operation
- * will succeed, but the traversal is not guaranteed to visit every object if
- * this is done.  Therefore we utilize the two-stage purge.
- *
- * 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.
  */
 
 
@@ -254,12 +233,17 @@ void PurgeMessages(void) {
 }
 
 
-void AddValidUser(struct ctdluser *usbuf, void *data) {
+void AddValidUser(char *username, void *data) {
        struct ValidUser *vuptr;
+       struct ctdluser usbuf;
+
+       if (CtdlGetUser(&usbuf, username) != 0) {
+               return;
+       }
 
        vuptr = (struct ValidUser *)malloc(sizeof(struct ValidUser));
        vuptr->next = ValidUserList;
-       vuptr->vu_usernum = usbuf->usernum;
+       vuptr->vu_usernum = usbuf.usernum;
        ValidUserList = vuptr;
 }
 
@@ -372,40 +356,23 @@ int PurgeRooms(void) {
 }
 
 
-/*
- * 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) {
+void do_user_purge(char *username, void *data) {
        int purge;
        time_t now;
        time_t purge_time;
        struct PurgeList *pptr;
+       struct ctdluser us;
+
+       if (CtdlGetUser(&us, username) != 0) {
+               return;
+       }
 
        /* Set purge time; if the user overrides the system default, use it */
-       if (us->USuserpurge > 0) {
-               purge_time = ((time_t)us->USuserpurge) * 86400;
+       if (us.USuserpurge > 0) {
+               purge_time = ((time_t)us.USuserpurge) * 86400;
        }
        else {
                purge_time = CtdlGetConfigLong("c_userpurge") * 86400;
@@ -420,49 +387,49 @@ void do_user_purge(struct ctdluser *us, void *data) {
        if (CtdlGetConfigLong("c_userpurge") > 0)
        {
                now = time(NULL);
-               if ((now - us->lastcall) > purge_time) purge = 1;
+               if ((now - us.lastcall) > purge_time) purge = 1;
        }
 
        /* If the record is marked as permanent, don't purge it.
         */
-       if (us->flags & US_PERM) purge = 0;
+       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 (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.
         */
-       if (us->axlevel == 0) purge = 1;
+       if (us.axlevel == 0) purge = 1;
 
        /* If the user set his/her password to 'deleteme', he/she
         * wishes to be deleted, so purge the record.
         * Moved this lower down so that aides and permanent users get purged if they ask to be.
         */
-       if (!strcasecmp(us->password, "deleteme")) purge = 1;
+       if (!strcasecmp(us.password, "deleteme")) purge = 1;
        
        /* 0 calls is impossible.  If there are 0 calls, it must
         * be a corrupted record, so purge it.
         * Actually it is possible if an Aide created the user so now we check for less than 0 (DRW)
         */
-       if (us->timescalled < 0) purge = 1;
+       if (us.timescalled < 0) purge = 1;
 
        /* any negative user number, is
         * also impossible.
         */
-       if (us->usernum < 0L) purge = 1;
+       if (us.usernum < 0L) purge = 1;
        
        /* Don't purge user 0. That user is there for the system */
-       if (us->usernum == 0L)
+       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.
                 */
-               syslog(LOG_DEBUG, "Auto purger found a user 0 with name <%s>", us->fullname);
+               syslog(LOG_DEBUG, "Auto purger found a user 0 with name <%s>", us.fullname);
                // purge = 0;
        }
        
@@ -470,11 +437,11 @@ void do_user_purge(struct ctdluser *us, void *data) {
         * since the actual purge can't find them.
         * This shouldn't happen but does somehow.
         */
-       if (IsEmptyStr(us->fullname))
+       if (IsEmptyStr(us.fullname))
        {
                purge = 0;
                
-               if (us->usernum > 0L)
+               if (us.usernum > 0L)
                {
                        purge=0;
                        if (users_corrupt_msg == NULL)
@@ -490,14 +457,14 @@ void do_user_purge(struct ctdluser *us, void *data) {
                        }
                
                        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);
+                       snprintf(&users_corrupt_msg[strlen(users_corrupt_msg)], 29, " %ld\n", us.usernum);
                }
        }
 
        if (purge == 1) {
                pptr = (struct PurgeList *) malloc(sizeof(struct PurgeList));
                pptr->next = UserPurgeList;
-               strcpy(pptr->name, us->fullname);
+               strcpy(pptr->name, us.fullname);
                UserPurgeList = pptr;
        }
        else {
@@ -519,9 +486,6 @@ int PurgeUsers(void) {
                case AUTHMODE_NATIVE:
                        ForEachUser(do_user_purge, NULL);
                        break;
-               case AUTHMODE_HOST:
-                       ForEachUser(do_uid_user_purge, NULL);
-                       break;
                default:
                        syslog(LOG_DEBUG, "User purge for auth mode %d is not implemented.", CtdlGetConfigInt("c_auth_mode"));
                        break;
@@ -559,15 +523,13 @@ int PurgeUsers(void) {
        if (num_users_purged > 0) CtdlAideMessage(transcript, "User Purge Message");
        free(transcript);
 
-       if(users_corrupt_msg)
-       {
+       if (users_corrupt_msg) {
                CtdlAideMessage(users_corrupt_msg, "User Corruption Message");
                free (users_corrupt_msg);
                users_corrupt_msg = NULL;
        }
        
-       if(users_zero_msg)
-       {
+       if(users_zero_msg) {
                CtdlAideMessage(users_zero_msg, "User Zero Message");
                free (users_zero_msg);
                users_zero_msg = NULL;
index d55f8e9de1c9ad8f424318a93a72d6e299e75a4a..7c62d34652754747a03a03ab6e433f6e67965d1e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This module dumps and/or loads the Citadel database in XML format.
  *
- * Copyright (c) 1987-2018 by the citadel.org team
+ * Copyright (c) 1987-2019 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 3.
@@ -114,21 +114,27 @@ void xml_strout(char *str) {
 /*
  * Export a user record as XML
  */
-void migr_export_users_backend(struct ctdluser *buf, void *data) {
+void migr_export_users_backend(char *username, void *data) {
+
+       struct ctdluser u;
+       if (CtdlGetUser(&u, username) != 0) {
+               return;
+       }
+
        client_write(HKEY("<user>\n"));
-       cprintf("<u_version>%d</u_version>\n", buf->version);
-       cprintf("<u_uid>%ld</u_uid>\n", (long)buf->uid);
-       client_write(HKEY("<u_password>"));     xml_strout(buf->password);              client_write(HKEY("</u_password>\n"));
-       cprintf("<u_flags>%u</u_flags>\n", buf->flags);
-       cprintf("<u_timescalled>%ld</u_timescalled>\n", buf->timescalled);
-       cprintf("<u_posted>%ld</u_posted>\n", buf->posted);
-       cprintf("<u_axlevel>%d</u_axlevel>\n", buf->axlevel);
-       cprintf("<u_usernum>%ld</u_usernum>\n", buf->usernum);
-       cprintf("<u_lastcall>%ld</u_lastcall>\n", (long)buf->lastcall);
-       cprintf("<u_USuserpurge>%d</u_USuserpurge>\n", buf->USuserpurge);
-       client_write(HKEY("<u_fullname>"));     xml_strout(buf->fullname);              client_write(HKEY("</u_fullname>\n"));
-       cprintf("<u_msgnum_bio>%ld</u_msgnum_bio>\n", buf->msgnum_bio);
-       cprintf("<u_msgnum_pic>%ld</u_msgnum_pic>\n", buf->msgnum_pic);
+       cprintf("<u_version>%d</u_version>\n", u.version);
+       cprintf("<u_uid>%ld</u_uid>\n", (long)u.uid);
+       client_write(HKEY("<u_password>"));     xml_strout(u.password);         client_write(HKEY("</u_password>\n"));
+       cprintf("<u_flags>%u</u_flags>\n", u.flags);
+       cprintf("<u_timescalled>%ld</u_timescalled>\n", u.timescalled);
+       cprintf("<u_posted>%ld</u_posted>\n", u.posted);
+       cprintf("<u_axlevel>%d</u_axlevel>\n", u.axlevel);
+       cprintf("<u_usernum>%ld</u_usernum>\n", u.usernum);
+       cprintf("<u_lastcall>%ld</u_lastcall>\n", (long)u.lastcall);
+       cprintf("<u_USuserpurge>%d</u_USuserpurge>\n", u.USuserpurge);
+       client_write(HKEY("<u_fullname>"));     xml_strout(u.fullname);         client_write(HKEY("</u_fullname>\n"));
+       cprintf("<u_msgnum_bio>%ld</u_msgnum_bio>\n", u.msgnum_bio);
+       cprintf("<u_msgnum_pic>%ld</u_msgnum_pic>\n", u.msgnum_pic);
        client_write(HKEY("</user>\n"));
 }
 
index 4311504a4e94346fddaaa339b8000a00978cd81d..59089d295fbfbfd738b801ab8bcde093fadb2748 100644 (file)
@@ -107,51 +107,23 @@ void fix_sys_user_name(void)
 
 /* 
  * Back end processing function for reindex_uids()
- * Call this function as a ForEachUser backend in order to queue up
- * user names, or call it with a null user to make it do the processing.
- * This allows us to maintain the list as a static instead of passing
- * pointers around.
  */
-void reindex_uids_backend(struct ctdluser *usbuf, void *data) {
-       static struct UserProcList *uplist = NULL;
-       struct UserProcList *ptr;
-       struct ctdluser us;
+void reindex_uids_backend(char *username, void *data) {
 
-       /* this is the calling mode where we add a user */
+       struct ctdluser us;
 
-       if (usbuf != NULL) {
-               ptr = (struct UserProcList *) malloc(sizeof (struct UserProcList));
-               if (ptr == NULL) {
-                       return;
+       if (CtdlGetUserLock(&us, username) == 0) {
+               syslog(LOG_DEBUG, "Processing <%s> (%d)", us.fullname, us.uid);
+               if (us.uid == CTDLUID) {
+                       us.uid = NATIVE_AUTH_UID;
                }
-
-               safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
-               ptr->next = uplist;
-               uplist = ptr;
-               return;
-       }
-
-       /* this is the calling mode where we do the processing */
-
-       while (uplist != NULL) {
-
-               if (CtdlGetUserLock(&us, uplist->user) == 0) {
-                       syslog(LOG_DEBUG, "Processing <%s> (%d)", uplist->user, us.uid);
-                       if (us.uid == CTDLUID) {
-                               us.uid = NATIVE_AUTH_UID;
-                       }
-                       CtdlPutUserLock(&us);
-                       if ((us.uid > 0) && (us.uid != NATIVE_AUTH_UID)) {              // if non-native auth , index by uid
-                               StrBuf *claimed_id = NewStrBuf();
-                               StrBufPrintf(claimed_id, "uid:%d", us.uid);
-                               attach_extauth(&us, claimed_id);
-                               FreeStrBuf(&claimed_id);
-                       }
+               CtdlPutUserLock(&us);
+               if ((us.uid > 0) && (us.uid != NATIVE_AUTH_UID)) {              // if non-native auth , index by uid
+                       StrBuf *claimed_id = NewStrBuf();
+                       StrBufPrintf(claimed_id, "uid:%d", us.uid);
+                       attach_extauth(&us, claimed_id);
+                       FreeStrBuf(&claimed_id);
                }
-
-               ptr = uplist;
-               uplist = uplist->next;
-               free(ptr);
        }
 }
 
@@ -163,7 +135,6 @@ void reindex_uids_backend(struct ctdluser *usbuf, void *data) {
 void reindex_uids(void) {
        syslog(LOG_WARNING, "upgrade: reindexing and applying uid changes");
        ForEachUser(reindex_uids_backend, NULL);
-       reindex_uids_backend(NULL, NULL);
        return;
 }
 
@@ -443,61 +414,32 @@ void update_config(void) {
  * This allows us to maintain the list as a static instead of passing
  * pointers around.
  */
-void miafvtur_backend(struct ctdluser *usbuf, void *data) {
-
-       struct miafvtur {
-               char name[64];
-               char emails[512];
-       };
-
-       static struct miafvtur *m = NULL;
-       static int num_m = 0;
-       static int alloc_m = 0;
+void miafvtur_backend(char *username, void *data) {
+       struct ctdluser usbuf;
+       char primary_inet_email[512] = { 0 };
+       char other_inet_emails[512] = { 0 };
+       char combined_inet_emails[512] = { 0 };
 
-       /* this is the calling mode where we add a user */
+       if (CtdlGetUser(&usbuf, username) != 0) {
+               return;
+       }
 
-       if (usbuf != NULL) {
-               char primary_inet_email[512] = { 0 };
-               char other_inet_emails[512] = { 0 };
-               struct vCard *v = vcard_get_user(usbuf);
-               if (!v) return;
-               extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
-               vcard_free(v);
+       struct vCard *v = vcard_get_user(&usbuf);
+       if (!v) return;
+       extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
+       vcard_free(v);
        
-               if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
-                       return;
-               }
-
-               if (num_m >= alloc_m) {
-                       if (alloc_m == 0) {
-                               alloc_m = 100;
-                               m = malloc(sizeof(struct miafvtur) * alloc_m);
-                       }
-                       else {
-                               alloc_m *= 2;
-                               m = realloc(m, (sizeof(struct miafvtur) * alloc_m));
-                       }
-               }
-
-               strcpy(m[num_m].name, usbuf->fullname);
-               snprintf(m[num_m].emails, 512, "%s%s%s",
-                       (!IsEmptyStr(primary_inet_email) ? primary_inet_email : ""),
-                       ((!IsEmptyStr(primary_inet_email)&&(!IsEmptyStr(other_inet_emails))) ? "|" : ""),
-                       (!IsEmptyStr(other_inet_emails) ? other_inet_emails : "")
-               );
-               ++num_m;
+       if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
                return;
        }
 
-       /* this is the calling mode where we do the processing */
-       int i;
-       for (i=0; i<num_m; ++i) {
-               CtdlSetEmailAddressesForUser(m[i].name, m[i].emails);
-       }
-       free(m);
-       num_m = 0;
-       alloc_m = 0;
-       return;
+       snprintf(combined_inet_emails, 512, "%s%s%s",
+               (!IsEmptyStr(primary_inet_email) ? primary_inet_email : ""),
+               ((!IsEmptyStr(primary_inet_email)&&(!IsEmptyStr(other_inet_emails))) ? "|" : ""),
+               (!IsEmptyStr(other_inet_emails) ? other_inet_emails : "")
+       );
+
+       CtdlSetEmailAddressesForUser(usbuf.fullname, combined_inet_emails);
 }
 
 
@@ -540,7 +482,6 @@ int ProcessOldStyleAdjRefCountQueue(void)
 void move_inet_addrs_from_vcards_to_user_records(void)
 {
        ForEachUser(miafvtur_backend, NULL);
-       miafvtur_backend(NULL, NULL);
        CtdlRebuildDirectoryIndex();
 }
 
@@ -650,6 +591,7 @@ CTDL_MODULE_INIT(upgrade)
 {
        if(!threading)
        {
+               move_inet_addrs_from_vcards_to_user_records();
                post_startup_upgrades();
        }
        
index c5eeabd2cbca8bd9fec607d7d10fc542f15af69d..99543f719c7e26efd640e939faa114eb15204707 100644 (file)
@@ -31,8 +31,6 @@
 int chkpwd_write_pipe[2];
 int chkpwd_read_pipe[2];
 
-
-
 /*
  * Trim a string down to the maximum username size and return the new length
  */
@@ -432,38 +430,12 @@ int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
 /*
  * Helper function for rebuild_usersbynumber()
  */
-void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
-
-       struct ubnlist {
-               struct ubnlist *next;
-               char username[USERNAME_SIZE];
-               long usernum;
-       };
-
-       static struct ubnlist *u = NULL;
-       struct ubnlist *ptr = NULL;
+void rebuild_ubn_for_user(char *username, void *data) {
+       struct ctdluser u;
 
-       /* Lazy programming here.  Call this function as a ForEachUser backend
-        * in order to queue up the room names, or call it with a null user
-        * to make it do the processing.
-        */
-       if (usbuf != NULL) {
-               ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
-               if (ptr == NULL) return;
-
-               ptr->usernum = usbuf->usernum;
-               safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
-               ptr->next = u;
-               u = ptr;
-               return;
-       }
-
-       while (u != NULL) {
-               syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index %10ld : %s", u->usernum, u->username);
-               cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
-               ptr = u;
-               u = u->next;
-               free(ptr);
+       syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index for %s", username);
+       if (CtdlGetUser(&u, username) == 0) {
+               cdb_store(CDB_USERSBYNUMBER, &(u.usernum), sizeof(long), u.fullname, strlen(u.fullname)+1);
        }
 }
 
@@ -474,7 +446,6 @@ void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
 void rebuild_usersbynumber(void) {
        cdb_trunc(CDB_USERSBYNUMBER);                   /* delete the old indices */
        ForEachUser(rebuild_ubn_for_user, NULL);        /* enumerate the users */
-       rebuild_ubn_for_user(NULL, NULL);               /* and index them */
 }
 
 
@@ -1173,52 +1144,49 @@ int CtdlForgetThisRoom(void) {
 
 
 /* 
- *  Traverse the user file...
+ * Traverse the user file and perform a callback for each user record.
+ * (New improved version that runs in two phases so that callbacks can perform writes without having a r/o cursor open)
  */
-void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
-                void *in_data)
+void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data)
 {
-       struct ctdluser usbuf;
+       struct feu {
+               char username[USERNAME_SIZE];
+               int version;
+       };
+
        struct cdbdata *cdbus;
+       struct ctdluser *usptr;
+       int i = 0;
+       struct feu *usernames = NULL;
+       int num_users = 0;
+       int num_users_alloc = 0;
 
        cdb_rewind(CDB_USERS);
 
+       // Phase 1 : load list of usernames
        while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
-               memset(&usbuf, 0, sizeof(struct ctdluser));
-               memcpy(&usbuf, cdbus->ptr,
-                      ((cdbus->len > sizeof(struct ctdluser)) ?
-                       sizeof(struct ctdluser) : cdbus->len));
-               cdb_free(cdbus);
-               (*CallBack) (&usbuf, in_data);
-       }
-}
+               usptr = (struct ctdluser *) cdbus->ptr;
 
+               if (strlen(usptr->fullname) > 0) {
+                       ++num_users;
+                       if (num_users > num_users_alloc) {
+                               num_users_alloc = ((num_users_alloc == 0) ? 1 : (num_users_alloc * 2));
+                               usernames = realloc(usernames, num_users_alloc * sizeof(struct feu));
+                       }
+                       strcpy(usernames[num_users-1].username, usptr->fullname);
+                       usernames[num_users-1].version = usptr->version;
+               }
+       }
 
-/*
- * List one user (this works with cmd_list)
- */
-void ListThisUser(struct ctdluser *usbuf, void *data)
-{
-       char *searchstring;
-
-       searchstring = (char *)data;
-       if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
-               return;
-       }
-
-       if (usbuf->axlevel > AxDeleted) {
-               if ((CC->user.axlevel >= AxAideU)
-                   || ((usbuf->flags & US_UNLISTED) == 0)
-                   || ((CC->internal_pgm))) {
-                       cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
-                               usbuf->fullname,
-                               usbuf->axlevel,
-                               usbuf->usernum,
-                               (long)usbuf->lastcall,
-                               usbuf->timescalled,
-                               usbuf->posted);
+       // Phase 2 : perform the callback for each username
+       for (i=0; i<num_users; ++i) {
+               if (usernames[i].version < 927) {
+                       // FIXME we have to reindex this record
                }
+               (*CallBack) (usernames[i].username, in_data);
        }
+
+       free(usernames);
 }
 
 
index 040932c1df29d33c5b196ead00d5ab9c900cd784..f58ec1bb6489cfbdfa5c1dfefc2598d58742ee1e 100644 (file)
@@ -37,8 +37,7 @@ enum {
 
 void do_login(void);
 int CtdlInvtKick(char *iuser, int op);
-void ForEachUser(void (*CallBack)(struct ctdluser *EachUser, void *out_data), void *in_data);
-void ListThisUser(struct ctdluser *usbuf, void *data);
+void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data);
 int NewMailCount(void);
 int InitialMailCheck(void);
 void put_visit(visit *newvisit);