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