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