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-2017 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"
60 * Fix up the name for Citadel user 0 and try to remove any extra users with number 0
62 void fix_sys_user_name(void)
64 struct ctdluser usbuf;
65 char usernamekey[USERNAME_SIZE];
67 /** If we have a user called Citadel rename them to SYS_Citadel */
68 if (CtdlGetUser(&usbuf, "Citadel") == 0)
70 rename_user("Citadel", "SYS_Citadel");
73 while (CtdlGetUserByNumber(&usbuf, 0) == 0)
75 /* delete user with number 0 and no name */
76 if (IsEmptyStr(usbuf.fullname)) {
77 cdb_delete(CDB_USERS, "", 0);
80 /* temporarily set this user to -1 */
86 /* Make sure user SYS_* is user 0 */
87 while (CtdlGetUserByNumber(&usbuf, -1) == 0)
89 if (strncmp(usbuf.fullname, "SYS_", 4))
90 { /* Delete any user 0 that doesn't start with SYS_ */
91 makeuserkey(usernamekey, usbuf.fullname, cutuserkey(usbuf.fullname));
92 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
103 * Back end processing function for reindex_uids()
104 * Call this function as a ForEachUser backend in order to queue up
105 * user names, or call it with a null user to make it do the processing.
106 * This allows us to maintain the list as a static instead of passing
109 void reindex_uids_backend(struct ctdluser *usbuf, void *data) {
110 static struct UserProcList *uplist = NULL;
111 struct UserProcList *ptr;
114 /* this is the calling mode where we add a user */
117 ptr = (struct UserProcList *) malloc(sizeof (struct UserProcList));
122 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
128 /* this is the calling mode where we do the processing */
130 while (uplist != NULL) {
132 if (CtdlGetUserLock(&us, uplist->user) == 0) {
133 syslog(LOG_DEBUG, "Processing <%s> (%d)", uplist->user, us.uid);
134 if (us.uid == CTDLUID) {
135 us.uid = NATIVE_AUTH_UID;
137 CtdlPutUserLock(&us);
138 if (us.uid > 0) { // if non-native auth , index by uid
139 StrBuf *claimed_id = NewStrBuf();
140 StrBufPrintf(claimed_id, "uid:%d", us.uid);
141 attach_extauth(&us, claimed_id);
142 FreeStrBuf(&claimed_id);
147 uplist = uplist->next;
153 * Build extauth index of all users with uid-based join (system auth, LDAP auth)
154 * Also changes all users with a uid of CTDLUID to NATIVE_AUTH_UID (-1)
156 void reindex_uids(void) {
157 syslog(LOG_WARNING, "upgrade: reindexing and applying uid changes");
158 ForEachUser(reindex_uids_backend, NULL);
159 reindex_uids_backend(NULL, NULL);
166 * These accounts may have been created by code that ran between mid 2008 and early 2011.
167 * If present they are no longer in use and may be deleted.
169 void remove_thread_users(void) {
170 char *deleteusers[] = {
179 "SYS_select_on_master",
184 struct ctdluser usbuf;
185 for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
186 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
188 strcpy(usbuf.password, "deleteme");
191 "System user account <%s> is no longer in use and will be deleted.",
200 * Attempt to guess the name of the time zone currently in use
201 * on the underlying host system.
203 void guess_time_zone(void) {
207 fp = popen(file_guesstimezone, "r");
209 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
210 buf[strlen(buf)-1] = 0;
211 CtdlSetConfigStr("c_default_cal_zone", buf);
212 syslog(LOG_INFO, "Configuring timezone: %s", buf);
220 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
222 * This is the second pass, where we process the list of rooms with info or pic files.
224 void iorarf_oneroom(char *roomname, char *infofile, char *picfile)
228 char *unencoded_data;
230 long info_msgnum = 0;
234 // Test for the presence of a legacy "room info file"
235 if (!IsEmptyStr(infofile)) {
236 fp = fopen(infofile, "r");
242 fseek(fp, 0, SEEK_END);
243 data_length = ftell(fp);
245 if (data_length >= 1) {
247 unencoded_data = malloc(data_length);
248 if (unencoded_data) {
249 fread(unencoded_data, data_length, 1, fp);
250 encoded_data = malloc((data_length * 2) + 100);
252 sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n");
253 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
254 snprintf(subject, sizeof subject, "Imported room banner for %s", roomname);
255 info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
258 free(unencoded_data);
262 if (info_msgnum > 0) unlink(infofile);
265 // Test for the presence of a legacy "room picture file" and import it.
266 if (!IsEmptyStr(picfile)) {
267 fp = fopen(picfile, "r");
273 fseek(fp, 0, SEEK_END);
274 data_length = ftell(fp);
276 if (data_length >= 1) {
278 unencoded_data = malloc(data_length);
279 if (unencoded_data) {
280 fread(unencoded_data, data_length, 1, fp);
281 encoded_data = malloc((data_length * 2) + 100);
283 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
284 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
285 snprintf(subject, sizeof subject, "Imported room icon for %s", roomname);
286 pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
289 free(unencoded_data);
293 if (pic_msgnum > 0) unlink(picfile);
296 // Now we have the message numbers of our new banner and icon. Record them in the room record.
297 // NOTE: we are not deleting the old msgnum_info because that position in the record was previously
298 // a pointer to the highest message number which existed in the room when the info file was saved,
299 // and we don't want to delete messages that are not *actually* old banners.
300 struct ctdlroom qrbuf;
301 if (CtdlGetRoomLock(&qrbuf, roomname) == 0) {
302 qrbuf.msgnum_info = info_msgnum;
303 qrbuf.msgnum_pic = pic_msgnum;
304 CtdlPutRoomLock(&qrbuf);
311 struct iorarf_list *next;
312 char name[ROOMNAMELEN];
319 * Per-room callback function for ingest_old_roominfo_and_roompic_files()
321 * This is the first pass, where the list of qualifying rooms is gathered.
323 void iorarf_backend(struct ctdlroom *qrbuf, void *data)
326 struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
328 struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
329 i->next = *iorarf_list;
330 strcpy(i->name, qrbuf->QRname);
334 // Test for the presence of a legacy "room info file"
335 assoc_file_name(i->info, sizeof i->info, qrbuf, ctdl_info_dir);
336 fp = fopen(i->info, "r");
344 // Test for the presence of a legacy "room picture file"
345 assoc_file_name(i->pic, sizeof i->pic, qrbuf, ctdl_image_dir);
346 fp = fopen(i->pic, "r");
354 if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
364 * Prior to Citadel Server version 902, room info and pictures (which comprise the
365 * displayed banner for each room) were stored in the filesystem. If we are upgrading
366 * from version >000 to version >=902, ingest those files into the database.
368 void ingest_old_roominfo_and_roompic_files(void)
370 struct iorarf_list *il = NULL;
372 CtdlForEachRoom(iorarf_backend, &il);
374 struct iorarf_list *p;
376 iorarf_oneroom(il->name, il->info, il->pic);
382 unlink(ctdl_info_dir);
387 * Perform any upgrades that can be done automatically based on our knowledge of the previous
388 * version of Citadel server that was running here.
390 * Note that if the previous version was 0 then this is a new installation running for the first time.
392 void update_config(void) {
394 int oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
397 CtdlSetConfigInt("c_rfc822_strict_from", 0);
401 CtdlSetConfigInt("c_purge_hour", 3);
405 CtdlSetConfigInt("c_ldap_port", 389);
409 CtdlSetConfigStr("c_ip_addr", "*");
413 CtdlSetConfigInt("c_enable_fulltext", 1);
417 CtdlSetConfigInt("c_auto_cull", 1);
421 CtdlSetConfigInt("c_xmpp_c2s_port", 5222);
422 CtdlSetConfigInt("c_xmpp_s2s_port", 5269);
426 CtdlSetConfigInt("c_nntp_port", 119);
427 CtdlSetConfigInt("c_nntps_port", 563);
430 if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
437 * Helper function for move_inet_addrs_from_vcards_to_user_records()
439 * Call this function as a ForEachUser backend in order to queue up
440 * user names, or call it with a null user to make it do the processing.
441 * This allows us to maintain the list as a static instead of passing
444 void miafvtur_backend(struct ctdluser *usbuf, void *data) {
451 static struct miafvtur *m = NULL;
452 static int num_m = 0;
453 static int alloc_m = 0;
455 /* this is the calling mode where we add a user */
458 char primary_inet_email[512] = { 0 };
459 char other_inet_emails[512] = { 0 };
460 struct vCard *v = vcard_get_user(usbuf);
462 extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
465 if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
469 if (num_m >= alloc_m) {
472 m = malloc(sizeof(struct miafvtur) * alloc_m);
476 m = realloc(m, (sizeof(struct miafvtur) * alloc_m));
480 strcpy(m[num_m].name, usbuf->fullname);
481 snprintf(m[num_m].emails, 512, "%s%s%s",
482 (!IsEmptyStr(primary_inet_email) ? primary_inet_email : ""),
483 ((!IsEmptyStr(primary_inet_email)&&(!IsEmptyStr(other_inet_emails))) ? "|" : ""),
484 (!IsEmptyStr(other_inet_emails) ? other_inet_emails : "")
490 /* this is the calling mode where we do the processing */
495 for (i=0; i<num_m; ++i) {
496 syslog(LOG_DEBUG, "<%s> = <%s>", m[i].name, m[i].emails);
497 if (CtdlGetUser(&u, m[i].name) == 0) {
498 safestrncpy(u.emailaddrs, m[i].emails, sizeof u.emailaddrs);
510 * Prior to version 912 we kept a user's various Internet email addresses in their vCards.
511 * This function moves them over to the user record, which is where we keep them now.
513 void move_inet_addrs_from_vcards_to_user_records(void)
515 ForEachUser(miafvtur_backend, NULL);
516 miafvtur_backend(NULL, NULL);
517 CtdlRebuildDirectoryIndex();
522 * Based on the server version number reported by the existing database,
523 * run in-place data format upgrades until everything is up to date.
525 void check_server_upgrades(void) {
527 syslog(LOG_INFO, "Existing database version on disk is %d", CtdlGetConfigInt("MM_hosted_upgrade_level"));
531 reindex_uids(); // FIXME FIXME FIXME remove this line after testing
534 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < REV_LEVEL) {
535 syslog(LOG_WARNING, "Server hosted updates need to be processed at this time. Please wait...");
543 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 591)) {
544 syslog(LOG_EMERG, "This database is too old to be upgraded. Citadel server will exit.");
547 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 913)) {
550 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 659)) {
551 rebuild_euid_index();
553 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 735) {
556 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 736) {
557 rebuild_usersbynumber();
559 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 790) {
560 remove_thread_users();
562 if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 810) {
563 struct ctdlroom QRoom;
564 if (!CtdlGetRoom(&QRoom, SMTP_SPOOLOUT_ROOM)) {
565 QRoom.QRdefaultview = VIEW_QUEUE;
570 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 902)) {
571 ingest_old_roominfo_and_roompic_files();
574 if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 912)) {
575 move_inet_addrs_from_vcards_to_user_records();
578 CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
581 * Negative values for maxsessions are not allowed.
583 if (CtdlGetConfigInt("c_maxsessions") < 0) {
584 CtdlSetConfigInt("c_maxsessions", 0);
587 /* We need a system default message expiry policy, because this is
588 * the top level and there's no 'higher' policy to fall back on.
589 * By default, do not expire messages at all.
591 if (CtdlGetConfigInt("c_ep_mode") == 0) {
592 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
593 CtdlSetConfigInt("c_ep_value", 0);
598 CTDL_MODULE_UPGRADE(upgrade)
600 check_server_upgrades();
602 /* return our module id for the Log */