From 9ffea7c3315046ddcea2589656c13da5f5e0c076 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Sun, 4 Aug 2019 22:44:34 -0400 Subject: [PATCH] Modified the behavior of ForEachUser() to do the two phase load/perform cycle as part of the function, since so many callbacks were implementing this anyway. --- citadel/citserver.h | 2 +- citadel/control.c | 17 ++-- citadel/internet_addressing.c | 56 ++--------- citadel/modules/ctdlproto/serv_user.c | 36 ++++++- citadel/modules/expire/serv_expire.c | 102 +++++++------------- citadel/modules/migrate/serv_migrate.c | 36 ++++--- citadel/modules/upgrade/serv_upgrade.c | 124 +++++++------------------ citadel/user_ops.c | 106 ++++++++------------- citadel/user_ops.h | 3 +- 9 files changed, 181 insertions(+), 301 deletions(-) diff --git a/citadel/citserver.h b/citadel/citserver.h index e292ab7a8..adfe476c1 100644 --- a/citadel/citserver.h +++ b/citadel/citserver.h @@ -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) diff --git a/citadel/control.c b/citadel/control.c index 6f3a1d7ec..3463de80b 100644 --- a/citadel/control.c +++ b/citadel/control.c @@ -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); } } } diff --git a/citadel/internet_addressing.c b/citadel/internet_addressing.c index 33d70451a..416586bed 100644 --- a/citadel/internet_addressing.c +++ b/citadel/internet_addressing.c @@ -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 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) */ diff --git a/citadel/modules/expire/serv_expire.c b/citadel/modules/expire/serv_expire.c index 879975496..09e9eab10 100644 --- a/citadel/modules/expire/serv_expire.c +++ b/citadel/modules/expire/serv_expire.c @@ -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 @@ -14,27 +14,6 @@ * 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; diff --git a/citadel/modules/migrate/serv_migrate.c b/citadel/modules/migrate/serv_migrate.c index d55f8e9de..7c62d3465 100644 --- a/citadel/modules/migrate/serv_migrate.c +++ b/citadel/modules/migrate/serv_migrate.c @@ -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("\n")); - cprintf("%d\n", buf->version); - cprintf("%ld\n", (long)buf->uid); - client_write(HKEY("")); xml_strout(buf->password); client_write(HKEY("\n")); - cprintf("%u\n", buf->flags); - cprintf("%ld\n", buf->timescalled); - cprintf("%ld\n", buf->posted); - cprintf("%d\n", buf->axlevel); - cprintf("%ld\n", buf->usernum); - cprintf("%ld\n", (long)buf->lastcall); - cprintf("%d\n", buf->USuserpurge); - client_write(HKEY("")); xml_strout(buf->fullname); client_write(HKEY("\n")); - cprintf("%ld\n", buf->msgnum_bio); - cprintf("%ld\n", buf->msgnum_pic); + cprintf("%d\n", u.version); + cprintf("%ld\n", (long)u.uid); + client_write(HKEY("")); xml_strout(u.password); client_write(HKEY("\n")); + cprintf("%u\n", u.flags); + cprintf("%ld\n", u.timescalled); + cprintf("%ld\n", u.posted); + cprintf("%d\n", u.axlevel); + cprintf("%ld\n", u.usernum); + cprintf("%ld\n", (long)u.lastcall); + cprintf("%d\n", u.USuserpurge); + client_write(HKEY("")); xml_strout(u.fullname); client_write(HKEY("\n")); + cprintf("%ld\n", u.msgnum_bio); + cprintf("%ld\n", u.msgnum_pic); client_write(HKEY("\n")); } diff --git a/citadel/modules/upgrade/serv_upgrade.c b/citadel/modules/upgrade/serv_upgrade.c index 4311504a4..59089d295 100644 --- a/citadel/modules/upgrade/serv_upgrade.c +++ b/citadel/modules/upgrade/serv_upgrade.c @@ -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; iusernum = 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