* compatibility with Berkeley DB < 3.3
[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 "dynloader.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 void do_pre555_usersupp_upgrade(void) {
48         struct pre555usersupp usbuf;
49         struct usersupp newus;
50         struct cdbdata *cdbus;
51         char tempfilename[PATH_MAX];
52         FILE *fp, *tp;
53         static char vcard[1024];
54
55         lprintf(5, "Upgrading user file\n");
56         fp = tmpfile();
57         if (fp == NULL) {
58                 lprintf(1, "%s\n", strerror(errno));
59                 exit(errno);
60         }
61         strcpy(tempfilename, tmpnam(NULL));
62
63         /* First, back out all old version records to a flat file */
64         cdb_rewind(CDB_USERSUPP);
65         while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
66                 memset(&usbuf, 0, sizeof(struct pre555usersupp));
67                 memcpy(&usbuf, cdbus->ptr,
68                         ( (cdbus->len > sizeof(struct pre555usersupp)) ?
69                         sizeof(struct pre555usersupp) : cdbus->len) );
70                 cdb_free(cdbus);
71                 fwrite(&usbuf, sizeof(struct pre555usersupp), 1, fp);
72         }
73
74         /* ...and overwrite the records with new format records */
75         rewind(fp);
76         while (fread(&usbuf, sizeof(struct pre555usersupp), 1, fp) > 0) {
77             if (strlen(usbuf.fullname) > 0) {
78                 lprintf(9, "Upgrading <%s>\n", usbuf.fullname);
79                 memset(&newus, 0, sizeof(struct usersupp));
80
81                 newus.uid = usbuf.USuid;
82                 strcpy(newus.password, usbuf.password);
83                 newus.flags = usbuf.flags;
84                 newus.timescalled = (long) usbuf.timescalled;
85                 newus.posted = (long) usbuf.posted;
86                 newus.axlevel = (CIT_UBYTE) usbuf.axlevel;
87                 newus.usernum = (long) usbuf.usernum;
88                 newus.lastcall = (long) usbuf.lastcall;
89                 newus.USuserpurge = (int) usbuf.USuserpurge;
90                 strcpy(newus.fullname, usbuf.fullname);
91                 newus.USscreenwidth = (CIT_UBYTE) usbuf.USscreenwidth;
92                 newus.USscreenheight = (CIT_UBYTE) usbuf.USscreenheight;
93
94                 putuser(&newus);
95
96                 /* write the vcard */
97                 snprintf(vcard, sizeof vcard,
98                         "Content-type: text/x-vcard\n\n"
99                         "begin:vcard\n"
100                         "n:%s\n"
101                         "tel;home:%s\n"
102                         "email;internet:%s\n"
103                         "adr:;;%s;%s;%s;%s;USA\n"
104                         "end:vcard\n",
105                         usbuf.USname,
106                         usbuf.USphone,
107                         usbuf.USemail,
108                         usbuf.USaddr,
109                         usbuf.UScity,
110                         usbuf.USstate,
111                         usbuf.USzip);
112
113                 tp = fopen(tempfilename, "w");
114                 fwrite(vcard, strlen(vcard)+1, 1, tp);
115                 fclose(tp);
116
117                 CtdlWriteObject(USERCONFIGROOM, "text/x-vcard",
118                         tempfilename, &newus, 0, 1, CM_SKIP_HOOKS);
119                 unlink(tempfilename);
120             }
121         }
122
123         fclose(fp);     /* this file deletes automatically */
124 }
125
126
127
128
129
130
131
132
133 void check_server_upgrades(void) {
134
135         get_control();
136         lprintf(5, "Server-hosted upgrade level is %d.%02d\n",
137                 (CitControl.version / 100),
138                 (CitControl.version % 100) );
139
140         if (CitControl.version < REV_LEVEL) {
141                 lprintf(5, "Server hosted updates need to be processed at "
142                                 "this time.  Please wait...\n");
143         }
144         else {
145                 return;
146         }
147
148         if (CitControl.version < 555) do_pre555_usersupp_upgrade();
149
150         CitControl.version = REV_LEVEL;
151         put_control();
152 }
153
154
155
156
157
158
159
160
161
162
163
164 /* 
165  * Back end processing function for cmd_bmbx
166  */
167 void cmd_bmbx_backend(struct quickroom *qrbuf, void *data) {
168         static struct RoomProcList *rplist = NULL;
169         struct RoomProcList *ptr;
170         struct quickroom qr;
171
172         /* Lazy programming here.  Call this function as a ForEachRoom backend
173          * in order to queue up the room names, or call it with a null room
174          * to make it do the processing.
175          */
176         if (qrbuf != NULL) {
177                 ptr = (struct RoomProcList *)
178                         mallok(sizeof (struct RoomProcList));
179                 if (ptr == NULL) return;
180
181                 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
182                 ptr->next = rplist;
183                 rplist = ptr;
184                 return;
185         }
186
187         while (rplist != NULL) {
188
189                 if (lgetroom(&qr, rplist->name) == 0) {
190                         lprintf(9, "Processing <%s>...\n", rplist->name);
191                         if ( (qr.QRflags & QR_MAILBOX) == 0) {
192                                 lprintf(9, "  -- not a mailbox\n");
193                         }
194                         else {
195
196                                 qr.QRgen = time(NULL);
197                                 lprintf(9, "  -- bumped!\n");
198                         }
199                         lputroom(&qr);
200                 }
201
202                 ptr = rplist;
203                 rplist = rplist->next;
204                 phree(ptr);
205         }
206 }
207
208 /*
209  * quick fix command to bump mailbox generation numbers
210  */
211 void cmd_bmbx(char *argbuf) {
212         int really_do_this  = 0;
213
214         if (CtdlAccessCheck(ac_internal)) return;
215         really_do_this = extract_int(argbuf, 0);
216
217         if (really_do_this != 1) {
218                 cprintf("%d You didn't really want to do that.\n", CIT_OK);
219                 return;
220         }
221
222         ForEachRoom(cmd_bmbx_backend, NULL);
223         cmd_bmbx_backend(NULL, NULL);
224
225         cprintf("%d Mailbox generation numbers bumped.\n", CIT_OK);
226         return;
227
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241
242 char *Dynamic_Module_Init(void)
243 {
244         check_server_upgrades();
245         CtdlRegisterProtoHook(cmd_bmbx, "BMBX", "Bump mailboxes");
246         return "$Id$";
247 }