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-2016 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"
58 * Fix up the name for Citadel user 0 and try to remove any extra users with number 0
60 void fix_sys_user_name(void)
62 struct ctdluser usbuf;
63 char usernamekey[USERNAME_SIZE];
65 /** If we have a user called Citadel rename them to SYS_Citadel */
66 if (CtdlGetUser(&usbuf, "Citadel") == 0)
68 rename_user("Citadel", "SYS_Citadel");
71 while (CtdlGetUserByNumber(&usbuf, 0) == 0)
73 /* delete user with number 0 and no name */
74 if (IsEmptyStr(usbuf.fullname)) {
75 cdb_delete(CDB_USERS, "", 0);
78 /* temporarily set this user to -1 */
84 /* Make sure user SYS_* is user 0 */
85 while (CtdlGetUserByNumber(&usbuf, -1) == 0)
87 if (strncmp(usbuf.fullname, "SYS_", 4))
88 { /* Delete any user 0 that doesn't start with SYS_ */
89 makeuserkey(usernamekey, usbuf.fullname, cutuserkey(usbuf.fullname));
90 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
101 * Back end processing function for convert_ctdluid_to_minusone()
103 void cbtm_backend(struct ctdluser *usbuf, void *data) {
104 static struct UserProcList *uplist = NULL;
105 struct UserProcList *ptr;
108 /* Lazy programming here. Call this function as a ForEachUser backend
109 * in order to queue up the room names, or call it with a null user
110 * to make it do the processing.
113 ptr = (struct UserProcList *)
114 malloc(sizeof (struct UserProcList));
115 if (ptr == NULL) return;
117 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
123 while (uplist != NULL) {
125 if (CtdlGetUserLock(&us, uplist->user) == 0) {
126 syslog(LOG_DEBUG, "Processing <%s>...", uplist->user);
127 if (us.uid == CTDLUID) {
130 CtdlPutUserLock(&us);
134 uplist = uplist->next;
140 * quick fix to change all CTDLUID users to (-1)
142 void convert_ctdluid_to_minusone(void) {
143 syslog(LOG_WARNING, "Applying uid changes");
144 ForEachUser(cbtm_backend, NULL);
145 cbtm_backend(NULL, NULL);
152 * These accounts may have been created by code that ran between mid 2008 and early 2011.
153 * If present they are no longer in use and may be deleted.
155 void remove_thread_users(void) {
156 char *deleteusers[] = {
165 "SYS_select_on_master",
170 struct ctdluser usbuf;
171 for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
172 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
174 strcpy(usbuf.password, "deleteme");
177 "System user account <%s> is no longer in use and will be deleted.",
186 * Attempt to guess the name of the time zone currently in use
187 * on the underlying host system.
189 void guess_time_zone(void) {
193 fp = popen(file_guesstimezone, "r");
195 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
196 buf[strlen(buf)-1] = 0;
197 CtdlSetConfigStr("c_default_cal_zone", buf);
198 syslog(LOG_INFO, "Configuring timezone: %s", buf);
206 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
208 * This is the second pass, where we process the list of rooms with info or pic files.
210 void iorarf_oneroom(char *roomname, char *infofile, char *picfile)
214 char *unencoded_data;
216 long info_msgnum = 0;
220 // Test for the presence of a legacy "room info file"
221 if (!IsEmptyStr(infofile)) {
222 fp = fopen(infofile, "r");
228 fseek(fp, 0, SEEK_END);
229 data_length = ftell(fp);
231 if (data_length >= 1) {
233 unencoded_data = malloc(data_length);
234 if (unencoded_data) {
235 fread(unencoded_data, data_length, 1, fp);
236 encoded_data = malloc((data_length * 2) + 100);
238 sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n");
239 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
240 snprintf(subject, sizeof subject, "Imported room banner for %s", roomname);
241 info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
244 free(unencoded_data);
248 if (info_msgnum > 0) unlink(infofile);
251 // Test for the presence of a legacy "room picture file" and import it.
252 if (!IsEmptyStr(picfile)) {
253 fp = fopen(picfile, "r");
259 fseek(fp, 0, SEEK_END);
260 data_length = ftell(fp);
262 if (data_length >= 1) {
264 unencoded_data = malloc(data_length);
265 if (unencoded_data) {
266 fread(unencoded_data, data_length, 1, fp);
267 encoded_data = malloc((data_length * 2) + 100);
269 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
270 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
271 snprintf(subject, sizeof subject, "Imported room icon for %s", roomname);
272 pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
275 free(unencoded_data);
279 if (pic_msgnum > 0) unlink(picfile);
282 // Now we have the message numbers of our new banner and icon. Record them in the room record.
283 // NOTE: we are not deleting the old msgnum_info because that position in the record was previously
284 // a pointer to the highest message number which existed in the room when the info file was saved,
285 // and we don't want to delete messages that are not *actually* old banners.
286 struct ctdlroom qrbuf;
287 if (CtdlGetRoomLock(&qrbuf, roomname) == 0) {
288 qrbuf.msgnum_info = info_msgnum;
289 qrbuf.msgnum_pic = pic_msgnum;
290 CtdlPutRoomLock(&qrbuf);
297 struct iorarf_list *next;
298 char name[ROOMNAMELEN];
305 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
307 * This is the first pass, where the list of qualifying rooms is gathered.
309 void iorarf_backend(struct ctdlroom *qrbuf, void *data)
312 struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
314 struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
315 i->next = *iorarf_list;
316 strcpy(i->name, qrbuf->QRname);
320 // Test for the presence of a legacy "room info file"
321 assoc_file_name(i->info, sizeof i->info, qrbuf, ctdl_info_dir);
322 fp = fopen(i->info, "r");
330 // Test for the presence of a legacy "room picture file"
331 assoc_file_name(i->pic, sizeof i->pic, qrbuf, ctdl_image_dir);
332 fp = fopen(i->pic, "r");
340 if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
350 * Prior to Citadel Server version 902, room info and pictures (which comprise the
351 * displayed banner for each room) were stored in the filesystem. If we are upgrading
352 * from version >000 to version >=902, ingest those files into the database.
354 void ingest_old_roominfo_and_roompic_files(void)
356 struct iorarf_list *il = NULL;
358 CtdlForEachRoom(iorarf_backend, &il);
360 struct iorarf_list *p;
362 iorarf_oneroom(il->name, il->info, il->pic);
368 unlink(ctdl_info_dir);
373 * Perform any upgrades that can be done automatically based on our knowledge of the previous
374 * version of Citadel server that was running here.
376 * Note that if the previous version was 0 then this is a new installation running for the first time.
378 void update_config(void) {
380 int oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
383 CtdlSetConfigInt("c_rfc822_strict_from", 0);
387 CtdlSetConfigInt("c_purge_hour", 3);
391 CtdlSetConfigInt("c_ldap_port", 389);
395 CtdlSetConfigStr("c_ip_addr", "*");
399 CtdlSetConfigInt("c_enable_fulltext", 1);
403 CtdlSetConfigInt("c_auto_cull", 1);
407 CtdlSetConfigInt("c_xmpp_c2s_port", 5222);
408 CtdlSetConfigInt("c_xmpp_s2s_port", 5269);
412 CtdlSetConfigInt("c_nntp_port", 119);
413 CtdlSetConfigInt("c_nntps_port", 563);
416 if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
424 * Based on the server version number reported by the existing database,
425 * run in-place data format upgrades until everything is up to date.
427 void check_server_upgrades(void) {
429 syslog(LOG_INFO, "Existing database version on disk is %d", CtdlGetConfigInt("MM_hosted_upgrade_level"));
431 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < REV_LEVEL) {
432 syslog(LOG_WARNING, "Server hosted updates need to be processed at this time. Please wait...");
440 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 591)) {
441 syslog(LOG_EMERG, "This database is too old to be upgraded. Citadel server will exit.");
444 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 608)) {
445 convert_ctdluid_to_minusone();
447 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 659)) {
448 rebuild_euid_index();
450 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 735) {
453 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 736) {
454 rebuild_usersbynumber();
456 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 790) {
457 remove_thread_users();
459 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 810) {
460 struct ctdlroom QRoom;
461 if (!CtdlGetRoom(&QRoom, SMTP_SPOOLOUT_ROOM)) {
462 QRoom.QRdefaultview = VIEW_QUEUE;
465 if (!CtdlGetRoom(&QRoom, FNBL_QUEUE_ROOM)) {
466 QRoom.QRdefaultview = VIEW_QUEUE;
471 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 902)) {
472 ingest_old_roominfo_and_roompic_files();
475 CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
478 * Negative values for maxsessions are not allowed.
480 if (CtdlGetConfigInt("c_maxsessions") < 0) {
481 CtdlSetConfigInt("c_maxsessions", 0);
484 /* We need a system default message expiry policy, because this is
485 * the top level and there's no 'higher' policy to fall back on.
486 * By default, do not expire messages at all.
488 if (CtdlGetConfigInt("c_ep_mode") == 0) {
489 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
490 CtdlSetConfigInt("c_ep_value", 0);
495 CTDL_MODULE_UPGRADE(upgrade)
497 check_server_upgrades();
499 /* return our module id for the Log */