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