]> code.citadel.org Git - citadel.git/blob - citadel/serv_upgrade.c
755dfa99d814ccb0c5c9f75bb2a50c96af71196a
[citadel.git] / citadel / serv_upgrade.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <fcntl.h>
5 #include <signal.h>
6 #include <pwd.h>
7 #include <errno.h>
8 #include <sys/types.h>
9 #include <sys/time.h>
10 #include <sys/wait.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <pthread.h>
14 #include "citadel.h"
15 #include "server.h"
16 #include <syslog.h>
17 #include "sysdep_decls.h"
18 #include "citserver.h"
19 #include "support.h"
20 #include "config.h"
21 #include "dynloader.h"
22 #include "room_ops.h"
23 #include "user_ops.h"
24 #include "database.h"
25 #include "control.h"
26
27 extern struct CitContext *ContextList;
28 FILE *imfp;
29
30 #define MODULE_NAME     "Import an unpacked system"
31 #define MODULE_AUTHOR   "Art Cancro"
32 #define MODULE_EMAIL    "ajc@uncnsrd.mt-kisco.ny.us"
33 #define MAJOR_VERSION   0
34 #define MINOR_VERSION   1
35
36 static struct DLModule_Info info = {
37   MODULE_NAME,
38   MODULE_AUTHOR,
39   MODULE_EMAIL,
40   MAJOR_VERSION,
41   MINOR_VERSION
42 };
43
44
45
46
47 void fpgetfield(fp,string)
48 FILE *fp;
49 char string[];
50 {
51         int a,b;
52         strcpy(string,"");
53         a=0;
54         do {
55                 b=getc(fp);
56                 if (b<1) {
57                         string[a]=0;
58                         return;
59                         }
60                 string[a]=b;
61                 ++a;
62                 } while (b!=0);
63         }
64
65
66 void import_message(long msgnum, long msglen) {
67         char *msgtext;
68
69         msgtext = malloc(msglen);
70         if (msgtext == NULL) {
71                 lprintf(3, "ERROR: cannot allocate memory\n");
72                 lprintf(3, "Your data files are now corrupt.\n");
73                 fclose(imfp);
74                 exit(1);
75                 }
76
77         fread(msgtext, msglen, 1, imfp);
78         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), msgtext, msglen);
79         free(msgtext);
80         }
81
82 void imp_floors() {
83         char key[256], tag[256], tval[256];
84         struct floor fl;
85         int floornum = 0;
86
87
88         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
89
90                 if (!strcasecmp(key, "floor")) {
91                         bzero(&fl, sizeof(struct floor));
92
93                         while(fpgetfield(imfp, tag),
94                              strcasecmp(tag, "endfloor")) {
95                                 fpgetfield(imfp, tval);
96
97                                 if (!strcasecmp(tag, "f_flags")) 
98                                         fl.f_flags = atoi(tval);
99                                 if (!strcasecmp(tag, "f_name")) {
100                                         lprintf(9, "Floor <%s>\n", tval);
101                                         strcpy(fl.f_name, tval);        
102                                         }
103                                 if (!strcasecmp(tag, "f_ref_count")) 
104                                         fl.f_ref_count = atoi(tval);
105                                 }
106
107                         putfloor(&fl, floornum);
108                         ++floornum;
109                         }
110                 else {
111                         lprintf(3, "ERROR: invalid floor section.\n");
112                         lprintf(3, "Your data files are now corrupt.\n");
113                         fclose(imfp);
114                         return;
115                         }
116                 }
117         }
118
119
120
121 void imp_rooms() {
122         char key[256];
123         char tag[256], tval[256];
124         int roomnum = 0;
125         struct quickroom qr;
126         long *msglist;
127         int num_msgs = 0;
128         long msgnum, msglen;
129         
130         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
131                 if (!strcasecmp(key, "room")) {
132                         bzero(&qr, sizeof(struct quickroom));
133                         msglist = NULL;
134                         num_msgs = 0;
135                         lprintf(9, "Room ");
136
137                         while(fpgetfield(imfp, tag),
138                              strcasecmp(tag, "endroom")) {
139                                 if (strcasecmp(tag, "message")) {
140                                         fpgetfield(imfp, tval);
141                                         }
142                                 else {
143                                         strcpy(tval, "");
144                                         }
145
146                                 if (!strcasecmp(tag, "qrname")) {
147                                         strcpy(qr.QRname, tval);
148                                         lprintf(9, "<%s>", qr.QRname);
149                                         }
150                                 if (!strcasecmp(tag, "qrpasswd"))
151                                         strcpy(qr.QRpasswd, tval);
152                                 if (!strcasecmp(tag, "qrroomaide"))
153                                         qr.QRroomaide = atol(tval);
154                                 if (!strcasecmp(tag, "qrhighest"))
155                                         qr.QRhighest = atol(tval);
156                                 if (!strcasecmp(tag, "qrgen"))
157                                         qr.QRgen = atol(tval);
158                                 if (!strcasecmp(tag, "qrflags"))
159                                         qr.QRflags = atoi(tval);
160                                 if (!strcasecmp(tag, "qrdirname"))
161                                         strcpy(qr.QRdirname, tval);
162                                 if (!strcasecmp(tag, "qrinfo"))
163                                         qr.QRinfo = atol(tval);
164                                 if (!strcasecmp(tag, "qrfloor"))
165                                         qr.QRfloor = atoi(tval);
166                                 if (!strcasecmp(tag, "message")) {
167                                         lprintf(9, ".");
168                                         fpgetfield(imfp, tval);
169                                         msgnum = atol(tval);
170                                         fpgetfield(imfp, tval);
171                                         msglen = atol(tval);
172                                         import_message(msgnum, msglen);
173                                         ++num_msgs;
174                                         msglist = realloc(msglist,
175                                                 (sizeof(long)*num_msgs) );
176                                         msglist[num_msgs - 1] = msgnum;
177                                         }
178
179                                 }
180
181                         lprintf(9, "\n");
182                         if ((roomnum!=1)&&(qr.QRflags&QR_INUSE)) {
183                                 putroom(&qr, qr.QRname);
184                                 }
185
186                         if (num_msgs > 0) {
187                                 if ((roomnum!=1)&&(qr.QRflags&QR_INUSE)) {
188                                         CC->msglist = msglist;
189                                         CC->num_msgs = num_msgs;
190                                         put_msglist(&qr);
191                                         }
192                                 free(msglist);
193                                 }
194
195                         ++roomnum;
196
197                         }
198                 else {
199                         lprintf(3, "ERROR: invalid room section.\n");
200                         lprintf(3, "Your data files are now corrupt.\n");
201                         fclose(imfp);
202                         return;
203                         }
204                 }
205         }
206
207
208
209
210
211 void import_a_user() {
212         char key[256], value[256], list[256];
213         char vkey[256], vvalue[256];
214         struct usersupp us;
215         struct quickroom qr;
216         struct visit vbuf;
217         long *mbox = NULL;
218         int mbox_size = 0;
219         long msgnum;
220         long msglen;
221
222         bzero(&us, sizeof(struct usersupp));    
223         while(fpgetfield(imfp, key), strcasecmp(key, "enduser")) {
224                 if ((strcasecmp(key, "mail"))
225                    &&(strcasecmp(key, "lastseen"))
226                    &&(strcasecmp(key, "generation"))
227                    &&(strcasecmp(key, "forget")) ) {
228                         fpgetfield(imfp, value);
229                         }
230                 else {
231                         strcpy(value, "");
232                         }
233
234                 if (!strcasecmp(key, "usuid"))
235                         us.USuid = atoi(value);
236                 if (!strcasecmp(key, "password")) {
237                         strcpy(us.password, value);
238                         }
239
240                 if (!strcasecmp(key, "flags"))
241                         us.flags = atoi(value);
242                 if (!strcasecmp(key, "timescalled"))
243                         us.timescalled = atoi(value);
244                 if (!strcasecmp(key, "posted"))
245                         us.posted = atoi(value);
246                 if (!strcasecmp(key, "fullname")) {
247                         strcpy(us.fullname, value);
248                         lprintf(9, "User <%s>", us.fullname);
249                         }
250                 if (!strcasecmp(key, "axlevel"))
251                         us.axlevel = atoi(value);
252                 if (!strcasecmp(key, "usscreenwidth"))
253                         us.USscreenwidth = atoi(value);
254                 if (!strcasecmp(key, "usscreenheight"))
255                         us.USscreenheight = atoi(value);
256                 if (!strcasecmp(key, "usernum"))
257                         us.usernum = atol(value);
258                 if (!strcasecmp(key, "lastcall"))
259                         us.lastcall = atol(value);
260                 if (!strcasecmp(key, "usname"))
261                         strcpy(us.USname, value);
262                 if (!strcasecmp(key, "usaddr"))
263                         strcpy(us.USaddr, value);
264                 if (!strcasecmp(key, "uscity"))
265                         strcpy(us.UScity, value);
266                 if (!strcasecmp(key, "usstate"))
267                         strcpy(us.USstate, value);
268                 if (!strcasecmp(key, "uszip"))
269                         strcpy(us.USzip, value);
270                 if (!strcasecmp(key, "usphone"))
271                         strcpy(us.USphone, value);
272                 if (!strcasecmp(key, "usemail"))
273                         strcpy(us.USemail, value);
274                 if (!strcasecmp(key, "mail")) {
275                         lprintf(9, ".");
276                         fpgetfield(imfp, list);
277                         msgnum = atol(list);
278                         fpgetfield(imfp, list);
279                         msglen = atol(list);
280                         import_message(msgnum, msglen);
281                         ++mbox_size;
282                         mbox = realloc(mbox, (sizeof(long)*mbox_size) );
283                         mbox[mbox_size - 1] = msgnum;
284                         }
285                 if (!strcasecmp(key, "visit")) {
286                         lprintf(9,"visit: ");
287                         bzero(&vbuf, sizeof(struct visit));
288                         bzero(&qr, sizeof(struct quickroom));
289                         while(fpgetfield(imfp, vkey),
290                           strcasecmp(vkey, "endvisit")) {
291                                 fpgetfield(imfp, vvalue);
292                                 if (!strcasecmp(vkey, "vname")) 
293                                         strcpy(qr.QRname, vvalue);
294                                 if (!strcasecmp(vkey, "vgen"))  {
295                                         qr.QRgen = atol(vvalue);
296                                         lprintf(9, "<%s><%ld> ",
297                                                 qr.QRname, qr.QRgen);
298                                         CtdlGetRelationship(&vbuf, &us, &qr);
299                                         }
300                                 if (!strcasecmp(vkey, "lastseen"))      
301                                         vbuf.v_lastseen = atol(vvalue);
302                                 if (!strcasecmp(vkey, "flags"))
303                                         vbuf.v_flags = atoi(vvalue);
304                                 }
305                         CtdlSetRelationship(&vbuf, &us, &qr);
306                         lprintf(9, ".\n");
307                         }
308                 }
309         
310         putuser(&us, us.fullname);
311
312         /* create a mailbox room */
313         bzero(&qr, sizeof(struct quickroom));
314         MailboxName(qr.QRname, &us, MAILROOM);
315         qr.QRflags = QR_INUSE | QR_MAILBOX;
316         time(&qr.QRgen);
317         if (mbox_size > 0) qr.QRhighest = mbox[mbox_size - 1];
318         putroom(&qr, qr.QRname);
319
320         CC->msglist = mbox;
321         CC->num_msgs = mbox_size;
322         put_msglist(&qr);
323
324         free(mbox);
325         lprintf(9, "\n");
326         }
327
328
329 void imp_usersupp() {
330         char key[256], value[256];
331         
332         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
333                 if (strcasecmp(key, "user")) {
334                         fpgetfield(imfp, value);
335                         }
336                 else {
337                         strcpy(value, "");
338                         }
339                 lprintf(9, " %s = %s\n", key, value);
340
341                 if (!strcasecmp(key, "user")) {
342                         import_a_user();
343                         }
344                 }               
345         }
346
347
348
349
350
351 void imp_globals() {
352         char key[256], value[256];
353
354         get_control();
355         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
356                 fpgetfield(imfp, value);
357                 lprintf(9, " %s = %s\n", key, value);
358
359                 if (!strcasecmp(key, "mmhighest"))
360                         CitControl.MMhighest = atol(value);
361                 if (!strcasecmp(key, "mmnextuser"))
362                         CitControl.MMnextuser = atol(value);
363
364                 }
365         put_control();
366         }
367
368
369
370 void imp_config() { 
371         char key[256], value[256];
372         FILE *fp;
373
374         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
375                 fpgetfield(imfp, value);
376                 lprintf(9, " %s = %s\n", key, value);
377
378                 if (!strcasecmp(key, "c_nodename"))
379                         strcpy(config.c_nodename, value);
380                 if (!strcasecmp(key, "c_fqdn"))
381                         strcpy(config.c_fqdn, value);
382                 if (!strcasecmp(key, "c_humannode"))
383                         strcpy(config.c_humannode, value);
384                 if (!strcasecmp(key, "c_phonenum"))
385                         strcpy(config.c_phonenum, value);
386                 if (!strcasecmp(key, "c_phonenum"))
387                         strcpy(config.c_phonenum, value);
388                 if (!strcasecmp(key, "c_bbsuid"))
389                         config.c_bbsuid = atoi(value);
390                 if (!strcasecmp(key, "c_creataide"))
391                         config.c_creataide = atoi(value);
392                 if (!strcasecmp(key, "c_sleeping"))
393                         config.c_sleeping = atoi(value);
394                 if (!strcasecmp(key, "c_initax"))
395                         config.c_initax = atoi(value);
396                 if (!strcasecmp(key, "c_regiscall"))
397                         config.c_regiscall = atoi(value);
398                 if (!strcasecmp(key, "c_twitdetect"))
399                         config.c_twitdetect = atoi(value);
400                 if (!strcasecmp(key, "c_twitroom"))
401                         strcpy(config.c_twitroom, value);
402                 if (!strcasecmp(key, "c_defent"))
403                         config.c_defent = atoi(value);
404                 if (!strcasecmp(key, "c_moreprompt"))
405                         strcpy(config.c_moreprompt, value);
406                 if (!strcasecmp(key, "c_restrict"))
407                         config.c_restrict = atoi(value);
408                 if (!strcasecmp(key, "c_bbs_city"))
409                         strcpy(config.c_bbs_city, value);
410                 if (!strcasecmp(key, "c_sysadm"))
411                         strcpy(config.c_sysadm, value);
412                 if (!strcasecmp(key, "c_bucket_dir"))
413                         strcpy(config.c_bucket_dir, value);
414                 if (!strcasecmp(key, "c_setup_level"))
415                         config.c_setup_level = atoi(value);
416                 if (!strcasecmp(key, "c_maxsessions"))
417                         config.c_maxsessions = atoi(value);
418                 if (!strcasecmp(key, "c_net_password"))
419                         strcpy(config.c_net_password, value);
420                 if (!strcasecmp(key, "c_port_number"))
421                         config.c_port_number = atoi(value);
422                 }
423
424         fp = fopen("citadel.config", "wb");
425         fwrite(&config, sizeof(struct config), 1, fp);
426         fclose(fp);
427         }
428                 
429                 
430
431
432
433 void imp_ssv() {
434         char key[256], value[256];
435         int ssv_maxfloors = MAXFLOORS;
436         
437         while(fpgetfield(imfp, key), strcasecmp(key, "endsection")) {
438                 fpgetfield(imfp, value);
439                 lprintf(9, " %s = %s\n", key, value);
440                 
441                 if (!strcasecmp(key, "maxfloors")) {
442                         ssv_maxfloors = atol(value);
443                         if (ssv_maxfloors > MAXFLOORS) {
444                                 lprintf(3, "ERROR: maxfloors is %d, need %d\n",
445                                         ssv_maxfloors, MAXFLOORS);
446                                 fclose(imfp);
447                                 return;
448                                 }
449                         }
450                 }
451         }
452
453
454
455
456
457
458
459 void import_databases() {
460         char section[256];
461
462         lprintf(9, " ** IMPORTING ** \n");
463         while (fpgetfield(imfp, section), strcasecmp(section, "endfile")) {
464                 lprintf(9, "Section: <%s>\n", section);
465
466                 if (!strcasecmp(section, "ssv"))                imp_ssv();
467                 else if (!strcasecmp(section, "config"))        imp_config();
468                 else if (!strcasecmp(section, "globals"))       imp_globals();
469                 else if (!strcasecmp(section, "usersupp"))      imp_usersupp();
470                 else if (!strcasecmp(section, "rooms"))         imp_rooms();
471                 else if (!strcasecmp(section, "floors"))        imp_floors();
472                 else {
473                         lprintf(3, "ERROR: invalid import section.\n");
474                         lprintf(3, "Your data files are now corrupt.\n");
475                         fclose(imfp);
476                         return;
477                         }
478
479                 }
480
481         }
482
483
484
485 void do_import(char *argbuf) {
486         char import_filename[PATH_MAX];
487         
488
489         if (num_parms(argbuf) != 1) {
490                 cprintf("%d usage: IMPO unpacked_filename\n", ERROR);
491                 return;
492                 }
493         extract(import_filename, argbuf, 0);
494         imfp = fopen(import_filename, "rb");
495         if (imfp == NULL) {
496                 lprintf(9, "Cannot open %s: %s\n",
497                         import_filename, strerror(errno));
498                 cprintf("%d Cannot open file\n", ERROR);
499                 return;
500                 }
501
502         import_databases();
503         cprintf("%d ok\n", OK);
504         }
505
506
507 struct DLModule_Info *Dynamic_Module_Init(void) {
508         CtdlRegisterProtoHook(do_import,
509                                 "IMPO",
510                                 "Import an unpacked Cit5");
511         return &info;
512         }