2 * Transparently handle the upgrading of server data formats. If we see
3 * an existing version number of our database, we can make some intelligent
4 * guesses about what kind of data format changes need to be applied, and
5 * we apply them transparently.
7 * Copyright (c) 1987-2019 by the citadel.org team
9 * This program is open source software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
26 #include <sys/types.h>
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
42 #include <libcitadel.h>
45 #include "citserver.h"
52 #include "serv_upgrade.h"
53 #include "euidindex.h"
54 #include "ctdl_module.h"
55 #include "serv_vcard.h"
56 #include "internet_addressing.h"
59 * oldver is the version number of Citadel Server which was active on the previous run of the program, learned from the system configuration.
60 * If we are running a new Citadel Server for the first time, oldver will be 0.
61 * We keep this value around for the entire duration of the program run because we'll need it during several stages of startup.
66 * Fix up the name for Citadel user 0 and try to remove any extra users with number 0
68 void fix_sys_user_name(void)
70 struct ctdluser usbuf;
71 char usernamekey[USERNAME_SIZE];
73 /** If we have a user called Citadel rename them to SYS_Citadel */
74 if (CtdlGetUser(&usbuf, "Citadel") == 0)
76 rename_user("Citadel", "SYS_Citadel");
79 while (CtdlGetUserByNumber(&usbuf, 0) == 0)
81 /* delete user with number 0 and no name */
82 if (IsEmptyStr(usbuf.fullname)) {
83 cdb_delete(CDB_USERS, "", 0);
86 /* temporarily set this user to -1 */
92 /* Make sure user SYS_* is user 0 */
93 while (CtdlGetUserByNumber(&usbuf, -1) == 0)
95 if (strncmp(usbuf.fullname, "SYS_", 4))
96 { /* Delete any user 0 that doesn't start with SYS_ */
97 makeuserkey(usernamekey, usbuf.fullname, cutusername(usbuf.fullname));
98 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
109 * Back end processing function for reindex_uids()
111 void reindex_uids_backend(char *username, void *data) {
115 if (CtdlGetUserLock(&us, username) == 0) {
116 syslog(LOG_DEBUG, "Processing <%s> (%d)", us.fullname, us.uid);
117 if (us.uid == CTDLUID) {
118 us.uid = NATIVE_AUTH_UID;
120 CtdlPutUserLock(&us);
121 if ((us.uid > 0) && (us.uid != NATIVE_AUTH_UID)) { // if non-native auth , index by uid
122 StrBuf *claimed_id = NewStrBuf();
123 StrBufPrintf(claimed_id, "uid:%d", us.uid);
124 attach_extauth(&us, claimed_id);
125 FreeStrBuf(&claimed_id);
132 * Build extauth index of all users with uid-based join (system auth, LDAP auth)
133 * Also changes all users with a uid of CTDLUID to NATIVE_AUTH_UID (-1)
135 void reindex_uids(void) {
136 syslog(LOG_WARNING, "upgrade: reindexing and applying uid changes");
137 ForEachUser(reindex_uids_backend, NULL);
143 * These accounts may have been created by code that ran between mid 2008 and early 2011.
144 * If present they are no longer in use and may be deleted.
146 void remove_thread_users(void) {
147 char *deleteusers[] = {
156 "SYS_select_on_master",
161 struct ctdluser usbuf;
162 for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
163 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
165 strcpy(usbuf.password, "deleteme");
168 "System user account <%s> is no longer in use and will be deleted.",
177 * Attempt to guess the name of the time zone currently in use
178 * on the underlying host system.
180 void guess_time_zone(void) {
184 fp = popen(file_guesstimezone, "r");
186 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
187 buf[strlen(buf)-1] = 0;
188 CtdlSetConfigStr("c_default_cal_zone", buf);
189 syslog(LOG_INFO, "Configuring timezone: %s", buf);
197 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
199 * This is the second pass, where we process the list of rooms with info or pic files.
201 void iorarf_oneroom(char *roomname, char *infofile, char *picfile)
205 char *unencoded_data;
207 long info_msgnum = 0;
211 // Test for the presence of a legacy "room info file"
212 if (!IsEmptyStr(infofile)) {
213 fp = fopen(infofile, "r");
219 fseek(fp, 0, SEEK_END);
220 data_length = ftell(fp);
222 if (data_length >= 1) {
224 unencoded_data = malloc(data_length);
225 if (unencoded_data) {
226 fread(unencoded_data, data_length, 1, fp);
227 encoded_data = malloc((data_length * 2) + 100);
229 sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n");
230 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
231 snprintf(subject, sizeof subject, "Imported room banner for %s", roomname);
232 info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
235 free(unencoded_data);
239 if (info_msgnum > 0) unlink(infofile);
242 // Test for the presence of a legacy "room picture file" and import it.
243 if (!IsEmptyStr(picfile)) {
244 fp = fopen(picfile, "r");
250 fseek(fp, 0, SEEK_END);
251 data_length = ftell(fp);
253 if (data_length >= 1) {
255 unencoded_data = malloc(data_length);
256 if (unencoded_data) {
257 fread(unencoded_data, data_length, 1, fp);
258 encoded_data = malloc((data_length * 2) + 100);
260 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
261 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
262 snprintf(subject, sizeof subject, "Imported room icon for %s", roomname);
263 pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
266 free(unencoded_data);
270 if (pic_msgnum > 0) unlink(picfile);
273 // Now we have the message numbers of our new banner and icon. Record them in the room record.
274 // NOTE: we are not deleting the old msgnum_info because that position in the record was previously
275 // a pointer to the highest message number which existed in the room when the info file was saved,
276 // and we don't want to delete messages that are not *actually* old banners.
277 struct ctdlroom qrbuf;
278 if (CtdlGetRoomLock(&qrbuf, roomname) == 0) {
279 qrbuf.msgnum_info = info_msgnum;
280 qrbuf.msgnum_pic = pic_msgnum;
281 CtdlPutRoomLock(&qrbuf);
288 struct iorarf_list *next;
289 char name[ROOMNAMELEN];
296 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
298 * This is the first pass, where the list of qualifying rooms is gathered.
300 void iorarf_backend(struct ctdlroom *qrbuf, void *data)
303 struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
305 struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
306 i->next = *iorarf_list;
307 strcpy(i->name, qrbuf->QRname);
311 // Test for the presence of a legacy "room info file"
312 assoc_file_name(i->info, sizeof i->info, qrbuf, ctdl_info_dir);
313 fp = fopen(i->info, "r");
321 // Test for the presence of a legacy "room picture file"
322 assoc_file_name(i->pic, sizeof i->pic, qrbuf, ctdl_image_dir);
323 fp = fopen(i->pic, "r");
331 if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
341 * Prior to Citadel Server version 902, room info and pictures (which comprise the
342 * displayed banner for each room) were stored in the filesystem. If we are upgrading
343 * from version >000 to version >=902, ingest those files into the database.
345 void ingest_old_roominfo_and_roompic_files(void)
347 struct iorarf_list *il = NULL;
349 CtdlForEachRoom(iorarf_backend, &il);
351 struct iorarf_list *p;
353 iorarf_oneroom(il->name, il->info, il->pic);
359 unlink(ctdl_info_dir);
364 * For upgrades in which a new config setting appears for the first time, set default values.
365 * For new installations (oldver == 0) also set default values.
367 void update_config(void) {
370 CtdlSetConfigInt("c_rfc822_strict_from", 0);
374 CtdlSetConfigInt("c_purge_hour", 3);
378 CtdlSetConfigInt("c_ldap_port", 389);
382 CtdlSetConfigStr("c_ip_addr", "*");
386 CtdlSetConfigInt("c_enable_fulltext", 1);
390 CtdlSetConfigInt("c_auto_cull", 1);
394 CtdlSetConfigInt("c_xmpp_c2s_port", 5222);
395 CtdlSetConfigInt("c_xmpp_s2s_port", 5269);
399 CtdlSetConfigInt("c_nntp_port", 119);
400 CtdlSetConfigInt("c_nntps_port", 563);
403 if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
410 * Helper function for move_inet_addrs_from_vcards_to_user_records()
412 * Call this function as a ForEachUser backend in order to queue up
413 * user names, or call it with a null user to make it do the processing.
414 * This allows us to maintain the list as a static instead of passing
417 void miafvtur_backend(char *username, void *data) {
418 struct ctdluser usbuf;
419 char primary_inet_email[512] = { 0 };
420 char other_inet_emails[512] = { 0 };
421 char combined_inet_emails[512] = { 0 };
423 if (CtdlGetUser(&usbuf, username) != 0) {
427 struct vCard *v = vcard_get_user(&usbuf);
429 extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
432 if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
436 snprintf(combined_inet_emails, 512, "%s%s%s",
437 (!IsEmptyStr(primary_inet_email) ? primary_inet_email : ""),
438 ((!IsEmptyStr(primary_inet_email)&&(!IsEmptyStr(other_inet_emails))) ? "|" : ""),
439 (!IsEmptyStr(other_inet_emails) ? other_inet_emails : "")
442 CtdlSetEmailAddressesForUser(usbuf.fullname, combined_inet_emails);
447 * If our system still has a "refcount_adjustments.dat" sitting around from an old version, ingest it now.
449 int ProcessOldStyleAdjRefCountQueue(void)
453 struct arcq arcq_rec;
454 int num_records_processed = 0;
456 fp = fopen(file_arcq, "rb");
458 return(num_records_processed);
461 syslog(LOG_INFO, "msgbase: ingesting %s", file_arcq);
463 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
464 AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
465 ++num_records_processed;
469 r = unlink(file_arcq);
471 syslog(LOG_ERR, "%s: %m", file_arcq);
474 return(num_records_processed);
479 * Prior to version 912 we kept a user's various Internet email addresses in their vCards.
480 * This function moves them over to the user record, which is where we keep them now.
482 void move_inet_addrs_from_vcards_to_user_records(void)
484 ForEachUser(miafvtur_backend, NULL);
485 CtdlRebuildDirectoryIndex();
490 * Based on the server version number reported by the existing database,
491 * run in-place data format upgrades until everything is up to date.
493 void pre_startup_upgrades(void) {
495 oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
496 syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
499 if (oldver < REV_LEVEL) {
500 syslog(LOG_WARNING, "Running pre-startup database upgrades.");
506 if ((oldver > 000) && (oldver < 591)) {
507 syslog(LOG_EMERG, "This database is too old to be upgraded. Citadel server will exit.");
510 if ((oldver > 000) && (oldver < 913)) {
513 if ((oldver > 000) && (oldver < 659)) {
514 rebuild_euid_index();
520 rebuild_usersbynumber();
523 remove_thread_users();
526 struct ctdlroom QRoom;
527 if (!CtdlGetRoom(&QRoom, SMTP_SPOOLOUT_ROOM)) {
528 QRoom.QRdefaultview = VIEW_QUEUE;
533 if ((oldver > 000) && (oldver < 902)) {
534 ingest_old_roominfo_and_roompic_files();
537 CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
540 * Negative values for maxsessions are not allowed.
542 if (CtdlGetConfigInt("c_maxsessions") < 0) {
543 CtdlSetConfigInt("c_maxsessions", 0);
546 /* We need a system default message expiry policy, because this is
547 * the top level and there's no 'higher' policy to fall back on.
548 * By default, do not expire messages at all.
550 if (CtdlGetConfigInt("c_ep_mode") == 0) {
551 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
552 CtdlSetConfigInt("c_ep_value", 0);
558 * Based on the server version number reported by the existing database,
559 * run in-place data format upgrades until everything is up to date.
561 void post_startup_upgrades(void) {
563 syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
565 if (oldver < REV_LEVEL) {
566 syslog(LOG_WARNING, "Running post-startup database upgrades.");
572 if ((oldver > 000) && (oldver < 912)) {
573 move_inet_addrs_from_vcards_to_user_records();
576 if ((oldver > 000) && (oldver < 922)) {
577 ProcessOldStyleAdjRefCountQueue();
582 CTDL_MODULE_UPGRADE(upgrade)
584 pre_startup_upgrades();
586 /* return our module id for the Log */
590 CTDL_MODULE_INIT(upgrade)
594 move_inet_addrs_from_vcards_to_user_records();
595 post_startup_upgrades();
598 /* return our module name for the log */