20497ded7e97eaa86ef0a91b88cb1334d1407834
[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-2010 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 "user_ops.h"
56 #include "msgbase.h"
57 #include "serv_upgrade.h"
58 #include "euidindex.h"
59
60
61 #include "ctdl_module.h"
62
63
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                 else
85                 { /* temporarily set this user to -1 */
86                         usbuf.usernum = -1;
87                         CtdlPutUser(&usbuf);
88                 }
89         }
90
91         /** Make sure user SYS_* is user 0 */
92         while (CtdlGetUserByNumber(&usbuf, -1) == 0)
93         {
94                 if (strncmp(usbuf.fullname, "SYS_", 4))
95                 {       /** Delete any user 0 that doesn't start with SYS_ */
96                         makeuserkey(usernamekey, 
97                                     usbuf.fullname, 
98                                     cutuserkey(usbuf.fullname));
99                         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
100                 }
101                 else
102                 {
103                         usbuf.usernum = 0;
104                         CtdlPutUser(&usbuf);
105                 }
106         }
107 }
108
109
110 /* 
111  * Back end processing function for cmd_bmbx
112  */
113 void cmd_bmbx_backend(struct ctdlroom *qrbuf, void *data) {
114         static struct RoomProcList *rplist = NULL;
115         struct RoomProcList *ptr;
116         struct ctdlroom qr;
117
118         /* Lazy programming here.  Call this function as a CtdlForEachRoom backend
119          * in order to queue up the room names, or call it with a null room
120          * to make it do the processing.
121          */
122         if (qrbuf != NULL) {
123                 ptr = (struct RoomProcList *)
124                         malloc(sizeof (struct RoomProcList));
125                 if (ptr == NULL) return;
126
127                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
128                 ptr->next = rplist;
129                 rplist = ptr;
130                 return;
131         }
132
133         while (rplist != NULL) {
134
135                 if (CtdlGetRoomLock(&qr, rplist->name) == 0) {
136                         CtdlLogPrintf(CTDL_DEBUG, "Processing <%s>...\n", rplist->name);
137                         if ( (qr.QRflags & QR_MAILBOX) == 0) {
138                                 CtdlLogPrintf(CTDL_DEBUG, "  -- not a mailbox\n");
139                         }
140                         else {
141
142                                 qr.QRgen = time(NULL);
143                                 CtdlLogPrintf(CTDL_DEBUG, "  -- fixed!\n");
144                         }
145                         CtdlPutRoomLock(&qr);
146                 }
147
148                 ptr = rplist;
149                 rplist = rplist->next;
150                 free(ptr);
151         }
152 }
153
154 /*
155  * quick fix to bump mailbox generation numbers
156  */
157 void bump_mailbox_generation_numbers(void) {
158         CtdlLogPrintf(CTDL_WARNING, "Applying security fix to mailbox rooms\n");
159         CtdlForEachRoom(cmd_bmbx_backend, NULL);
160         cmd_bmbx_backend(NULL, NULL);
161         return;
162 }
163
164
165 /* 
166  * Back end processing function for convert_ctdluid_to_minusone()
167  */
168 void cbtm_backend(struct ctdluser *usbuf, void *data) {
169         static struct UserProcList *uplist = NULL;
170         struct UserProcList *ptr;
171         struct ctdluser us;
172
173         /* Lazy programming here.  Call this function as a ForEachUser backend
174          * in order to queue up the room names, or call it with a null user
175          * to make it do the processing.
176          */
177         if (usbuf != NULL) {
178                 ptr = (struct UserProcList *)
179                         malloc(sizeof (struct UserProcList));
180                 if (ptr == NULL) return;
181
182                 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
183                 ptr->next = uplist;
184                 uplist = ptr;
185                 return;
186         }
187
188         while (uplist != NULL) {
189
190                 if (CtdlGetUserLock(&us, uplist->user) == 0) {
191                         CtdlLogPrintf(CTDL_DEBUG, "Processing <%s>...\n", uplist->user);
192                         if (us.uid == CTDLUID) {
193                                 us.uid = (-1);
194                         }
195                         CtdlPutUserLock(&us);
196                 }
197
198                 ptr = uplist;
199                 uplist = uplist->next;
200                 free(ptr);
201         }
202 }
203
204 /*
205  * quick fix to change all CTDLUID users to (-1)
206  */
207 void convert_ctdluid_to_minusone(void) {
208         CtdlLogPrintf(CTDL_WARNING, "Applying uid changes\n");
209         ForEachUser(cbtm_backend, NULL);
210         cbtm_backend(NULL, NULL);
211         return;
212 }
213
214
215 /*
216  * Attempt to guess the name of the time zone currently in use
217  * on the underlying host system.
218  */
219 void guess_time_zone(void) {
220         FILE *fp;
221         char buf[PATH_MAX];
222
223         fp = popen(file_guesstimezone, "r");
224         if (fp) {
225                 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
226                         buf[strlen(buf)-1] = 0;
227                         safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
228                         CtdlLogPrintf(CTDL_INFO, "Configuring timezone: %s\n", config.c_default_cal_zone);
229                 }
230                 fclose(fp);
231         }
232 }
233
234
235 /*
236  * Do various things to our configuration file
237  */
238 void update_config(void) {
239         get_config();
240
241         if (CitControl.version < 606) {
242                 config.c_rfc822_strict_from = 0;
243         }
244
245         if (CitControl.version < 609) {
246                 config.c_purge_hour = 3;
247         }
248
249         if (CitControl.version < 615) {
250                 config.c_ldap_port = 389;
251         }
252
253         if (CitControl.version < 623) {
254                 strcpy(config.c_ip_addr, "*");
255         }
256
257         if (CitControl.version < 650) {
258                 config.c_enable_fulltext = 0;
259         }
260
261         if (CitControl.version < 652) {
262                 config.c_auto_cull = 1;
263         }
264
265         if (CitControl.version < 725) {
266                 config.c_xmpp_c2s_port = 5222;
267                 config.c_xmpp_s2s_port = 5269;
268         }
269
270         if (IsEmptyStr(config.c_default_cal_zone)) {
271                 guess_time_zone();
272         }
273
274         put_config();
275 }
276
277
278
279
280 void check_server_upgrades(void) {
281
282         get_control();
283         CtdlLogPrintf(CTDL_INFO, "Server-hosted upgrade level is %d.%02d\n",
284                 (CitControl.version / 100),
285                 (CitControl.version % 100) );
286
287         if (CitControl.version < REV_LEVEL) {
288                 CtdlLogPrintf(CTDL_WARNING,
289                         "Server hosted updates need to be processed at "
290                         "this time.  Please wait...\n");
291         }
292         else {
293                 return;
294         }
295
296         update_config();
297
298         if ((CitControl.version > 000) && (CitControl.version < 555)) {
299                 CtdlLogPrintf(CTDL_EMERG,
300                         "Your data files are from a version of Citadel\n"
301                         "that is too old to be upgraded.  Sorry.\n");
302                 exit(EXIT_FAILURE);
303         }
304         if ((CitControl.version > 000) && (CitControl.version < 591)) {
305                 bump_mailbox_generation_numbers();
306         }
307         if ((CitControl.version > 000) && (CitControl.version < 608)) {
308                 convert_ctdluid_to_minusone();
309         }
310         if ((CitControl.version > 000) && (CitControl.version < 659)) {
311                 rebuild_euid_index();
312         }
313         if (CitControl.version < 735) {
314                 fix_sys_user_name();
315         }
316         if (CitControl.version < 736) {
317                 rebuild_usersbynumber();
318         }
319         CitControl.version = REV_LEVEL;
320         put_control();
321 }
322
323
324 CTDL_MODULE_UPGRADE(upgrade)
325 {
326         check_server_upgrades();
327         
328         /* return our Subversion id for the Log */
329         return "$Id$";
330 }