d89bf3fd71d57bcc2b13de9111f39cdbaf1408f1
[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-2016 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
56
57 /*
58  * Fix up the name for Citadel user 0 and try to remove any extra users with number 0
59  */
60 void fix_sys_user_name(void)
61 {
62         struct ctdluser usbuf;
63         char usernamekey[USERNAME_SIZE];
64
65         /** If we have a user called Citadel rename them to SYS_Citadel */
66         if (CtdlGetUser(&usbuf, "Citadel") == 0)
67         {
68                 rename_user("Citadel", "SYS_Citadel");
69         }
70
71         while (CtdlGetUserByNumber(&usbuf, 0) == 0)
72         {
73                 /* delete user with number 0 and no name */
74                 if (IsEmptyStr(usbuf.fullname)) {
75                         cdb_delete(CDB_USERS, "", 0);
76                 }
77                 else {
78                         /* temporarily set this user to -1 */
79                         usbuf.usernum = -1;
80                         CtdlPutUser(&usbuf);
81                 }
82         }
83
84         /* Make sure user SYS_* is user 0 */
85         while (CtdlGetUserByNumber(&usbuf, -1) == 0)
86         {
87                 if (strncmp(usbuf.fullname, "SYS_", 4))
88                 {       /* Delete any user 0 that doesn't start with SYS_ */
89                         makeuserkey(usernamekey, usbuf.fullname, cutuserkey(usbuf.fullname));
90                         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
91                 }
92                 else {
93                         usbuf.usernum = 0;
94                         CtdlPutUser(&usbuf);
95                 }
96         }
97 }
98
99
100 /* 
101  * Back end processing function for convert_ctdluid_to_minusone()
102  */
103 void cbtm_backend(struct ctdluser *usbuf, void *data) {
104         static struct UserProcList *uplist = NULL;
105         struct UserProcList *ptr;
106         struct ctdluser us;
107
108         /* Lazy programming here.  Call this function as a ForEachUser backend
109          * in order to queue up the room names, or call it with a null user
110          * to make it do the processing.
111          */
112         if (usbuf != NULL) {
113                 ptr = (struct UserProcList *)
114                         malloc(sizeof (struct UserProcList));
115                 if (ptr == NULL) return;
116
117                 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
118                 ptr->next = uplist;
119                 uplist = ptr;
120                 return;
121         }
122
123         while (uplist != NULL) {
124
125                 if (CtdlGetUserLock(&us, uplist->user) == 0) {
126                         syslog(LOG_DEBUG, "Processing <%s>...", uplist->user);
127                         if (us.uid == CTDLUID) {
128                                 us.uid = (-1);
129                         }
130                         CtdlPutUserLock(&us);
131                 }
132
133                 ptr = uplist;
134                 uplist = uplist->next;
135                 free(ptr);
136         }
137 }
138
139 /*
140  * quick fix to change all CTDLUID users to (-1)
141  */
142 void convert_ctdluid_to_minusone(void) {
143         syslog(LOG_WARNING, "Applying uid changes");
144         ForEachUser(cbtm_backend, NULL);
145         cbtm_backend(NULL, NULL);
146         return;
147 }
148
149
150
151 /*
152  * These accounts may have been created by code that ran between mid 2008 and early 2011.
153  * If present they are no longer in use and may be deleted.
154  */
155 void remove_thread_users(void) {
156         char *deleteusers[] = {
157                 "SYS_checkpoint",
158                 "SYS_extnotify",
159                 "SYS_IGnet Queue",
160                 "SYS_indexer",
161                 "SYS_network",
162                 "SYS_popclient",
163                 "SYS_purger",
164                 "SYS_rssclient",
165                 "SYS_select_on_master",
166                 "SYS_SMTP Send"
167         };
168
169         int i;
170         struct ctdluser usbuf;
171         for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
172                 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
173                         usbuf.axlevel = 0;
174                         strcpy(usbuf.password, "deleteme");
175                         CtdlPutUser(&usbuf);
176                         syslog(LOG_INFO,
177                                 "System user account <%s> is no longer in use and will be deleted.",
178                                 deleteusers[i]
179                         );
180                 }
181         }
182 }
183
184
185 /*
186  * Attempt to guess the name of the time zone currently in use
187  * on the underlying host system.
188  */
189 void guess_time_zone(void) {
190         FILE *fp;
191         char buf[PATH_MAX];
192
193         fp = popen(file_guesstimezone, "r");
194         if (fp) {
195                 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
196                         buf[strlen(buf)-1] = 0;
197                         CtdlSetConfigStr("c_default_cal_zone", buf);
198                         syslog(LOG_INFO, "Configuring timezone: %s", buf);
199                 }
200                 fclose(fp);
201         }
202 }
203
204
205
206 /*
207  * Per-room callback function for ingest_old_roominfo_and_roompic_files()
208  *
209  * This is the second pass, where we process the list of rooms with info or pic files.
210  */
211 void iorarf_oneroom(char *roomname, char *infofile, char *picfile)
212 {
213         FILE *fp;
214         long data_length;
215         char *unencoded_data;
216         char *encoded_data;
217         long info_msgnum = 0;
218         long pic_msgnum = 0;
219         char subject[SIZ];
220
221         syslog(LOG_DEBUG, "iorarf_oneroom( %s , %s , %s )", roomname, infofile, picfile);
222
223         // Test for the presence of a legacy "room info file"
224         if (!IsEmptyStr(infofile)) {
225                 fp = fopen(infofile, "r");
226         }
227         else {
228                 fp = NULL;
229         }
230         if (fp) {
231                 fseek(fp, 0, SEEK_END);
232                 data_length = ftell(fp);
233
234                 if (data_length >= 1) {
235                         rewind(fp);
236                         unencoded_data = malloc(data_length);
237                         if (unencoded_data) {
238                                 fread(unencoded_data, data_length, 1, fp);
239                                 encoded_data = malloc((data_length * 2) + 100);
240                                 if (encoded_data) {
241                                         sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n");
242                                         CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
243                                         snprintf(subject, sizeof subject, "Imported room banner for %s", roomname);
244                                         info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
245                                         free(encoded_data);
246                                 }
247                                 free(unencoded_data);
248                         }
249                 }
250                 fclose(fp);
251                 if (info_msgnum > 0) unlink(infofile);
252         }
253
254         // Test for the presence of a legacy "room picture file" and import it.
255         if (!IsEmptyStr(picfile)) {
256                 fp = fopen(picfile, "r");
257         }
258         else {
259                 fp = NULL;
260         }
261         if (fp) {
262                 fseek(fp, 0, SEEK_END);
263                 data_length = ftell(fp);
264
265                 if (data_length >= 1) {
266                         rewind(fp);
267                         unencoded_data = malloc(data_length);
268                         if (unencoded_data) {
269                                 fread(unencoded_data, data_length, 1, fp);
270                                 encoded_data = malloc((data_length * 2) + 100);
271                                 if (encoded_data) {
272                                         sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
273                                         CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
274                                         snprintf(subject, sizeof subject, "Imported room icon for %s", roomname);
275                                         pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject);
276                                         free(encoded_data);
277                                 }
278                                 free(unencoded_data);
279                         }
280                 }
281                 fclose(fp);
282                 if (pic_msgnum > 0) unlink(picfile);
283         }
284
285         // Now we have the message numbers of our new banner and icon.  Record them in the room record.
286         // NOTE: we are not deleting the old msgnum_info because that position in the record was previously
287         // a pointer to the highest message number which existed in the room when the info file was saved,
288         // and we don't want to delete messages that are not *actually* old banners.
289         struct ctdlroom qrbuf;
290         if (CtdlGetRoomLock(&qrbuf, roomname) == 0) {
291                 qrbuf.msgnum_info = info_msgnum;
292                 qrbuf.msgnum_pic = pic_msgnum;
293                 CtdlPutRoomLock(&qrbuf);
294         }
295
296 }
297
298
299
300 struct iorarf_list {
301         struct iorarf_list *next;
302         char name[ROOMNAMELEN];
303         char info[PATH_MAX];
304         char pic[PATH_MAX];
305 };
306
307
308 /*
309  * Per-room callback function for ingest_old_roominfo_and_roompic_files()
310  *
311  * This is the first pass, where the list of qualifying rooms is gathered.
312  */
313 void iorarf_backend(struct ctdlroom *qrbuf, void *data)
314 {
315         FILE *fp;
316         struct iorarf_list **iorarf_list = (struct iorarf_list **)data;
317
318         struct iorarf_list *i = malloc(sizeof(struct iorarf_list));
319         i->next = *iorarf_list;
320         strcpy(i->name, qrbuf->QRname);
321         strcpy(i->info, "");
322         strcpy(i->pic, "");
323
324         // Test for the presence of a legacy "room info file"
325         assoc_file_name(i->info, sizeof i->info, qrbuf, ctdl_info_dir);
326         fp = fopen(i->info, "r");
327         if (fp) {
328                 fclose(fp);
329         }
330         else {
331                 i->info[0] = 0;
332         }
333
334         // Test for the presence of a legacy "room picture file"
335         assoc_file_name(i->pic, sizeof i->pic, qrbuf, ctdl_image_dir);
336         fp = fopen(i->pic, "r");
337         if (fp) {
338                 fclose(fp);
339         }
340         else {
341                 i->pic[0] = 0;
342         }
343
344         if ( (!IsEmptyStr(i->info)) || (!IsEmptyStr(i->pic)) ) {
345                 *iorarf_list = i;
346         }
347         else {
348                 free(i);
349         }
350 }
351
352
353 /*
354  * Prior to Citadel Server version 902, room info and pictures (which comprise the
355  * displayed banner for each room) were stored in the filesystem.  If we are upgrading
356  * from version >000 to version >=902, ingest those files into the database.
357  */
358 void ingest_old_roominfo_and_roompic_files(void)
359 {
360         struct iorarf_list *il = NULL;
361
362         CtdlForEachRoom(iorarf_backend, &il);
363
364         struct iorarf_list *p;
365         while (il) {
366                 iorarf_oneroom(il->name, il->info, il->pic);
367                 p = il->next;
368                 free(il);
369                 il = p;
370         }
371
372 }
373
374
375 /*
376  * Perform any upgrades that can be done automatically based on our knowledge of the previous
377  * version of Citadel server that was running here.
378  *
379  * Note that if the previous version was 0 then this is a new installation running for the first time.
380  */
381 void update_config(void) {
382
383         int oldver = CtdlGetConfigInt("MM_hosted_upgrade_level");
384
385         if (oldver < 606) {
386                 CtdlSetConfigInt("c_rfc822_strict_from", 0);
387         }
388
389         if (oldver < 609) {
390                 CtdlSetConfigInt("c_purge_hour", 3);
391         }
392
393         if (oldver < 615) {
394                 CtdlSetConfigInt("c_ldap_port", 389);
395         }
396
397         if (oldver < 623) {
398                 CtdlSetConfigStr("c_ip_addr", "*");
399         }
400
401         if (oldver < 650) {
402                 CtdlSetConfigInt("c_enable_fulltext", 1);
403         }
404
405         if (oldver < 652) {
406                 CtdlSetConfigInt("c_auto_cull", 1);
407         }
408
409         if (oldver < 725) {
410                 CtdlSetConfigInt("c_xmpp_c2s_port", 5222);
411                 CtdlSetConfigInt("c_xmpp_s2s_port", 5269);
412         }
413
414         if (oldver < 830) {
415                 CtdlSetConfigInt("c_nntp_port", 119);
416                 CtdlSetConfigInt("c_nntps_port", 563);
417         }
418
419         if (IsEmptyStr(CtdlGetConfigStr("c_default_cal_zone"))) {
420                 guess_time_zone();
421         }
422 }
423
424
425
426 /*
427  * Based on the server version number reported by the existing database,
428  * run in-place data format upgrades until everything is up to date.
429  */
430 void check_server_upgrades(void) {
431
432         syslog(LOG_INFO, "Existing database version on disk is %d", CtdlGetConfigInt("MM_hosted_upgrade_level"));
433
434         if (CtdlGetConfigInt("MM_hosted_upgrade_level") < REV_LEVEL) {
435                 syslog(LOG_WARNING, "Server hosted updates need to be processed at this time.  Please wait...");
436         }
437         else {
438                 return;
439         }
440
441         update_config();
442
443         if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 591)) {
444                 syslog(LOG_EMERG, "This database is too old to be upgraded.  Citadel server will exit.");
445                 exit(EXIT_FAILURE);
446         }
447         if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 608)) {
448                 convert_ctdluid_to_minusone();
449         }
450         if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 659)) {
451                 rebuild_euid_index();
452         }
453         if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 735) {
454                 fix_sys_user_name();
455         }
456         if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 736) {
457                 rebuild_usersbynumber();
458         }
459         if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 790) {
460                 remove_thread_users();
461         }
462         if (CtdlGetConfigInt("MM_hosted_upgrade_level") < 810) {
463                 struct ctdlroom QRoom;
464                 if (!CtdlGetRoom(&QRoom, SMTP_SPOOLOUT_ROOM)) {
465                         QRoom.QRdefaultview = VIEW_QUEUE;
466                         CtdlPutRoom(&QRoom);
467                 }
468                 if (!CtdlGetRoom(&QRoom, FNBL_QUEUE_ROOM)) {
469                         QRoom.QRdefaultview = VIEW_QUEUE;
470                         CtdlPutRoom(&QRoom);
471                 }
472         }
473
474         if ((CtdlGetConfigInt("MM_hosted_upgrade_level") > 000) && (CtdlGetConfigInt("MM_hosted_upgrade_level") < 902)) {
475                 ingest_old_roominfo_and_roompic_files();
476         }
477
478         CtdlSetConfigInt("MM_hosted_upgrade_level", REV_LEVEL);
479
480         /*
481          * Negative values for maxsessions are not allowed.
482          */
483         if (CtdlGetConfigInt("c_maxsessions") < 0) {
484                 CtdlSetConfigInt("c_maxsessions", 0);
485         }
486
487         /* We need a system default message expiry policy, because this is
488          * the top level and there's no 'higher' policy to fall back on.
489          * By default, do not expire messages at all.
490          */
491         if (CtdlGetConfigInt("c_ep_mode") == 0) {
492                 CtdlSetConfigInt("c_ep_mode", EXPIRE_MANUAL);
493                 CtdlSetConfigInt("c_ep_value", 0);
494         }
495 }
496
497
498 CTDL_MODULE_UPGRADE(upgrade)
499 {
500         check_server_upgrades();
501         
502         /* return our module id for the Log */
503         return "upgrade";
504 }