* More license declarations
[citadel.git] / citadel / modules / upgrade / serv_upgrade.c
1 /*
2  * $Id$
3  *
4  * Transparently handle the upgrading of server data formats.
5  *
6  * Copyright (c) 1987-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #include <sys/wait.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <libcitadel.h>
48 #include "citadel.h"
49 #include "server.h"
50 #include "citserver.h"
51 #include "support.h"
52 #include "config.h"
53 #include "control.h"
54 #include "database.h"
55 #include "room_ops.h"
56 #include "user_ops.h"
57 #include "msgbase.h"
58 #include "serv_upgrade.h"
59 #include "euidindex.h"
60
61
62 #include "ctdl_module.h"
63
64
65
66 /*
67  * Fix up the name for Citadel user 0 and try to remove any extra users with number 0
68  */
69 void fix_sys_user_name(void)
70 {
71         struct ctdluser usbuf;
72         char usernamekey[USERNAME_SIZE];
73
74         /** If we have a user called Citadel rename them to SYS_Citadel */
75         if (getuser(&usbuf, "Citadel") == 0)
76         {
77                 rename_user("Citadel", "SYS_Citadel");
78         }
79
80         while (getuserbynumber(&usbuf, 0) == 0)
81         {
82                 /* delete user with number 0 and no name */
83                 if (IsEmptyStr(usbuf.fullname))
84                         cdb_delete(CDB_USERS, "", 0);
85                 else
86                 { /* temporarily set this user to -1 */
87                         usbuf.usernum = -1;
88                         putuser(&usbuf);
89                 }
90         }
91
92         /** Make sure user SYS_* is user 0 */
93         while (getuserbynumber(&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);
98                         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
99                 }
100                 else
101                 {
102                         usbuf.usernum = 0;
103                         putuser(&usbuf);
104                 }
105         }
106 }
107
108
109 /* 
110  * Back end processing function for cmd_bmbx
111  */
112 void cmd_bmbx_backend(struct ctdlroom *qrbuf, void *data) {
113         static struct RoomProcList *rplist = NULL;
114         struct RoomProcList *ptr;
115         struct ctdlroom qr;
116
117         /* Lazy programming here.  Call this function as a ForEachRoom backend
118          * in order to queue up the room names, or call it with a null room
119          * to make it do the processing.
120          */
121         if (qrbuf != NULL) {
122                 ptr = (struct RoomProcList *)
123                         malloc(sizeof (struct RoomProcList));
124                 if (ptr == NULL) return;
125
126                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
127                 ptr->next = rplist;
128                 rplist = ptr;
129                 return;
130         }
131
132         while (rplist != NULL) {
133
134                 if (lgetroom(&qr, rplist->name) == 0) {
135                         CtdlLogPrintf(CTDL_DEBUG, "Processing <%s>...\n", rplist->name);
136                         if ( (qr.QRflags & QR_MAILBOX) == 0) {
137                                 CtdlLogPrintf(CTDL_DEBUG, "  -- not a mailbox\n");
138                         }
139                         else {
140
141                                 qr.QRgen = time(NULL);
142                                 CtdlLogPrintf(CTDL_DEBUG, "  -- fixed!\n");
143                         }
144                         lputroom(&qr);
145                 }
146
147                 ptr = rplist;
148                 rplist = rplist->next;
149                 free(ptr);
150         }
151 }
152
153 /*
154  * quick fix to bump mailbox generation numbers
155  */
156 void bump_mailbox_generation_numbers(void) {
157         CtdlLogPrintf(CTDL_WARNING, "Applying security fix to mailbox rooms\n");
158         ForEachRoom(cmd_bmbx_backend, NULL);
159         cmd_bmbx_backend(NULL, NULL);
160         return;
161 }
162
163
164 /* 
165  * Back end processing function for convert_ctdluid_to_minusone()
166  */
167 void cbtm_backend(struct ctdluser *usbuf, void *data) {
168         static struct UserProcList *uplist = NULL;
169         struct UserProcList *ptr;
170         struct ctdluser us;
171
172         /* Lazy programming here.  Call this function as a ForEachUser backend
173          * in order to queue up the room names, or call it with a null user
174          * to make it do the processing.
175          */
176         if (usbuf != NULL) {
177                 ptr = (struct UserProcList *)
178                         malloc(sizeof (struct UserProcList));
179                 if (ptr == NULL) return;
180
181                 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
182                 ptr->next = uplist;
183                 uplist = ptr;
184                 return;
185         }
186
187         while (uplist != NULL) {
188
189                 if (lgetuser(&us, uplist->user) == 0) {
190                         CtdlLogPrintf(CTDL_DEBUG, "Processing <%s>...\n", uplist->user);
191                         if (us.uid == CTDLUID) {
192                                 us.uid = (-1);
193                         }
194                         lputuser(&us);
195                 }
196
197                 ptr = uplist;
198                 uplist = uplist->next;
199                 free(ptr);
200         }
201 }
202
203 /*
204  * quick fix to change all CTDLUID users to (-1)
205  */
206 void convert_ctdluid_to_minusone(void) {
207         CtdlLogPrintf(CTDL_WARNING, "Applying uid changes\n");
208         ForEachUser(cbtm_backend, NULL);
209         cbtm_backend(NULL, NULL);
210         return;
211 }
212
213
214 /*
215  * Attempt to guess the name of the time zone currently in use
216  * on the underlying host system.
217  */
218 void guess_time_zone(void) {
219         FILE *fp;
220         char buf[PATH_MAX];
221
222         fp = popen(file_guesstimezone, "r");
223         if (fp) {
224                 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
225                         buf[strlen(buf)-1] = 0;
226                         safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
227                         CtdlLogPrintf(CTDL_INFO, "Configuring timezone: %s\n", config.c_default_cal_zone);
228                 }
229                 fclose(fp);
230         }
231 }
232
233
234 /*
235  * Do various things to our configuration file
236  */
237 void update_config(void) {
238         get_config();
239
240         if (CitControl.version < 606) {
241                 config.c_rfc822_strict_from = 0;
242         }
243
244         if (CitControl.version < 609) {
245                 config.c_purge_hour = 3;
246         }
247
248         if (CitControl.version < 615) {
249                 config.c_ldap_port = 389;
250         }
251
252         if (CitControl.version < 623) {
253                 strcpy(config.c_ip_addr, "0.0.0.0");
254         }
255
256         if (CitControl.version < 650) {
257                 config.c_enable_fulltext = 0;
258         }
259
260         if (CitControl.version < 652) {
261                 config.c_auto_cull = 1;
262         }
263
264         if (CitControl.version < 725) {
265                 config.c_xmpp_c2s_port = 5222;
266                 config.c_xmpp_s2s_port = 5269;
267         }
268
269         /* Erase LDAP config that might be left over from the old connector
270          * which served a different purpose.
271          */
272         if (CitControl.version < 760) {
273                 strcpy(config.c_ldap_host, "");
274                 config.c_ldap_port = 389;
275                 strcpy(config.c_ldap_base_dn, "");
276                 strcpy(config.c_ldap_bind_dn, "");
277                 strcpy(config.c_ldap_bind_pw, "");
278         }
279
280         if (IsEmptyStr(config.c_default_cal_zone)) {
281                 guess_time_zone();
282         }
283
284         put_config();
285 }
286
287
288
289
290 void check_server_upgrades(void) {
291
292         get_control();
293         CtdlLogPrintf(CTDL_INFO, "Server-hosted upgrade level is %d.%02d\n",
294                 (CitControl.version / 100),
295                 (CitControl.version % 100) );
296
297         if (CitControl.version < REV_LEVEL) {
298                 CtdlLogPrintf(CTDL_WARNING,
299                         "Server hosted updates need to be processed at "
300                         "this time.  Please wait...\n");
301         }
302         else {
303                 return;
304         }
305
306         update_config();
307
308         if ((CitControl.version > 000) && (CitControl.version < 555)) {
309                 CtdlLogPrintf(CTDL_EMERG,
310                         "Your data files are from a version of Citadel\n"
311                         "that is too old to be upgraded.  Sorry.\n");
312                 exit(EXIT_FAILURE);
313         }
314         if ((CitControl.version > 000) && (CitControl.version < 591)) {
315                 bump_mailbox_generation_numbers();
316         }
317         if ((CitControl.version > 000) && (CitControl.version < 608)) {
318                 convert_ctdluid_to_minusone();
319         }
320         if ((CitControl.version > 000) && (CitControl.version < 659)) {
321                 rebuild_euid_index();
322         }
323         if (CitControl.version < 735) {
324                 fix_sys_user_name();
325         }
326         if (CitControl.version < 736) {
327                 rebuild_usersbynumber();
328         }
329         CitControl.version = REV_LEVEL;
330         put_control();
331 }
332
333
334 CTDL_MODULE_UPGRADE(upgrade)
335 {
336         check_server_upgrades();
337         
338         /* return our Subversion id for the Log */
339         return "$Id$";
340 }