* Use syslog-compatible logging levels in lprintf(); the loglevel chosen
[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                         mallok(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                 phree(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_bbsuid_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                         mallok(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 == BBSUID) {
132                                 us.uid = (-1);
133                         }
134                         lputuser(&us);
135                 }
136
137                 ptr = uplist;
138                 uplist = uplist->next;
139                 phree(ptr);
140         }
141 }
142
143 /*
144  * quick fix to change all BBSUID users to (-1)
145  */
146 void convert_bbsuid_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         put_config();
172 }
173
174
175
176
177 void check_server_upgrades(void) {
178
179         get_control();
180         lprintf(CTDL_INFO, "Server-hosted upgrade level is %d.%02d\n",
181                 (CitControl.version / 100),
182                 (CitControl.version % 100) );
183
184         if (CitControl.version < REV_LEVEL) {
185                 lprintf(CTDL_WARNING,
186                         "Server hosted updates need to be processed at "
187                         "this time.  Please wait...\n");
188         }
189         else {
190                 return;
191         }
192
193         update_config();
194
195         if ((CitControl.version > 000) && (CitControl.version < 555)) {
196                 lprintf(CTDL_EMERG,
197                         "Your data files are from a version of Citadel\n"
198                         "that is too old to be upgraded.  Sorry.\n");
199                 exit(EXIT_FAILURE);
200         }
201         if ((CitControl.version > 000) && (CitControl.version < 591)) {
202                 bump_mailbox_generation_numbers();
203         }
204         if ((CitControl.version > 000) && (CitControl.version < 608)) {
205                 convert_bbsuid_to_minusone();
206         }
207
208         CitControl.version = REV_LEVEL;
209         put_control();
210 }
211
212
213 char *serv_upgrade_init(void)
214 {
215         check_server_upgrades();
216         return "$Id$";
217 }