serv_upgrade will delete internal system accounts no longer needed
[citadel.git] / citadel / modules / upgrade / serv_upgrade.c
1 /*
2  * Transparently handle the upgrading of server data formats.
3  *
4  * Copyright (c) 1987-2011 by the citadel.org team
5  *
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.
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                 }
83                 else {
84                         /* temporarily set this user to -1 */
85                         usbuf.usernum = -1;
86                         CtdlPutUser(&usbuf);
87                 }
88         }
89
90         /* Make sure user SYS_* is user 0 */
91         while (CtdlGetUserByNumber(&usbuf, -1) == 0)
92         {
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));
97                 }
98                 else {
99                         usbuf.usernum = 0;
100                         CtdlPutUser(&usbuf);
101                 }
102         }
103 }
104
105
106 /* 
107  * Back end processing function for cmd_bmbx
108  */
109 void cmd_bmbx_backend(struct ctdlroom *qrbuf, void *data) {
110         static struct RoomProcList *rplist = NULL;
111         struct RoomProcList *ptr;
112         struct ctdlroom qr;
113
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.
117          */
118         if (qrbuf != NULL) {
119                 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
120                 if (ptr == NULL) return;
121
122                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
123                 ptr->next = rplist;
124                 rplist = ptr;
125                 return;
126         }
127
128         while (rplist != NULL) {
129
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");
134                         }
135                         else {
136
137                                 qr.QRgen = time(NULL);
138                                 syslog(LOG_DEBUG, "  -- fixed!");
139                         }
140                         CtdlPutRoomLock(&qr);
141                 }
142
143                 ptr = rplist;
144                 rplist = rplist->next;
145                 free(ptr);
146         }
147 }
148
149 /*
150  * quick fix to bump mailbox generation numbers
151  */
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);
156         return;
157 }
158
159
160 /* 
161  * Back end processing function for convert_ctdluid_to_minusone()
162  */
163 void cbtm_backend(struct ctdluser *usbuf, void *data) {
164         static struct UserProcList *uplist = NULL;
165         struct UserProcList *ptr;
166         struct ctdluser us;
167
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.
171          */
172         if (usbuf != NULL) {
173                 ptr = (struct UserProcList *)
174                         malloc(sizeof (struct UserProcList));
175                 if (ptr == NULL) return;
176
177                 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
178                 ptr->next = uplist;
179                 uplist = ptr;
180                 return;
181         }
182
183         while (uplist != NULL) {
184
185                 if (CtdlGetUserLock(&us, uplist->user) == 0) {
186                         syslog(LOG_DEBUG, "Processing <%s>...", uplist->user);
187                         if (us.uid == CTDLUID) {
188                                 us.uid = (-1);
189                         }
190                         CtdlPutUserLock(&us);
191                 }
192
193                 ptr = uplist;
194                 uplist = uplist->next;
195                 free(ptr);
196         }
197 }
198
199 /*
200  * quick fix to change all CTDLUID users to (-1)
201  */
202 void convert_ctdluid_to_minusone(void) {
203         syslog(LOG_WARNING, "Applying uid changes");
204         ForEachUser(cbtm_backend, NULL);
205         cbtm_backend(NULL, NULL);
206         return;
207 }
208
209
210
211 /*
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.
214  */
215 void remove_thread_users(void) {
216         char *deleteusers[] = {
217                 "SYS_checkpoint",
218                 "SYS_extnotify",
219                 "SYS_IGnet Queue",
220                 "SYS_indexer",
221                 "SYS_network",
222                 "SYS_popclient",
223                 "SYS_purger",
224                 "SYS_rssclient",
225                 "SYS_select_on_master",
226                 "SYS_SMTP Send"
227         };
228
229         int i;
230         struct ctdluser usbuf;
231         for (i=0; i<(sizeof(deleteusers)/sizeof(char *)); ++i) {
232                 if (CtdlGetUser(&usbuf, deleteusers[i]) == 0) {
233                         usbuf.axlevel = 0;
234                         strcpy(usbuf.password, "deleteme");
235                         CtdlPutUser(&usbuf);
236                         syslog(LOG_INFO,
237                                 "System user account <%s> is no longer in use and will be deleted.",
238                                 deleteusers[i]
239                         );
240                 }
241         }
242 }
243
244
245 /*
246  * Attempt to guess the name of the time zone currently in use
247  * on the underlying host system.
248  */
249 void guess_time_zone(void) {
250         FILE *fp;
251         char buf[PATH_MAX];
252
253         fp = popen(file_guesstimezone, "r");
254         if (fp) {
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);
259                 }
260                 fclose(fp);
261         }
262 }
263
264
265 /*
266  * Do various things to our configuration file
267  */
268 void update_config(void) {
269         get_config();
270
271         if (CitControl.version < 606) {
272                 config.c_rfc822_strict_from = 0;
273         }
274
275         if (CitControl.version < 609) {
276                 config.c_purge_hour = 3;
277         }
278
279         if (CitControl.version < 615) {
280                 config.c_ldap_port = 389;
281         }
282
283         if (CitControl.version < 623) {
284                 strcpy(config.c_ip_addr, "*");
285         }
286
287         if (CitControl.version < 650) {
288                 config.c_enable_fulltext = 0;
289         }
290
291         if (CitControl.version < 652) {
292                 config.c_auto_cull = 1;
293         }
294
295         if (CitControl.version < 725) {
296                 config.c_xmpp_c2s_port = 5222;
297                 config.c_xmpp_s2s_port = 5269;
298         }
299
300         if (IsEmptyStr(config.c_default_cal_zone)) {
301                 guess_time_zone();
302         }
303
304         put_config();
305 }
306
307
308
309 /*
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.
312  */
313 void check_server_upgrades(void) {
314
315         get_control();
316         syslog(LOG_INFO, "Existing database version on disk is %d.%02d",
317                 (CitControl.version / 100),
318                 (CitControl.version % 100)
319         );
320
321         if (CitControl.version < REV_LEVEL) {
322                 syslog(LOG_WARNING,
323                         "Server hosted updates need to be processed at this time.  Please wait..."
324                 );
325         }
326         else {
327                 return;
328         }
329
330         update_config();
331
332         if ((CitControl.version > 000) && (CitControl.version < 555)) {
333                 syslog(LOG_EMERG, "This database is too old to be upgraded.  Citadel server will exit.");
334                 exit(EXIT_FAILURE);
335         }
336         if ((CitControl.version > 000) && (CitControl.version < 591)) {
337                 bump_mailbox_generation_numbers();
338         }
339         if ((CitControl.version > 000) && (CitControl.version < 608)) {
340                 convert_ctdluid_to_minusone();
341         }
342         if ((CitControl.version > 000) && (CitControl.version < 659)) {
343                 rebuild_euid_index();
344         }
345         if (CitControl.version < 735) {
346                 fix_sys_user_name();
347         }
348         if (CitControl.version < 736) {
349                 rebuild_usersbynumber();
350         }
351         if (CitControl.version < 790) {
352                 remove_thread_users();
353         }
354         CitControl.version = REV_LEVEL;
355         put_control();
356 }
357
358
359 CTDL_MODULE_UPGRADE(upgrade)
360 {
361         check_server_upgrades();
362         
363         /* return our module id for the Log */
364         return "upgrade";
365 }