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