* Variable names, comments, documentation, etc... removed the acronym 'BBS'
[citadel.git] / citadel / serv_upgrade.c
1 /*
2  * $Id$
3  *
4  * Transparently handle the upgrading of server data formats.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <limits.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "sysdep_decls.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "control.h"
39 #include "serv_extensions.h"
40 #include "database.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "msgbase.h"
44 #include "tools.h"
45 #include "serv_upgrade.h"
46
47
48
49 /* 
50  * Back end processing function for cmd_bmbx
51  */
52 void cmd_bmbx_backend(struct ctdlroom *qrbuf, void *data) {
53         static struct RoomProcList *rplist = NULL;
54         struct RoomProcList *ptr;
55         struct ctdlroom qr;
56
57         /* Lazy programming here.  Call this function as a ForEachRoom backend
58          * in order to queue up the room names, or call it with a null room
59          * to make it do the processing.
60          */
61         if (qrbuf != NULL) {
62                 ptr = (struct RoomProcList *)
63                         malloc(sizeof (struct RoomProcList));
64                 if (ptr == NULL) return;
65
66                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
67                 ptr->next = rplist;
68                 rplist = ptr;
69                 return;
70         }
71
72         while (rplist != NULL) {
73
74                 if (lgetroom(&qr, rplist->name) == 0) {
75                         lprintf(CTDL_DEBUG, "Processing <%s>...\n", rplist->name);
76                         if ( (qr.QRflags & QR_MAILBOX) == 0) {
77                                 lprintf(CTDL_DEBUG, "  -- not a mailbox\n");
78                         }
79                         else {
80
81                                 qr.QRgen = time(NULL);
82                                 lprintf(CTDL_DEBUG, "  -- fixed!\n");
83                         }
84                         lputroom(&qr);
85                 }
86
87                 ptr = rplist;
88                 rplist = rplist->next;
89                 free(ptr);
90         }
91 }
92
93 /*
94  * quick fix to bump mailbox generation numbers
95  */
96 void bump_mailbox_generation_numbers(void) {
97         lprintf(CTDL_WARNING, "Applying security fix to mailbox rooms\n");
98         ForEachRoom(cmd_bmbx_backend, NULL);
99         cmd_bmbx_backend(NULL, NULL);
100         return;
101 }
102
103
104 /* 
105  * Back end processing function for convert_ctdluid_to_minusone()
106  */
107 void cbtm_backend(struct ctdluser *usbuf, void *data) {
108         static struct UserProcList *uplist = NULL;
109         struct UserProcList *ptr;
110         struct ctdluser us;
111
112         /* Lazy programming here.  Call this function as a ForEachUser backend
113          * in order to queue up the room names, or call it with a null user
114          * to make it do the processing.
115          */
116         if (usbuf != NULL) {
117                 ptr = (struct UserProcList *)
118                         malloc(sizeof (struct UserProcList));
119                 if (ptr == NULL) return;
120
121                 safestrncpy(ptr->user, usbuf->fullname, sizeof ptr->user);
122                 ptr->next = uplist;
123                 uplist = ptr;
124                 return;
125         }
126
127         while (uplist != NULL) {
128
129                 if (lgetuser(&us, uplist->user) == 0) {
130                         lprintf(CTDL_DEBUG, "Processing <%s>...\n", uplist->user);
131                         if (us.uid == CTDLUID) {
132                                 us.uid = (-1);
133                         }
134                         lputuser(&us);
135                 }
136
137                 ptr = uplist;
138                 uplist = uplist->next;
139                 free(ptr);
140         }
141 }
142
143 /*
144  * quick fix to change all CTDLUID users to (-1)
145  */
146 void convert_ctdluid_to_minusone(void) {
147         lprintf(CTDL_WARNING, "Applying uid changes\n");
148         ForEachUser(cbtm_backend, NULL);
149         cbtm_backend(NULL, NULL);
150         return;
151 }
152
153 /*
154  * Do various things to our configuration file
155  */
156 void update_config(void) {
157         get_config();
158
159         if (CitControl.version < 606) {
160                 config.c_rfc822_strict_from = 0;
161         }
162
163         if (CitControl.version < 609) {
164                 config.c_purge_hour = 3;
165         }
166
167         if (CitControl.version < 615) {
168                 config.c_ldap_port = 389;
169         }
170
171         if (CitControl.version < 623) {
172                 strcpy(config.c_ip_addr, "0.0.0.0");
173         }
174
175         put_config();
176 }
177
178
179
180
181 void check_server_upgrades(void) {
182
183         get_control();
184         lprintf(CTDL_INFO, "Server-hosted upgrade level is %d.%02d\n",
185                 (CitControl.version / 100),
186                 (CitControl.version % 100) );
187
188         if (CitControl.version < REV_LEVEL) {
189                 lprintf(CTDL_WARNING,
190                         "Server hosted updates need to be processed at "
191                         "this time.  Please wait...\n");
192         }
193         else {
194                 return;
195         }
196
197         update_config();
198
199         if ((CitControl.version > 000) && (CitControl.version < 555)) {
200                 lprintf(CTDL_EMERG,
201                         "Your data files are from a version of Citadel\n"
202                         "that is too old to be upgraded.  Sorry.\n");
203                 exit(EXIT_FAILURE);
204         }
205         if ((CitControl.version > 000) && (CitControl.version < 591)) {
206                 bump_mailbox_generation_numbers();
207         }
208         if ((CitControl.version > 000) && (CitControl.version < 608)) {
209                 convert_ctdluid_to_minusone();
210         }
211
212         CitControl.version = REV_LEVEL;
213         put_control();
214 }
215
216
217 char *serv_upgrade_init(void)
218 {
219         check_server_upgrades();
220         return "$Id$";
221 }