Blockquotes are now rendered to console as dim instead of italic
[citadel.git] / citadel / server / modules / upgrade / serv_upgrade.c
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.
5 //
6 // Copyright (c) 1987-2023 by the citadel.org team
7 //
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.
10 //
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.
15
16 #include "../../sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <time.h>
26 #include <sys/wait.h>
27 #include <string.h>
28 #include <limits.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"
45
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.
49 int oldver = 0;
50
51
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) {
55         FILE *fp;
56         char buf[PATH_MAX];
57
58         fp = popen(file_guesstimezone, "r");
59         if (fp) {
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);
64                 }
65                 fclose(fp);
66         }
67 }
68
69
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) {
73         FILE *fp;
74         long data_length;
75         char *unencoded_data;
76         char *encoded_data;
77         long info_msgnum = 0;
78         long pic_msgnum = 0;
79         char subject[SIZ];
80
81         // Test for the presence of a legacy "room info file"
82         if (!IsEmptyStr(infofile)) {
83                 fp = fopen(infofile, "r");
84         }
85         else {
86                 fp = NULL;
87         }
88         if (fp) {
89                 fseek(fp, 0, SEEK_END);
90                 data_length = ftell(fp);
91
92                 if (data_length >= 1) {
93                         rewind(fp);
94                         unencoded_data = malloc(data_length);
95                         if (unencoded_data) {
96                                 fread(unencoded_data, data_length, 1, fp);
97                                 encoded_data = malloc((data_length * 2) + 100);
98                                 if (encoded_data) {
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);
103                                         free(encoded_data);
104                                 }
105                                 free(unencoded_data);
106                         }
107                 }
108                 fclose(fp);
109                 if (info_msgnum > 0) unlink(infofile);
110         }
111
112         // Test for the presence of a legacy "room picture file" and import it.
113         if (!IsEmptyStr(picfile)) {
114                 fp = fopen(picfile, "r");
115         }
116         else {
117                 fp = NULL;
118         }
119         if (fp) {
120                 fseek(fp, 0, SEEK_END);
121                 data_length = ftell(fp);
122
123                 if (data_length >= 1) {
124                         rewind(fp);
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);
129                                 if (encoded_data) {
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);
134                                         free(encoded_data);
135                                 }
136                                 free(unencoded_data);
137                         }
138                 }
139                 fclose(fp);
140                 if (pic_msgnum > 0) unlink(picfile);
141         }
142
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);
152         }
153
154 }
155
156
157 struct iorarf_list {
158         struct iorarf_list *next;
159         char name[ROOMNAMELEN];
160         char info[PATH_MAX];
161         char pic[PATH_MAX];
162 };
163
164
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) {
168         FILE *fp;
169         struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
170
171         struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
172         i->next = *iorarf_list;
173         strcpy(i->name, qrbuf->QRname);
174         strcpy(i->info, "");
175         strcpy(i->pic, "");
176
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");
180         if (fp) {
181                 fclose(fp);
182         }
183         else {
184                 i->info[0] = 0;
185         }
186
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");
190         if (fp) {
191                 fclose(fp);
192         }
193         else {
194                 i->pic[0] = 0;
195         }
196
197         if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
198                 *iorarf_list = i;
199         }
200         else {
201                 free(i);
202         }
203 }
204
205
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;
211
212         CtdlForEachRoom(iorarf_backend, &il);
213
214         struct iorarf_list *p;
215         while (il) {
216                 iorarf_oneroom(il->name, il->info, il->pic);
217                 p = il->next;
218                 free(il);
219                 il = p;
220         }
221
222         unlink(ctdl_info_dir);
223 }
224
225
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) {
229
230         if (oldver < 811) {
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);
238         }
239
240         if (oldver < 830) {
241                 CtdlSetConfigInt("c_nntp_port", 119);
242                 CtdlSetConfigInt("c_nntps_port", 563);
243         }
244
245         if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
246                 guess_time_zone();
247         }
248 }
249
250
251 // Helper function for move_inet_addrs_from_vcards_to_user_records()
252 //
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
256 // pointers around.
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 };
262
263         if (CtdlGetUser(&usbuf, username) != 0) {
264                 return;
265         }
266
267         struct vCard *v = vcard_get_user(&usbuf);
268         if (!v) return;
269         extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
270         vcard_free(v);
271         
272         if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
273                 return;
274         }
275
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 : "")
280         );
281
282         CtdlSetEmailAddressesForUser(usbuf.fullname, combined_inet_emails);
283 }
284
285
286 // If our system still has a "refcount_adjustments.dat" sitting around from an old version, ingest it now.
287 int ProcessOldStyleAdjRefCountQueue(void) {
288         int r;
289         FILE *fp;
290         struct arcq arcq_rec;
291         int num_records_processed = 0;
292
293         fp = fopen(file_arcq, "rb");
294         if (fp == NULL) {
295                 return(num_records_processed);
296         }
297
298         syslog(LOG_INFO, "msgbase: ingesting %s", file_arcq);
299
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;
303         }
304
305         fclose(fp);
306         r = unlink(file_arcq);
307         if (r != 0) {
308                 syslog(LOG_ERR, "%s: %m", file_arcq);
309         }
310
311         return(num_records_processed);
312 }
313
314
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();
320 }
321
322
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;
326
327         us->msgnum_inboxrules = msgnum;
328         syslog(LOG_DEBUG, "user: <%s> inbox filter msgnum: <%ld>", us->fullname, us->msgnum_inboxrules);
329 }
330
331
332 // Helper function for migrate_inbox_filter_msgnums()
333 void mifm_backend(char *username, void *data) {
334         struct ctdluser us;
335         char roomname[ROOMNAMELEN];
336
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 );
343                 }
344                 CtdlPutUserLock(&us);
345         }
346 }
347
348
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);
353 }
354
355
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;
359
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;
364         CtdlPutUser(&usbuf);
365         CtdlSetConfigStr("c_sysadm", DEFAULT_ADMIN_USERNAME);
366 }
367
368
369 void regenerate_secondary_indices(void) {
370         syslog(LOG_INFO, "regenerate_secondary_indices has been activated.");
371
372         rebuild_usersbynumber();
373         rebuild_euid_index();
374         CtdlRebuildDirectoryIndex();
375         // we also need to trigger the fulltext foo
376
377         CtdlSetConfigInt("regenerate_secondary_indices", 0);
378 }
379
380
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) {
384
385         oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
386         syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
387         update_config();
388
389         if (oldver < REV_LEVEL) {
390                 syslog(LOG_WARNING, "Running pre-startup database upgrades.");
391         }
392         else {
393                 return;
394         }
395
396         // After 2026-may-06 we will do the following:
397         // 1. Set the oldest upgradable version to 902 (it will then be ten years old)
398         // 2. Remove support for upgrading from the legacy config and control file formats.
399
400         // Currently, the oldest upgradable version is 811, which was released on 2012-may-21
401
402         if ((oldver > 000) && (oldver < 811)) {
403                 syslog(LOG_EMERG, "This database is too old to be upgraded.  Citadel server will exit.");
404                 exit(EXIT_FAILURE);
405         }
406         if ((oldver > 000) && (oldver < 902)) {
407                 ingest_old_roominfo_and_roompic_files();
408         }
409         if ((oldver > 000) && (oldver < 973)) {                         // This was the old extauth table.
410                 cdb_trunc(CDB_UNUSED1);                                 // We will do this better someday.
411         }
412         if ((oldver > 000) && (oldver < 975)) {                         // An unrepairable defect was found in this table.
413                 cdb_trunc(CDB_USETABLE);                                // We have to discard it and start over.
414         }
415
416         CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
417
418         // Negative values for maxsessions are not allowed.
419         if (CtdlGetConfigInt("c_maxsessions") < 0) {
420                 CtdlSetConfigInt("c_maxsessions", 0);
421         }
422
423         // We need a system default message expiry policy, because this is
424         // the top level and there's no 'higher' policy to fall back on.
425         // By default, do not expire messages at all.
426         if (CtdlGetConfigInt("c_ep_mode") == 0) {
427                 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
428                 CtdlSetConfigInt("c_ep_value", 0);
429         }
430
431         // If this is the first run on an empty database, create a default administrator
432         if (oldver == 0) {
433                 create_default_admin_account();
434         }
435 }
436
437
438 // Based on the server version number reported by the existing database,
439 // run in-place data format upgrades until everything is up to date.
440 void post_startup_upgrades(void) {
441
442         syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
443
444         if (oldver < REV_LEVEL) {
445                 syslog(LOG_WARNING, "Running post-startup database upgrades.");
446
447                 if ((oldver > 000) && (oldver < 912)) {
448                         move_inet_addrs_from_vcards_to_user_records();
449                 }
450         
451                 if ((oldver > 000) && (oldver < 922)) {
452                         ProcessOldStyleAdjRefCountQueue();
453                 }
454         
455                 if ((oldver > 000) && (oldver < 930)) {
456                         migrate_inbox_filter_msgnums();
457                 }
458         }
459 }
460
461
462 // Initialization function, called from modules_init.c
463 char *ctdl_module_init_upgrade(void) {
464         if (!threading) {
465                 post_startup_upgrades();
466         }
467
468         /* return our module name for the log */
469         return "upgrade";
470 }