2 * Transparently handle the upgrading of server data formats.
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
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.
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
29 #include <sys/types.h>
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
45 #include <libcitadel.h>
48 #include "citserver.h"
55 #include "serv_upgrade.h"
56 #include "euidindex.h"
59 #include "ctdl_module.h"
64 * Fix up the name for Citadel user 0 and try to remove any extra users with number 0
66 void fix_sys_user_name(void)
68 struct ctdluser usbuf;
69 char usernamekey[USERNAME_SIZE];
71 /** If we have a user called Citadel rename them to SYS_Citadel */
72 if (CtdlGetUser(&usbuf, "Citadel") == 0)
74 rename_user("Citadel", "SYS_Citadel");
77 while (CtdlGetUserByNumber(&usbuf, 0) == 0)
79 /* delete user with number 0 and no name */
80 if (IsEmptyStr(usbuf.fullname)) {
81 cdb_delete(CDB_USERS, "", 0);
84 /* temporarily set this user to -1 */
90 /* Make sure user SYS_* is user 0 */
91 while (CtdlGetUserByNumber(&usbuf, -1) == 0)
93 if (strncmp(usbuf.fullname, "SYS_", 4))
94 { /* Delete any user 0 that doesn't start with SYS_ */
95 makeuserkey(usernamekey, usbuf.fullname, cutuserkey(usbuf.fullname));
96 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
107 * Back end processing function for cmd_bmbx
109 void cmd_bmbx_backend(struct ctdlroom *qrbuf, void *data) {
110 static struct RoomProcList *rplist = NULL;
111 struct RoomProcList *ptr;
114 /* Lazy programming here. Call this function as a CtdlForEachRoom backend
115 * in order to queue up the room names, or call it with a null room
116 * to make it do the processing.
119 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
120 if (ptr == NULL) return;
122 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
128 while (rplist != NULL) {
130 if (CtdlGetRoomLock(&qr, rplist->name) == 0) {
131 syslog(LOG_DEBUG, "Processing <%s>...", rplist->name);
132 if ( (qr.QRflags & QR_MAILBOX) == 0) {
133 syslog(LOG_DEBUG, " -- not a mailbox");
137 qr.QRgen = time(NULL);
138 syslog(LOG_DEBUG, " -- fixed!");
140 CtdlPutRoomLock(&qr);
144 rplist = rplist->next;
150 * quick fix to bump mailbox generation numbers
152 void bump_mailbox_generation_numbers(void) {
153 syslog(LOG_WARNING, "Applying security fix to mailbox rooms");
154 CtdlForEachRoom(cmd_bmbx_backend, NULL);
155 cmd_bmbx_backend(NULL, NULL);
161 * Back end processing function for convert_ctdluid_to_minusone()
163 void cbtm_backend(struct ctdluser *usbuf, void *data) {
164 static struct UserProcList *uplist = NULL;
165 struct UserProcList *ptr;
168 /* Lazy programming here. Call this function as a ForEachUser backend
169 * in order to queue up the room names, or call it with a null user
170 * to make it do the processing.
173 ptr = (struct UserProcList *)
174 malloc(sizeof (struct UserProcList));
175 if (ptr == NULL) return;
177 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
183 while (uplist != NULL) {
185 if (CtdlGetUserLock(&us, uplist->user) == 0) {
186 syslog(LOG_DEBUG, "Processing <%s>...", uplist->user);
187 if (us.uid == CTDLUID) {
190 CtdlPutUserLock(&us);
194 uplist = uplist->next;
200 * quick fix to change all CTDLUID users to (-1)
202 void convert_ctdluid_to_minusone(void) {
203 syslog(LOG_WARNING, "Applying uid changes");
204 ForEachUser(cbtm_backend, NULL);
205 cbtm_backend(NULL, NULL);
212 * These accounts may have been created by code that ran between mid 2008 and early 2011.
213 * If present they are no longer in use and may be deleted.
215 void remove_thread_users(void) {
216 char *deleteusers[] = {
225 "SYS_select_on_master",
230 struct ctdluser usbuf;
231 for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
232 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
234 strcpy(usbuf.password, "deleteme");
237 "System user account <%s> is no longer in use and will be deleted.",
246 * Attempt to guess the name of the time zone currently in use
247 * on the underlying host system.
249 void guess_time_zone(void) {
253 fp = popen(file_guesstimezone, "r");
255 if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
256 buf[strlen(buf)-1] = 0;
257 safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
258 syslog(LOG_INFO, "Configuring timezone: %s", config.c_default_cal_zone);
266 * Do various things to our configuration file
268 void update_config(void) {
271 if (CitControl.version < 606) {
272 config.c_rfc822_strict_from = 0;
275 if (CitControl.version < 609) {
276 config.c_purge_hour = 3;
279 if (CitControl.version < 615) {
280 config.c_ldap_port = 389;
283 if (CitControl.version < 623) {
284 strcpy(config.c_ip_addr, "*");
287 if (CitControl.version < 650) {
288 config.c_enable_fulltext = 0;
291 if (CitControl.version < 652) {
292 config.c_auto_cull = 1;
295 if (CitControl.version < 725) {
296 config.c_xmpp_c2s_port = 5222;
297 config.c_xmpp_s2s_port = 5269;
300 if (IsEmptyStr(config.c_default_cal_zone)) {
310 * Based on the server version number reported by the existing database,
311 * run in-place data format upgrades until everything is up to date.
313 void check_server_upgrades(void) {
316 syslog(LOG_INFO, "Existing database version on disk is %d.%02d",
317 (CitControl.version / 100),
318 (CitControl.version % 100)
321 if (CitControl.version < REV_LEVEL) {
323 "Server hosted updates need to be processed at this time. Please wait..."
332 if ((CitControl.version > 000) && (CitControl.version < 555)) {
333 syslog(LOG_EMERG, "This database is too old to be upgraded. Citadel server will exit.");
336 if ((CitControl.version > 000) && (CitControl.version < 591)) {
337 bump_mailbox_generation_numbers();
339 if ((CitControl.version > 000) && (CitControl.version < 608)) {
340 convert_ctdluid_to_minusone();
342 if ((CitControl.version > 000) && (CitControl.version < 659)) {
343 rebuild_euid_index();
345 if (CitControl.version < 735) {
348 if (CitControl.version < 736) {
349 rebuild_usersbynumber();
351 if (CitControl.version < 790) {
352 remove_thread_users();
354 CitControl.version = REV_LEVEL;
359 CTDL_MODULE_UPGRADE(upgrade)
361 check_server_upgrades();
363 /* return our module id for the Log */