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