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 * 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 while (CtdlGetUserByNumber(&usbuf, 0) == 0) {
74 /* delete user with number 0 and no name */
75 if (IsEmptyStr(usbuf.fullname)) {
76 cdb_delete(CDB_USERS, "", 0);
79 /* temporarily set this user to -1 */
85 /* Delete any "user 0" accounts */
86 while (CtdlGetUserByNumber(&usbuf, -1) == 0) {
87 makeuserkey(usernamekey, usbuf.fullname, cutusername(usbuf.fullname));
88 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
94 * Back end processing function for reindex_uids()
96 void reindex_uids_backend(char *username, void *data) {
100 if (CtdlGetUserLock(&us, username) == 0) {
101 syslog(LOG_DEBUG, "Processing <%s> (%d)", us.fullname, us.uid);
102 if (us.uid == CTDLUID) {
103 us.uid = NATIVE_AUTH_UID;
105 CtdlPutUserLock(&us);
106 if ((us.uid > 0) && (us.uid != NATIVE_AUTH_UID)) { // if non-native auth , index by uid
107 StrBuf *claimed_id = NewStrBuf();
108 StrBufPrintf(claimed_id, "uid:%d", us.uid);
109 attach_extauth(&us, claimed_id);
110 FreeStrBuf(&claimed_id);
117 * Build extauth index of all users with uid-based join (system auth, LDAP auth)
118 * Also changes all users with a uid of CTDLUID to NATIVE_AUTH_UID (-1)
120 void reindex_uids(void) {
121 syslog(LOG_WARNING, "upgrade: reindexing and applying uid changes");
122 ForEachUser(reindex_uids_backend, NULL);
128 * These accounts may have been created by code that ran between mid 2008 and early 2011.
129 * If present they are no longer in use and may be deleted.
131 void remove_thread_users(void) {
132 char *deleteusers[] = {
141 "SYS_select_on_master",
146 struct ctdluser usbuf;
147 for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
148 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
150 strcpy(usbuf.password, "deleteme");
153 "System user account <%s> is no longer in use and will be deleted.",
162 * Attempt to guess the name of the time zone currently in use
163 * on the underlying host system.
165 void guess_time_zone(void) {
169 fp = popen(file_guesstimezone, "r");
171 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
172 buf[strlen(buf)-1] = 0;
173 CtdlSetConfigStr("c_default_cal_zone", buf);
174 syslog(LOG_INFO, "Configuring timezone: %s", buf);
182 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
184 * This is the second pass, where we process the list of rooms with info or pic files.
186 void iorarf_oneroom(char *roomname, char *infofile, char *picfile)
190 char *unencoded_data;
192 long info_msgnum = 0;
196 // Test for the presence of a legacy "room info file"
197 if (!IsEmptyStr(infofile)) {
198 fp = fopen(infofile, "r");
204 fseek(fp, 0, SEEK_END);
205 data_length = ftell(fp);
207 if (data_length >= 1) {
209 unencoded_data = malloc(data_length);
210 if (unencoded_data) {
211 fread(unencoded_data, data_length, 1, fp);
212 encoded_data = malloc((data_length * 2) + 100);
214 sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n");
215 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
216 snprintf(subject, sizeof subject, "Imported room banner for %s", roomname);
217 info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
220 free(unencoded_data);
224 if (info_msgnum > 0) unlink(infofile);
227 // Test for the presence of a legacy "room picture file" and import it.
228 if (!IsEmptyStr(picfile)) {
229 fp = fopen(picfile, "r");
235 fseek(fp, 0, SEEK_END);
236 data_length = ftell(fp);
238 if (data_length >= 1) {
240 unencoded_data = malloc(data_length);
241 if (unencoded_data) {
242 fread(unencoded_data, data_length, 1, fp);
243 encoded_data = malloc((data_length * 2) + 100);
245 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
246 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
247 snprintf(subject, sizeof subject, "Imported room icon for %s", roomname);
248 pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
251 free(unencoded_data);
255 if (pic_msgnum > 0) unlink(picfile);
258 // Now we have the message numbers of our new banner and icon. Record them in the room record.
259 // NOTE: we are not deleting the old msgnum_info because that position in the record was previously
260 // a pointer to the highest message number which existed in the room when the info file was saved,
261 // and we don't want to delete messages that are not *actually* old banners.
262 struct ctdlroom qrbuf;
263 if (CtdlGetRoomLock(&qrbuf, roomname) == 0) {
264 qrbuf.msgnum_info = info_msgnum;
265 qrbuf.msgnum_pic = pic_msgnum;
266 CtdlPutRoomLock(&qrbuf);
273 struct iorarf_list *next;
274 char name[ROOMNAMELEN];
281 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
283 * This is the first pass, where the list of qualifying rooms is gathered.
285 void iorarf_backend(struct ctdlroom *qrbuf, void *data)
288 struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
290 struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
291 i->next = *iorarf_list;
292 strcpy(i->name, qrbuf->QRname);
296 // Test for the presence of a legacy "room info file"
297 assoc_file_name(i->info, sizeof i->info, qrbuf, ctdl_info_dir);
298 fp = fopen(i->info, "r");
306 // Test for the presence of a legacy "room picture file"
307 assoc_file_name(i->pic, sizeof i->pic, qrbuf, ctdl_image_dir);
308 fp = fopen(i->pic, "r");
316 if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
326 * Prior to Citadel Server version 902, room info and pictures (which comprise the
327 * displayed banner for each room) were stored in the filesystem. If we are upgrading
328 * from version >000 to version >=902, ingest those files into the database.
330 void ingest_old_roominfo_and_roompic_files(void)
332 struct iorarf_list *il = NULL;
334 CtdlForEachRoom(iorarf_backend, &il);
336 struct iorarf_list *p;
338 iorarf_oneroom(il->name, il->info, il->pic);
344 unlink(ctdl_info_dir);
349 * For upgrades in which a new config setting appears for the first time, set default values.
350 * For new installations (oldver == 0) also set default values.
352 void update_config(void) {
355 CtdlSetConfigInt("c_rfc822_strict_from", 0);
359 CtdlSetConfigInt("c_purge_hour", 3);
363 CtdlSetConfigInt("c_ldap_port", 389);
367 CtdlSetConfigStr("c_ip_addr", "*");
371 CtdlSetConfigInt("c_enable_fulltext", 1);
375 CtdlSetConfigInt("c_auto_cull", 1);
379 CtdlSetConfigInt("c_xmpp_c2s_port", 5222);
380 CtdlSetConfigInt("c_xmpp_s2s_port", 5269);
384 CtdlSetConfigInt("c_nntp_port", 119);
385 CtdlSetConfigInt("c_nntps_port", 563);
388 if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
395 * Helper function for move_inet_addrs_from_vcards_to_user_records()
397 * Call this function as a ForEachUser backend in order to queue up
398 * user names, or call it with a null user to make it do the processing.
399 * This allows us to maintain the list as a static instead of passing
402 void miafvtur_backend(char *username, void *data) {
403 struct ctdluser usbuf;
404 char primary_inet_email[512] = { 0 };
405 char other_inet_emails[512] = { 0 };
406 char combined_inet_emails[512] = { 0 };
408 if (CtdlGetUser(&usbuf, username) != 0) {
412 struct vCard *v = vcard_get_user(&usbuf);
414 extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
417 if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
421 snprintf(combined_inet_emails, 512, "%s%s%s",
422 (!IsEmptyStr(primary_inet_email) ? primary_inet_email : ""),
423 ((!IsEmptyStr(primary_inet_email)&&(!IsEmptyStr(other_inet_emails))) ? "|" : ""),
424 (!IsEmptyStr(other_inet_emails) ? other_inet_emails : "")
427 CtdlSetEmailAddressesForUser(usbuf.fullname, combined_inet_emails);
432 * If our system still has a "refcount_adjustments.dat" sitting around from an old version, ingest it now.
434 int ProcessOldStyleAdjRefCountQueue(void)
438 struct arcq arcq_rec;
439 int num_records_processed = 0;
441 fp = fopen(file_arcq, "rb");
443 return(num_records_processed);
446 syslog(LOG_INFO, "msgbase: ingesting %s", file_arcq);
448 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
449 AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
450 ++num_records_processed;
454 r = unlink(file_arcq);
456 syslog(LOG_ERR, "%s: %m", file_arcq);
459 return(num_records_processed);
464 * Prior to version 912 we kept a user's various Internet email addresses in their vCards.
465 * This function moves them over to the user record, which is where we keep them now.
467 void move_inet_addrs_from_vcards_to_user_records(void)
469 ForEachUser(miafvtur_backend, NULL);
470 CtdlRebuildDirectoryIndex();
475 * Based on the server version number reported by the existing database,
476 * run in-place data format upgrades until everything is up to date.
478 void pre_startup_upgrades(void) {
480 oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
481 syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
484 if (oldver < REV_LEVEL) {
485 syslog(LOG_WARNING, "Running pre-startup database upgrades.");
491 if ((oldver > 000) && (oldver < 591)) {
492 syslog(LOG_EMERG, "This database is too old to be upgraded. Citadel server will exit.");
495 if ((oldver > 000) && (oldver < 913)) {
498 if ((oldver > 000) && (oldver < 659)) {
499 rebuild_euid_index();
505 rebuild_usersbynumber();
508 remove_thread_users();
511 struct ctdlroom QRoom;
512 if (!CtdlGetRoom(&QRoom, SMTP_SPOOLOUT_ROOM)) {
513 QRoom.QRdefaultview = VIEW_QUEUE;
518 if ((oldver > 000) && (oldver < 902)) {
519 ingest_old_roominfo_and_roompic_files();
522 CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
525 * Negative values for maxsessions are not allowed.
527 if (CtdlGetConfigInt("c_maxsessions") < 0) {
528 CtdlSetConfigInt("c_maxsessions", 0);
531 /* We need a system default message expiry policy, because this is
532 * the top level and there's no 'higher' policy to fall back on.
533 * By default, do not expire messages at all.
535 if (CtdlGetConfigInt("c_ep_mode") == 0) {
536 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
537 CtdlSetConfigInt("c_ep_value", 0);
543 * Based on the server version number reported by the existing database,
544 * run in-place data format upgrades until everything is up to date.
546 void post_startup_upgrades(void) {
548 syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
550 if (oldver < REV_LEVEL) {
551 syslog(LOG_WARNING, "Running post-startup database upgrades.");
557 if ((oldver > 000) && (oldver < 912)) {
558 move_inet_addrs_from_vcards_to_user_records();
561 if ((oldver > 000) && (oldver < 922)) {
562 ProcessOldStyleAdjRefCountQueue();
567 CTDL_MODULE_UPGRADE(upgrade)
569 pre_startup_upgrades();
571 /* return our module id for the Log */
576 CTDL_MODULE_INIT(upgrade)
579 post_startup_upgrades();
582 /* return our module name for the log */