Back to index

citadel  8.12
serv_upgrade.c
Go to the documentation of this file.
00001 /*
00002  * Transparently handle the upgrading of server data formats.
00003  *
00004  * Copyright (c) 1987-2012 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "sysdep.h"
00016 #include <stdlib.h>
00017 #include <unistd.h>
00018 #include <stdio.h>
00019 #include <fcntl.h>
00020 #include <signal.h>
00021 #include <pwd.h>
00022 #include <errno.h>
00023 #include <sys/types.h>
00024 
00025 #if TIME_WITH_SYS_TIME
00026 # include <sys/time.h>
00027 # include <time.h>
00028 #else
00029 # if HAVE_SYS_TIME_H
00030 #  include <sys/time.h>
00031 # else
00032 #  include <time.h>
00033 # endif
00034 #endif
00035 
00036 #include <sys/wait.h>
00037 #include <string.h>
00038 #include <limits.h>
00039 #include <libcitadel.h>
00040 #include "citadel.h"
00041 #include "server.h"
00042 #include "citserver.h"
00043 #include "support.h"
00044 #include "config.h"
00045 #include "control.h"
00046 #include "database.h"
00047 #include "user_ops.h"
00048 #include "msgbase.h"
00049 #include "serv_upgrade.h"
00050 #include "euidindex.h"
00051 #include "ctdl_module.h"
00052 
00053 
00054 /*
00055  * Fix up the name for Citadel user 0 and try to remove any extra users with number 0
00056  */
00057 void fix_sys_user_name(void)
00058 {
00059        struct ctdluser usbuf;
00060        char usernamekey[USERNAME_SIZE];
00061 
00063        if (CtdlGetUser(&usbuf, "Citadel") == 0)
00064        {
00065               rename_user("Citadel", "SYS_Citadel");
00066        }
00067 
00068        while (CtdlGetUserByNumber(&usbuf, 0) == 0)
00069        {
00070               /* delete user with number 0 and no name */
00071               if (IsEmptyStr(usbuf.fullname)) {
00072                      cdb_delete(CDB_USERS, "", 0);
00073               }
00074               else {
00075                      /* temporarily set this user to -1 */
00076                      usbuf.usernum = -1;
00077                      CtdlPutUser(&usbuf);
00078               }
00079        }
00080 
00081        /* Make sure user SYS_* is user 0 */
00082        while (CtdlGetUserByNumber(&usbuf, -1) == 0)
00083        {
00084               if (strncmp(usbuf.fullname, "SYS_", 4))
00085               {      /* Delete any user 0 that doesn't start with SYS_ */
00086                      makeuserkey(usernamekey, usbuf.fullname, cutuserkey(usbuf.fullname));
00087                      cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
00088               }
00089               else {
00090                      usbuf.usernum = 0;
00091                      CtdlPutUser(&usbuf);
00092               }
00093        }
00094 }
00095 
00096 
00097 /* 
00098  * Back end processing function for cmd_bmbx
00099  */
00100 void cmd_bmbx_backend(struct ctdlroom *qrbuf, void *data) {
00101        static struct RoomProcList *rplist = NULL;
00102        struct RoomProcList *ptr;
00103        struct ctdlroom qr;
00104 
00105        /* Lazy programming here.  Call this function as a CtdlForEachRoom backend
00106         * in order to queue up the room names, or call it with a null room
00107         * to make it do the processing.
00108         */
00109        if (qrbuf != NULL) {
00110               ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
00111               if (ptr == NULL) return;
00112 
00113               safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
00114               ptr->next = rplist;
00115               rplist = ptr;
00116               return;
00117        }
00118 
00119        while (rplist != NULL) {
00120 
00121               if (CtdlGetRoomLock(&qr, rplist->name) == 0) {
00122                      syslog(LOG_DEBUG, "Processing <%s>...", rplist->name);
00123                      if ( (qr.QRflags & QR_MAILBOX) == 0) {
00124                             syslog(LOG_DEBUG, "  -- not a mailbox");
00125                      }
00126                      else {
00127 
00128                             qr.QRgen = time(NULL);
00129                             syslog(LOG_DEBUG, "  -- fixed!");
00130                      }
00131                      CtdlPutRoomLock(&qr);
00132               }
00133 
00134               ptr = rplist;
00135               rplist = rplist->next;
00136               free(ptr);
00137        }
00138 }
00139 
00140 /*
00141  * quick fix to bump mailbox generation numbers
00142  */
00143 void bump_mailbox_generation_numbers(void) {
00144        syslog(LOG_WARNING, "Applying security fix to mailbox rooms");
00145        CtdlForEachRoom(cmd_bmbx_backend, NULL);
00146        cmd_bmbx_backend(NULL, NULL);
00147        return;
00148 }
00149 
00150 
00151 /* 
00152  * Back end processing function for convert_ctdluid_to_minusone()
00153  */
00154 void cbtm_backend(struct ctdluser *usbuf, void *data) {
00155        static struct UserProcList *uplist = NULL;
00156        struct UserProcList *ptr;
00157        struct ctdluser us;
00158 
00159        /* Lazy programming here.  Call this function as a ForEachUser backend
00160         * in order to queue up the room names, or call it with a null user
00161         * to make it do the processing.
00162         */
00163        if (usbuf != NULL) {
00164               ptr = (struct UserProcList *)
00165                      malloc(sizeof (struct UserProcList));
00166               if (ptr == NULL) return;
00167 
00168               safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
00169               ptr->next = uplist;
00170               uplist = ptr;
00171               return;
00172        }
00173 
00174        while (uplist != NULL) {
00175 
00176               if (CtdlGetUserLock(&us, uplist->user) == 0) {
00177                      syslog(LOG_DEBUG, "Processing <%s>...", uplist->user);
00178                      if (us.uid == CTDLUID) {
00179                             us.uid = (-1);
00180                      }
00181                      CtdlPutUserLock(&us);
00182               }
00183 
00184               ptr = uplist;
00185               uplist = uplist->next;
00186               free(ptr);
00187        }
00188 }
00189 
00190 /*
00191  * quick fix to change all CTDLUID users to (-1)
00192  */
00193 void convert_ctdluid_to_minusone(void) {
00194        syslog(LOG_WARNING, "Applying uid changes");
00195        ForEachUser(cbtm_backend, NULL);
00196        cbtm_backend(NULL, NULL);
00197        return;
00198 }
00199 
00200 
00201 
00202 /*
00203  * These accounts may have been created by code that ran between mid 2008 and early 2011.
00204  * If present they are no longer in use and may be deleted.
00205  */
00206 void remove_thread_users(void) {
00207        char *deleteusers[] = {
00208               "SYS_checkpoint",
00209               "SYS_extnotify",
00210               "SYS_IGnet Queue",
00211               "SYS_indexer",
00212               "SYS_network",
00213               "SYS_popclient",
00214               "SYS_purger",
00215               "SYS_rssclient",
00216               "SYS_select_on_master",
00217               "SYS_SMTP Send"
00218        };
00219 
00220        int i;
00221        struct ctdluser usbuf;
00222        for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
00223               if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
00224                      usbuf.axlevel = 0;
00225                      strcpy(usbuf.password, "deleteme");
00226                      CtdlPutUser(&usbuf);
00227                      syslog(LOG_INFO,
00228                             "System user account <%s> is no longer in use and will be deleted.",
00229                             deleteusers[i]
00230                      );
00231               }
00232        }
00233 }
00234 
00235 
00236 /*
00237  * Attempt to guess the name of the time zone currently in use
00238  * on the underlying host system.
00239  */
00240 void guess_time_zone(void) {
00241        FILE *fp;
00242        char buf[PATH_MAX];
00243 
00244        fp = popen(file_guesstimezone, "r");
00245        if (fp) {
00246               if (fgets(buf, sizeof buf, fp) && (strlen(buf) > 2)) {
00247                      buf[strlen(buf)-1] = 0;
00248                      safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
00249                      syslog(LOG_INFO, "Configuring timezone: %s", config.c_default_cal_zone);
00250               }
00251               fclose(fp);
00252        }
00253 }
00254 
00255 
00256 /*
00257  * Perform any upgrades that can be done automatically based on our knowledge of the previous
00258  * version of Citadel server that was running here.
00259  *
00260  * Note that if the previous version was 0 then this is a new installation running for the first time.
00261  */
00262 void update_config(void) {
00263        get_config();
00264 
00265        if (CitControl.version < 606) {
00266               config.c_rfc822_strict_from = 0;
00267        }
00268 
00269        if (CitControl.version < 609) {
00270               config.c_purge_hour = 3;
00271        }
00272 
00273        if (CitControl.version < 615) {
00274               config.c_ldap_port = 389;
00275        }
00276 
00277        if (CitControl.version < 623) {
00278               strcpy(config.c_ip_addr, "*");
00279        }
00280 
00281        if (CitControl.version < 650) {
00282               config.c_enable_fulltext = 1;
00283        }
00284 
00285        if (CitControl.version < 652) {
00286               config.c_auto_cull = 1;
00287        }
00288 
00289        if (CitControl.version < 725) {
00290               config.c_xmpp_c2s_port = 5222;
00291               config.c_xmpp_s2s_port = 5269;
00292        }
00293 
00294        if (CitControl.version < 725) {
00295 
00296 
00297        }
00298        if (IsEmptyStr(config.c_default_cal_zone)) {
00299               guess_time_zone();
00300        }
00301 
00302        put_config();
00303 }
00304 
00305 
00306 
00307 /*
00308  * Based on the server version number reported by the existing database,
00309  * run in-place data format upgrades until everything is up to date.
00310  */
00311 void check_server_upgrades(void) {
00312 
00313        get_control();
00314        syslog(LOG_INFO, "Existing database version on disk is %d.%02d",
00315               (CitControl.version / 100),
00316               (CitControl.version % 100)
00317        );
00318 
00319        if (CitControl.version < REV_LEVEL) {
00320               syslog(LOG_WARNING,
00321                      "Server hosted updates need to be processed at this time.  Please wait..."
00322               );
00323        }
00324        else {
00325               return;
00326        }
00327 
00328        update_config();
00329 
00330        if ((CitControl.version > 000) && (CitControl.version < 555)) {
00331               syslog(LOG_EMERG, "This database is too old to be upgraded.  Citadel server will exit.");
00332               exit(EXIT_FAILURE);
00333        }
00334        if ((CitControl.version > 000) && (CitControl.version < 591)) {
00335               bump_mailbox_generation_numbers();
00336        }
00337        if ((CitControl.version > 000) && (CitControl.version < 608)) {
00338               convert_ctdluid_to_minusone();
00339        }
00340        if ((CitControl.version > 000) && (CitControl.version < 659)) {
00341               rebuild_euid_index();
00342        }
00343        if (CitControl.version < 735) {
00344               fix_sys_user_name();
00345        }
00346        if (CitControl.version < 736) {
00347               rebuild_usersbynumber();
00348        }
00349        if (CitControl.version < 790) {
00350               remove_thread_users();
00351        }
00352        if (CitControl.version < 810) {
00353               struct ctdlroom QRoom;
00354               if (!CtdlGetRoom(&QRoom, SMTP_SPOOLOUT_ROOM)) {
00355                      QRoom.QRdefaultview = VIEW_QUEUE;
00356                      CtdlPutRoom(&QRoom);
00357               }
00358               if (!CtdlGetRoom(&QRoom, FNBL_QUEUE_ROOM)) {
00359                      QRoom.QRdefaultview = VIEW_QUEUE;
00360                      CtdlPutRoom(&QRoom);
00361               }
00362        }
00363 
00364        CitControl.version = REV_LEVEL;
00365 
00366        /*
00367         * Negative values for maxsessions are not allowed.
00368         */
00369        if (config.c_maxsessions < 0) {
00370               config.c_maxsessions = 0;
00371        }
00372 
00373        /* We need a system default message expiry policy, because this is
00374         * the top level and there's no 'higher' policy to fall back on.
00375         * By default, do not expire messages at all.
00376         */
00377        if (config.c_ep.expire_mode == 0) {
00378               config.c_ep.expire_mode = EXPIRE_MANUAL;
00379               config.c_ep.expire_value = 0;
00380        }
00381 
00382        put_control();
00383 }
00384 
00385 
00386 CTDL_MODULE_UPGRADE(upgrade)
00387 {
00388        check_server_upgrades();
00389        
00390        /* return our module id for the Log */
00391        return "upgrade";
00392 }