]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/upgrade/serv_upgrade.c
citserver: regenerate secondary indices if the regenerate_secondary_indices config...
[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 // Try to remove any extra users with number 0
52 void fix_sys_user_name(void) {
53         struct ctdluser usbuf;
54         char usernamekey[USERNAME_SIZE];
55
56         while (CtdlGetUserByNumber(&usbuf, 0) == 0) {
57                 // delete user with number 0 and no name
58                 if (IsEmptyStr(usbuf.fullname)) {
59                         cdb_delete(CDB_USERS, "", 0);
60                 }
61                 else {
62                         // temporarily set this user to -1
63                         usbuf.usernum = -1;
64                         CtdlPutUser(&usbuf);
65                 }
66         }
67
68         // Delete any "user 0" accounts
69         while (CtdlGetUserByNumber(&usbuf, -1) == 0) {
70                 makeuserkey(usernamekey, usbuf.fullname);
71                 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
72         }
73 }
74
75
76 // These accounts may have been created by code that ran between mid 2008 and early 2011.
77 // If present they are no longer in use and may be deleted.
78 void remove_thread_users(void) {
79         char *deleteusers[] = {
80                 "SYS_checkpoint",
81                 "SYS_extnotify",
82                 "SYS_IGnet Queue",
83                 "SYS_indexer",
84                 "SYS_network",
85                 "SYS_popclient",
86                 "SYS_purger",
87                 "SYS_rssclient",
88                 "SYS_select_on_master",
89                 "SYS_SMTP Send"
90         };
91
92         int i;
93         struct ctdluser usbuf;
94         for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
95                 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
96                         usbuf.axlevel = 0;
97                         strcpy(usbuf.password, "deleteme");
98                         CtdlPutUser(&usbuf);
99                         syslog(LOG_INFO,
100                                 "System user account <%s> is no longer in use and will be deleted.",
101                                 deleteusers[i]
102                         );
103                 }
104         }
105 }
106
107
108 // Attempt to guess the name of the time zone currently in use
109 // on the underlying host system.
110 void guess_time_zone(void) {
111         FILE *fp;
112         char buf[PATH_MAX];
113
114         fp = popen(file_guesstimezone, "r");
115         if (fp) {
116                 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
117                         buf[strlen(buf)-1] = 0;
118                         CtdlSetConfigStr("c_default_cal_zone", buf);
119                         syslog(LOG_INFO, "Configuring timezone: %s", buf);
120                 }
121                 fclose(fp);
122         }
123 }
124
125
126 // Per-room callback function for ingest_old_roominfo_and_roompic_files()
127 // This is the second pass, where we process the list of rooms with info or pic files.
128 void iorarf_oneroom(char *roomname, char *infofile, char *picfile) {
129         FILE *fp;
130         long data_length;
131         char *unencoded_data;
132         char *encoded_data;
133         long info_msgnum = 0;
134         long pic_msgnum = 0;
135         char subject[SIZ];
136
137         // Test for the presence of a legacy "room info file"
138         if (!IsEmptyStr(infofile)) {
139                 fp = fopen(infofile, "r");
140         }
141         else {
142                 fp = NULL;
143         }
144         if (fp) {
145                 fseek(fp, 0, SEEK_END);
146                 data_length = ftell(fp);
147
148                 if (data_length >= 1) {
149                         rewind(fp);
150                         unencoded_data = malloc(data_length);
151                         if (unencoded_data) {
152                                 fread(unencoded_data, data_length, 1, fp);
153                                 encoded_data = malloc((data_length * 2) + 100);
154                                 if (encoded_data) {
155                                         sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n");
156                                         CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
157                                         snprintf(subject, sizeof subject, "Imported room banner for %s", roomname);
158                                         info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
159                                         free(encoded_data);
160                                 }
161                                 free(unencoded_data);
162                         }
163                 }
164                 fclose(fp);
165                 if (info_msgnum > 0) unlink(infofile);
166         }
167
168         // Test for the presence of a legacy "room picture file" and import it.
169         if (!IsEmptyStr(picfile)) {
170                 fp = fopen(picfile, "r");
171         }
172         else {
173                 fp = NULL;
174         }
175         if (fp) {
176                 fseek(fp, 0, SEEK_END);
177                 data_length = ftell(fp);
178
179                 if (data_length >= 1) {
180                         rewind(fp);
181                         unencoded_data = malloc(data_length);
182                         if (unencoded_data) {
183                                 fread(unencoded_data, data_length, 1, fp);
184                                 encoded_data = malloc((data_length * 2) + 100);
185                                 if (encoded_data) {
186                                         sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
187                                         CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
188                                         snprintf(subject, sizeof subject, "Imported room icon for %s", roomname);
189                                         pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
190                                         free(encoded_data);
191                                 }
192                                 free(unencoded_data);
193                         }
194                 }
195                 fclose(fp);
196                 if (pic_msgnum > 0) unlink(picfile);
197         }
198
199         // Now we have the message numbers of our new banner and icon.  Record them in the room record.
200         // NOTE: we are not deleting the old msgnum_info because that position in the record was previously
201         // a pointer to the highest message number which existed in the room when the info file was saved,
202         // and we don't want to delete messages that are not *actually* old banners.
203         struct ctdlroom qrbuf;
204         if (CtdlGetRoomLock(&qrbuf, roomname) == 0) {
205                 qrbuf.msgnum_info = info_msgnum;
206                 qrbuf.msgnum_pic = pic_msgnum;
207                 CtdlPutRoomLock(&qrbuf);
208         }
209
210 }
211
212
213 struct iorarf_list {
214         struct iorarf_list *next;
215         char name[ROOMNAMELEN];
216         char info[PATH_MAX];
217         char pic[PATH_MAX];
218 };
219
220
221 // Per-room callback function for ingest_old_roominfo_and_roompic_files()
222 // This is the first pass, where the list of qualifying rooms is gathered.
223 void iorarf_backend(struct ctdlroom *qrbuf, void *data) {
224         FILE *fp;
225         struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
226
227         struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
228         i->next = *iorarf_list;
229         strcpy(i->name, qrbuf->QRname);
230         strcpy(i->info, "");
231         strcpy(i->pic, "");
232
233         // Test for the presence of a legacy "room info file"
234         assoc_file_name(i->info, sizeof i->info, qrbuf, ctdl_info_dir);
235         fp = fopen(i->info, "r");
236         if (fp) {
237                 fclose(fp);
238         }
239         else {
240                 i->info[0] = 0;
241         }
242
243         // Test for the presence of a legacy "room picture file"
244         assoc_file_name(i->pic, sizeof i->pic, qrbuf, ctdl_image_dir);
245         fp = fopen(i->pic, "r");
246         if (fp) {
247                 fclose(fp);
248         }
249         else {
250                 i->pic[0] = 0;
251         }
252
253         if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
254                 *iorarf_list = i;
255         }
256         else {
257                 free(i);
258         }
259 }
260
261
262 // Prior to Citadel Server version 902, room info and pictures (which comprise the
263 // displayed banner for each room) were stored in the filesystem.  If we are upgrading
264 // from version >000 to version >=902, ingest those files into the database.
265 void ingest_old_roominfo_and_roompic_files(void) {
266         struct iorarf_list *il = NULL;
267
268         CtdlForEachRoom(iorarf_backend, &il);
269
270         struct iorarf_list *p;
271         while (il) {
272                 iorarf_oneroom(il->name, il->info, il->pic);
273                 p = il->next;
274                 free(il);
275                 il = p;
276         }
277
278         unlink(ctdl_info_dir);
279 }
280
281
282 // For upgrades in which a new config setting appears for the first time, set default values.
283 // For new installations (oldver == 0) also set default values.
284 void update_config(void) {
285
286         if (oldver < 606) {
287                 CtdlSetConfigInt("c_rfc822_strict_from", 0);
288         }
289
290         if (oldver < 609) {
291                 CtdlSetConfigInt("c_purge_hour", 3);
292         }
293
294         if (oldver < 615) {
295                 CtdlSetConfigInt("c_ldap_port", 389);
296         }
297
298         if (oldver < 623) {
299                 CtdlSetConfigStr("c_ip_addr", "*");
300         }
301
302         if (oldver < 650) {
303                 CtdlSetConfigInt("c_enable_fulltext", 1);
304         }
305
306         if (oldver < 652) {
307                 CtdlSetConfigInt("c_auto_cull", 1);
308         }
309
310         if (oldver < 725) {
311                 CtdlSetConfigInt("c_xmpp_c2s_port", 5222);
312                 CtdlSetConfigInt("c_xmpp_s2s_port", 5269);
313         }
314
315         if (oldver < 830) {
316                 CtdlSetConfigInt("c_nntp_port", 119);
317                 CtdlSetConfigInt("c_nntps_port", 563);
318         }
319
320         if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
321                 guess_time_zone();
322         }
323 }
324
325
326 // Helper function for move_inet_addrs_from_vcards_to_user_records()
327 //
328 // Call this function as a ForEachUser backend in order to queue up
329 // user names, or call it with a null user to make it do the processing.
330 // This allows us to maintain the list as a static instead of passing
331 // pointers around.
332 void miafvtur_backend(char *username, void *data) {
333         struct ctdluser usbuf;
334         char primary_inet_email[512] = { 0 };
335         char other_inet_emails[512] = { 0 };
336         char combined_inet_emails[512] = { 0 };
337
338         if (CtdlGetUser(&usbuf, username) != 0) {
339                 return;
340         }
341
342         struct vCard *v = vcard_get_user(&usbuf);
343         if (!v) return;
344         extract_inet_email_addrs(primary_inet_email, sizeof primary_inet_email, other_inet_emails, sizeof other_inet_emails, v, 1);
345         vcard_free(v);
346         
347         if ( (IsEmptyStr(primary_inet_email)) && (IsEmptyStr(other_inet_emails)) ) {
348                 return;
349         }
350
351         snprintf(combined_inet_emails, 512, "%s%s%s",
352                 (!IsEmptyStr(primary_inet_email) ? primary_inet_email : ""),
353                 ((!IsEmptyStr(primary_inet_email)&&(!IsEmptyStr(other_inet_emails))) ? "|" : ""),
354                 (!IsEmptyStr(other_inet_emails) ? other_inet_emails : "")
355         );
356
357         CtdlSetEmailAddressesForUser(usbuf.fullname, combined_inet_emails);
358 }
359
360
361 // If our system still has a "refcount_adjustments.dat" sitting around from an old version, ingest it now.
362 int ProcessOldStyleAdjRefCountQueue(void) {
363         int r;
364         FILE *fp;
365         struct arcq arcq_rec;
366         int num_records_processed = 0;
367
368         fp = fopen(file_arcq, "rb");
369         if (fp == NULL) {
370                 return(num_records_processed);
371         }
372
373         syslog(LOG_INFO, "msgbase: ingesting %s", file_arcq);
374
375         while (fread(&arcq_rec, sizeof(struct arcq), 1, fp) == 1) {
376                 AdjRefCount(arcq_rec.arcq_msgnum, arcq_rec.arcq_delta);
377                 ++num_records_processed;
378         }
379
380         fclose(fp);
381         r = unlink(file_arcq);
382         if (r != 0) {
383                 syslog(LOG_ERR, "%s: %m", file_arcq);
384         }
385
386         return(num_records_processed);
387 }
388
389
390 // Prior to version 912 we kept a user's various Internet email addresses in their vCards.
391 // This function moves them over to the user record, which is where we keep them now.
392 void move_inet_addrs_from_vcards_to_user_records(void) {
393         ForEachUser(miafvtur_backend, NULL);
394         CtdlRebuildDirectoryIndex();
395 }
396
397
398 // We found the legacy sieve config in the user's config room.  Store the message number in the user record.
399 void mifm_found_config(long msgnum, void *userdata) {
400         struct ctdluser *us = (struct ctdluser *)userdata;
401
402         us->msgnum_inboxrules = msgnum;
403         syslog(LOG_DEBUG, "user: <%s> inbox filter msgnum: <%ld>", us->fullname, us->msgnum_inboxrules);
404 }
405
406
407 // Helper function for migrate_inbox_filter_msgnums()
408 void mifm_backend(char *username, void *data) {
409         struct ctdluser us;
410         char roomname[ROOMNAMELEN];
411
412         if (CtdlGetUserLock(&us, username) == 0) {
413                 // Take a spin through the user's personal config room
414                 syslog(LOG_DEBUG, "Processing <%s> (%ld)", us.fullname, us.usernum);
415                 snprintf(roomname, sizeof roomname, "%010ld.%s", us.usernum, USERCONFIGROOM);
416                 if (CtdlGetRoom(&CC->room, roomname) == 0) {
417                         CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL, mifm_found_config, (void *)&us );
418                 }
419                 CtdlPutUserLock(&us);
420         }
421 }
422
423
424 // Prior to version 930 we used a MIME type search to locate the user's inbox filter rules. 
425 // This function locates those ruleset messages and simply stores the message number in the user record.
426 void migrate_inbox_filter_msgnums(void) {
427         ForEachUser(mifm_backend, NULL);
428 }
429
430
431 // Create a default administrator account so we can log in to a new installation
432 void create_default_admin_account(void) {
433         struct ctdluser usbuf;
434
435         create_user(DEFAULT_ADMIN_USERNAME, CREATE_USER_DO_NOT_BECOME_USER, (-1));
436         CtdlGetUser(&usbuf, DEFAULT_ADMIN_USERNAME);
437         safestrncpy(usbuf.password, DEFAULT_ADMIN_PASSWORD, sizeof(usbuf.password));
438         usbuf.axlevel = AxAideU;
439         CtdlPutUser(&usbuf);
440         CtdlSetConfigStr("c_sysadm", DEFAULT_ADMIN_USERNAME);
441 }
442
443
444 void regenerate_secondary_indices(void) {
445         syslog(LOG_INFO, "regenerate_secondary_indices has been activated.");
446
447         rebuild_usersbynumber();
448         rebuild_euid_index();
449         CtdlRebuildDirectoryIndex();
450         // we also need to trigger the fulltext foo
451
452         CtdlSetConfigInt("regenerate_secondary_indices", 0);
453 }
454
455
456 // Based on the server version number reported by the existing database,
457 // run in-place data format upgrades until everything is up to date.
458 void pre_startup_upgrades(void) {
459
460         oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
461         syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
462         update_config();
463
464         if (oldver < REV_LEVEL) {
465                 syslog(LOG_WARNING, "Running pre-startup database upgrades.");
466         }
467         else {
468                 return;
469         }
470
471         if ((oldver > 000) && (oldver < 591)) {
472                 syslog(LOG_EMERG, "This database is too old to be upgraded.  Citadel server will exit.");
473                 exit(EXIT_FAILURE);
474         }
475         if ((oldver > 000) && (oldver < 659)) {
476                 rebuild_euid_index();
477         }
478         if (oldver < 735) {
479                 fix_sys_user_name();
480         }
481         if (oldver < 736) {
482                 rebuild_usersbynumber();
483         }
484         if (oldver < 790) {
485                 remove_thread_users();
486         }
487         if (oldver < 810) {
488                 struct ctdlroom QRoom;
489                 if (!CtdlGetRoom(&QRoom, SMTP_SPOOLOUT_ROOM)) {
490                         QRoom.QRdefaultview = VIEW_QUEUE;
491                         CtdlPutRoom(&QRoom);
492                 }
493         }
494         if ((oldver > 000) && (oldver < 902)) {
495                 ingest_old_roominfo_and_roompic_files();
496         }
497         if ((oldver > 000) && (oldver < 973)) {                         // This was the old extauth table.
498                 cdb_trunc(CDB_UNUSED1);                                 // We will do this better someday.
499         }
500         if ((oldver > 000) && (oldver < 975)) {                         // An unrepairable defect was found in this table.
501                 cdb_trunc(CDB_USETABLE);                                // We have to discard it and start over.
502         }
503
504         CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
505
506         // Negative values for maxsessions are not allowed.
507         if (CtdlGetConfigInt("c_maxsessions") < 0) {
508                 CtdlSetConfigInt("c_maxsessions", 0);
509         }
510
511         // We need a system default message expiry policy, because this is
512         // the top level and there's no 'higher' policy to fall back on.
513         // By default, do not expire messages at all.
514         if (CtdlGetConfigInt("c_ep_mode") == 0) {
515                 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
516                 CtdlSetConfigInt("c_ep_value", 0);
517         }
518
519         // If this is the first run on an empty database, create a default administrator
520         if (oldver == 0) {
521                 create_default_admin_account();
522         }
523 }
524
525
526 // Based on the server version number reported by the existing database,
527 // run in-place data format upgrades until everything is up to date.
528 void post_startup_upgrades(void) {
529
530         syslog(LOG_INFO, "Existing database version on disk is %d", oldver);
531
532         if (oldver < REV_LEVEL) {
533                 syslog(LOG_WARNING, "Running post-startup database upgrades.");
534
535                 if ((oldver > 000) && (oldver < 912)) {
536                         move_inet_addrs_from_vcards_to_user_records();
537                 }
538         
539                 if ((oldver > 000) && (oldver < 922)) {
540                         ProcessOldStyleAdjRefCountQueue();
541                 }
542         
543                 if ((oldver > 000) && (oldver < 930)) {
544                         migrate_inbox_filter_msgnums();
545                 }
546         }
547 }
548
549
550 // Initialization function, called from modules_init.c
551 char *ctdl_module_init_upgrade(void) {
552         if (!threading) {
553                 post_startup_upgrades();
554         }
555
556         /* return our module name for the log */
557         return "upgrade";
558 }