1 // Transparently handle the upgrading of server data formats. If we see
2 // an existing version number of our database, we can make some intelligent
3 // guesses about what kind of data format changes need to be applied, and
4 // we apply them transparently.
6 // Copyright (c) 1987-2023 by the citadel.org team
8 // This program is open source software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License version 3.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 #include "../../sysdep.h"
24 #include <sys/types.h>
29 #include <libcitadel.h>
30 #include "../../citadel_defs.h"
31 #include "../../server.h"
32 #include "../../citserver.h"
33 #include "../../support.h"
34 #include "../../config.h"
35 #include "../../control.h"
36 #include "../../database.h"
37 #include "../../user_ops.h"
38 #include "../../makeuserkey.h"
39 #include "../../msgbase.h"
40 #include "serv_upgrade.h"
41 #include "../../euidindex.h"
42 #include "../../ctdl_module.h"
43 #include "../../serv_vcard.h"
44 #include "../../internet_addressing.h"
46 // oldver is the version number of Citadel Server which was active on the previous run of the program, learned from the system configuration.
47 // If we are running a new Citadel Server for the first time, oldver will be 0.
48 // We keep this value around for the entire duration of the program run because we'll need it during several stages of startup.
52 // Attempt to guess the name of the time zone currently in use
53 // on the underlying host system.
54 void guess_time_zone(void) {
58 fp = popen(file_guesstimezone, "r");
60 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
61 buf[strlen(buf)-1] = 0;
62 CtdlSetConfigStr("c_default_cal_zone", buf);
63 syslog(LOG_INFO, "Configuring timezone: %s", buf);
70 // Per-room callback function for ingest_old_roominfo_and_roompic_files()
71 // This is the second pass, where we process the list of rooms with info or pic files.
72 void iorarf_oneroom(char *roomname, char *infofile, char *picfile) {
81 // Test for the presence of a legacy "room info file"
82 if (!IsEmptyStr(infofile)) {
83 fp = fopen(infofile, "r");
89 fseek(fp, 0, SEEK_END);
90 data_length = ftell(fp);
92 if (data_length >= 1) {
94 unencoded_data = malloc(data_length);
96 fread(unencoded_data, data_length, 1, fp);
97 encoded_data = malloc((data_length * 2) + 100);
99 sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n");
100 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
101 snprintf(subject, sizeof subject, "Imported room banner for %s", roomname);
102 info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
105 free(unencoded_data);
109 if (info_msgnum > 0) unlink(infofile);
112 // Test for the presence of a legacy "room picture file" and import it.
113 if (!IsEmptyStr(picfile)) {
114 fp = fopen(picfile, "r");
120 fseek(fp, 0, SEEK_END);
121 data_length = ftell(fp);
123 if (data_length >= 1) {
125 unencoded_data = malloc(data_length);
126 if (unencoded_data) {
127 fread(unencoded_data, data_length, 1, fp);
128 encoded_data = malloc((data_length * 2) + 100);
130 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
131 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
132 snprintf(subject, sizeof subject, "Imported room icon for %s", roomname);
133 pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
136 free(unencoded_data);
140 if (pic_msgnum > 0) unlink(picfile);
143 // Now we have the message numbers of our new banner and icon. Record them in the room record.
144 // NOTE: we are not deleting the old msgnum_info because that position in the record was previously
145 // a pointer to the highest message number which existed in the room when the info file was saved,
146 // and we don't want to delete messages that are not *actually* old banners.
147 struct ctdlroom qrbuf;
148 if (CtdlGetRoomLock(&qrbuf, roomname) == 0) {
149 qrbuf.msgnum_info = info_msgnum;
150 qrbuf.msgnum_pic = pic_msgnum;
151 CtdlPutRoomLock(&qrbuf);
158 struct iorarf_list *next;
159 char name[ROOMNAMELEN];
165 // Per-room callback function for ingest_old_roominfo_and_roompic_files()
166 // This is the first pass, where the list of qualifying rooms is gathered.
167 void iorarf_backend(struct ctdlroom *qrbuf, void *data) {
169 struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
171 struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
172 i->next = *iorarf_list;
173 strcpy(i->name, qrbuf->QRname);
177 // Test for the presence of a legacy "room info file"
178 assoc_file_name(i->info, sizeof i->info, qrbuf, ctdl_info_dir);
179 fp = fopen(i->info, "r");
187 // Test for the presence of a legacy "room picture file"
188 assoc_file_name(i->pic, sizeof i->pic, qrbuf, ctdl_image_dir);
189 fp = fopen(i->pic, "r");
197 if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
206 // Prior to Citadel Server version 902, room info and pictures (which comprise the
207 // displayed banner for each room) were stored in the filesystem. If we are upgrading
208 // from version >000 to version >=902, ingest those files into the database.
209 void ingest_old_roominfo_and_roompic_files(void) {
210 struct iorarf_list *il = NULL;
212 CtdlForEachRoom(iorarf_backend, &il);
214 struct iorarf_list *p;
216 iorarf_oneroom(il->name, il->info, il->pic);
222 unlink(ctdl_info_dir);
226 // For upgrades in which a new config setting appears for the first time, set default values.
227 // For new installations (oldver == 0) also set default values.
228 void update_config(void) {
231 CtdlSetConfigInt("c_rfc822_strict_from", 0);
232 CtdlSetConfigInt("c_purge_hour", 3);
233 CtdlSetConfigInt("c_ldap_port", 389);
234 CtdlSetConfigStr("c_ip_addr", "*");
235 CtdlSetConfigInt("c_enable_fulltext", 1);
236 CtdlSetConfigInt("c_xmpp_c2s_port", 5222);
237 CtdlSetConfigInt("c_xmpp_s2s_port", 5269);
241 CtdlSetConfigInt("c_nntp_port", 119);
242 CtdlSetConfigInt("c_nntps_port", 563);
245 if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
251 // Helper function for move_inet_addrs_from_vcards_to_user_records()
253 // Call this function as a ForEachUser backend in order to queue up
254 // user names, or call it with a null user to make it do the processing.
255 // This allows us to maintain the list as a static instead of passing
257 void miafvtur_backend(char *username, void *data) {
258 struct ctdluser usbuf;
259 char primary_inet_email[512] = { 0 };
260 char other_inet_emails[512] = { 0 };
261 char combined_inet_emails[512] = { 0 };
263 if (CtdlGetUser(&usbuf, username) != 0) {
267 struct vCard *v = vcard_get_user(&usbuf);
269 extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
272 if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
276 snprintf(combined_inet_emails, 512, "%s%s%s",
277 (!IsEmptyStr(primary_inet_email) ? primary_inet_email : ""),
278 ((!IsEmptyStr(primary_inet_email)&&(!IsEmptyStr(other_inet_emails))) ? "|" : ""),
279 (!IsEmptyStr(other_inet_emails) ? other_inet_emails : "")
282 CtdlSetEmailAddressesForUser(usbuf.fullname, combined_inet_emails);
286 // If our system still has a "refcount_adjustments.dat" sitting around from an old version, ingest it now.
287 int ProcessOldStyleAdjRefCountQueue(void) {
290 struct arcq arcq_rec;
291 int num_records_processed = 0;
293 fp = fopen(file_arcq, "rb");
295 return(num_records_processed);
298 syslog(LOG_INFO, "msgbase: ingesting %s", file_arcq);
300 while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
301 AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
302 ++num_records_processed;
306 r = unlink(file_arcq);
308 syslog(LOG_ERR, "%s: %m", file_arcq);
311 return(num_records_processed);
315 // Prior to version 912 we kept a user's various Internet email addresses in their vCards.
316 // This function moves them over to the user record, which is where we keep them now.
317 void move_inet_addrs_from_vcards_to_user_records(void) {
318 ForEachUser(miafvtur_backend, NULL);
319 CtdlRebuildDirectoryIndex();
323 // We found the legacy sieve config in the user's config room. Store the message number in the user record.
324 void mifm_found_config(long msgnum, void *userdata) {
325 struct ctdluser *us = (struct ctdluser *)userdata;
327 us->msgnum_inboxrules = msgnum;
328 syslog(LOG_DEBUG, "user: <%s> inbox filter msgnum: <%ld>", us->fullname, us->msgnum_inboxrules);
332 // Helper function for migrate_inbox_filter_msgnums()
333 void mifm_backend(char *username, void *data) {
335 char roomname[ROOMNAMELEN];
337 if (CtdlGetUserLock(&us, username) == 0) {
338 // Take a spin through the user's personal config room
339 syslog(LOG_DEBUG, "Processing <%s> (%ld)", us.fullname, us.usernum);
340 snprintf(roomname, sizeof roomname, "%010ld.%s", us.usernum, USERCONFIGROOM);
341 if (CtdlGetRoom(&CC->room, roomname) == 0) {
342 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL, mifm_found_config, (void *)&us );
344 CtdlPutUserLock(&us);
349 // Prior to version 930 we used a MIME type search to locate the user's inbox filter rules.
350 // This function locates those ruleset messages and simply stores the message number in the user record.
351 void migrate_inbox_filter_msgnums(void) {
352 ForEachUser(mifm_backend, NULL);
356 // Create a default administrator account so we can log in to a new installation
357 void create_default_admin_account(void) {
358 struct ctdluser usbuf;
360 create_user(DEFAULT_ADMIN_USERNAME, CREATE_USER_DO_NOT_BECOME_USER, (-1));
361 CtdlGetUser(&usbuf, DEFAULT_ADMIN_USERNAME);
362 safestrncpy(usbuf.password, DEFAULT_ADMIN_PASSWORD, sizeof(usbuf.password));
363 usbuf.axlevel = AxAideU;
365 CtdlSetConfigStr("c_sysadm", DEFAULT_ADMIN_USERNAME);
369 void regenerate_secondary_indices(void) {
370 syslog(LOG_INFO, "regenerate_secondary_indices has been activated.");
372 rebuild_usersbynumber();
373 rebuild_euid_index();
374 CtdlRebuildDirectoryIndex();
375 // we also need to trigger the fulltext foo
377 CtdlSetConfigInt("regenerate_secondary_indices", 0);
381 // Based on the server version number reported by the existing database,
382 // run in-place data format upgrades until everything is up to date.
383 void pre_startup_upgrades(void) {
385 oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
386 syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
389 if (oldver < REV_LEVEL) {
390 syslog(LOG_WARNING, "Running pre-startup database upgrades.");
396 if ((oldver > 000) && (oldver < 811)) {
397 syslog(LOG_EMERG, "This database is too old to be upgraded. Citadel server will exit.");
400 if ((oldver > 000) && (oldver < 902)) {
401 ingest_old_roominfo_and_roompic_files();
403 if ((oldver > 000) && (oldver < 973)) { // This was the old extauth table.
404 cdb_trunc(CDB_UNUSED1); // We will do this better someday.
406 if ((oldver > 000) && (oldver < 975)) { // An unrepairable defect was found in this table.
407 cdb_trunc(CDB_USETABLE); // We have to discard it and start over.
410 CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
412 // Negative values for maxsessions are not allowed.
413 if (CtdlGetConfigInt("c_maxsessions") < 0) {
414 CtdlSetConfigInt("c_maxsessions", 0);
417 // We need a system default message expiry policy, because this is
418 // the top level and there's no 'higher' policy to fall back on.
419 // By default, do not expire messages at all.
420 if (CtdlGetConfigInt("c_ep_mode") == 0) {
421 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
422 CtdlSetConfigInt("c_ep_value", 0);
425 // If this is the first run on an empty database, create a default administrator
427 create_default_admin_account();
432 // Based on the server version number reported by the existing database,
433 // run in-place data format upgrades until everything is up to date.
434 void post_startup_upgrades(void) {
436 syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
438 if (oldver < REV_LEVEL) {
439 syslog(LOG_WARNING, "Running post-startup database upgrades.");
441 if ((oldver > 000) && (oldver < 912)) {
442 move_inet_addrs_from_vcards_to_user_records();
445 if ((oldver > 000) && (oldver < 922)) {
446 ProcessOldStyleAdjRefCountQueue();
449 if ((oldver > 000) && (oldver < 930)) {
450 migrate_inbox_filter_msgnums();
456 // Initialization function, called from modules_init.c
457 char *ctdl_module_init_upgrade(void) {
459 post_startup_upgrades();
462 /* return our module name for the log */