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