ed3ea391c88a478661bbf132de8496d1545c6efa
[citadel.git] / citadel / modules / migrate / serv_migrate.c
1 /*
2  * This module dumps and/or loads the Citadel database in XML format.
3  *
4  * Copyright (c) 1987-2020 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 /*
16  * Explanation of <progress> tags:
17  *
18  * 0%              nothing
19  * 2%              finished exporting configuration
20  * 7%              finished exporting users
21  * 12%             finished exporting openids
22  * 17%             finished exporting rooms
23  * 18%             finished exporting floors
24  * 25%             finished exporting visits
25  * 100%            finished exporting messages
26  */
27
28 #include "sysdep.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <pwd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 #  include <sys/time.h>
44 # else
45 #  include <time.h>
46 # endif
47 #endif
48
49 #include <sys/wait.h>
50 #include <string.h>
51 #include <ctype.h>
52 #include <limits.h>
53 #include <expat.h>
54 #include <libcitadel.h>
55 #include "citadel.h"
56 #include "server.h"
57 #include "citserver.h"
58 #include "support.h"
59 #include "config.h"
60 #include "database.h"
61 #include "msgbase.h"
62 #include "user_ops.h"
63 #include "euidindex.h"
64 #include "ctdl_module.h"
65
66 #define END_OF_MESSAGE  "---eom---dbd---"
67
68 char migr_tempfilename1[PATH_MAX];
69 char migr_tempfilename2[PATH_MAX];
70 FILE *migr_global_message_list;
71 int total_msgs = 0;
72 char *ikey = NULL;                      // If we're importing a config key we store it here.
73
74
75 /******************************************************************************
76  *        Code which implements the export appears in this section            *
77  ******************************************************************************/
78
79 /*
80  * Output a string to the client with these characters escaped:  & < > " '
81  */
82 void xml_strout(char *str) {
83
84         char *c = str;
85
86         if (str == NULL) {
87                 return;
88         }
89
90         while (*c != 0) {
91                 if (*c == '\"') {
92                         client_write(HKEY("&quot;"));
93                 }
94                 else if (*c == '\'') {
95                         client_write(HKEY("&apos;"));
96                 }
97                 else if (*c == '<') {
98                         client_write(HKEY("&lt;"));
99                 }
100                 else if (*c == '>') {
101                         client_write(HKEY("&gt;"));
102                 }
103                 else if (*c == '&') {
104                         client_write(HKEY("&amp;"));
105                 }
106                 else {
107                         client_write(c, 1);
108                 }
109                 ++c;
110         }
111 }
112
113
114 /*
115  * Export a user record as XML
116  */
117 void migr_export_users_backend(char *username, void *data) {
118
119         struct ctdluser u;
120         if (CtdlGetUser(&u, username) != 0) {
121                 return;
122         }
123
124         client_write(HKEY("<user>\n"));
125         cprintf("<u_version>%d</u_version>\n", u.version);
126         cprintf("<u_uid>%ld</u_uid>\n", (long)u.uid);
127         client_write(HKEY("<u_password>"));     xml_strout(u.password);         client_write(HKEY("</u_password>\n"));
128         cprintf("<u_flags>%u</u_flags>\n", u.flags);
129         cprintf("<u_timescalled>%ld</u_timescalled>\n", u.timescalled);
130         cprintf("<u_posted>%ld</u_posted>\n", u.posted);
131         cprintf("<u_axlevel>%d</u_axlevel>\n", u.axlevel);
132         cprintf("<u_usernum>%ld</u_usernum>\n", u.usernum);
133         cprintf("<u_lastcall>%ld</u_lastcall>\n", (long)u.lastcall);
134         cprintf("<u_USuserpurge>%d</u_USuserpurge>\n", u.USuserpurge);
135         client_write(HKEY("<u_fullname>"));     xml_strout(u.fullname);         client_write(HKEY("</u_fullname>\n"));
136         cprintf("<u_msgnum_bio>%ld</u_msgnum_bio>\n", u.msgnum_bio);
137         cprintf("<u_msgnum_pic>%ld</u_msgnum_pic>\n", u.msgnum_pic);
138         cprintf("<u_emailaddrs>%s</u_emailaddrs>\n", u.emailaddrs);
139         cprintf("<u_msgnum_inboxrules>%ld</u_msgnum_inboxrules>\n", u.msgnum_inboxrules);
140         cprintf("<u_lastproc_inboxrules>%ld</u_lastproc_inboxrules>\n", u.lastproc_inboxrules);
141         client_write(HKEY("</user>\n"));
142 }
143
144
145 void migr_export_users(void) {
146         ForEachUser(migr_export_users_backend, NULL);
147 }
148
149
150 void migr_export_room_msg(long msgnum, void *userdata) {
151         cprintf("%ld\n", msgnum);
152         fprintf(migr_global_message_list, "%ld\n", msgnum);
153 }
154
155
156 void migr_export_rooms_backend(struct ctdlroom *buf, void *data) {
157         client_write(HKEY("<room>\n"));
158         client_write(HKEY("<QRname>")); xml_strout(buf->QRname);        client_write(HKEY("</QRname>\n"));
159         client_write(HKEY("<QRpasswd>"));       xml_strout(buf->QRpasswd);      client_write(HKEY("</QRpasswd>\n"));
160         cprintf("<QRroomaide>%ld</QRroomaide>\n", buf->QRroomaide);
161         cprintf("<QRhighest>%ld</QRhighest>\n", buf->QRhighest);
162         cprintf("<QRgen>%ld</QRgen>\n", (long)buf->QRgen);
163         cprintf("<QRflags>%u</QRflags>\n", buf->QRflags);
164         if (buf->QRflags & QR_DIRECTORY) {
165                 client_write(HKEY("<QRdirname>"));
166                 xml_strout(buf->QRdirname);
167                 client_write(HKEY("</QRdirname>\n"));
168         }
169         cprintf("<QRfloor>%d</QRfloor>\n", buf->QRfloor);
170         cprintf("<QRmtime>%ld</QRmtime>\n", (long)buf->QRmtime);
171         cprintf("<QRexpire_mode>%d</QRexpire_mode>\n", buf->QRep.expire_mode);
172         cprintf("<QRexpire_value>%d</QRexpire_value>\n", buf->QRep.expire_value);
173         cprintf("<QRnumber>%ld</QRnumber>\n", buf->QRnumber);
174         cprintf("<QRorder>%d</QRorder>\n", buf->QRorder);
175         cprintf("<QRflags2>%u</QRflags2>\n", buf->QRflags2);
176         cprintf("<QRdefaultview>%d</QRdefaultview>\n", buf->QRdefaultview);
177         cprintf("<msgnum_info>%ld</msgnum_info>\n", buf->msgnum_info);
178         cprintf("<msgnum_pic>%ld</msgnum_pic>\n", buf->msgnum_pic);
179         client_write(HKEY("</room>\n"));
180
181         /* message list goes inside this tag */
182
183         CtdlGetRoom(&CC->room, buf->QRname);
184         client_write(HKEY("<room_messages>"));
185         client_write(HKEY("<FRname>")); xml_strout(CC->room.QRname);    client_write(HKEY("</FRname>\n"));
186         client_write(HKEY("<FRmsglist>"));
187         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_export_room_msg, NULL);
188         client_write(HKEY("</FRmsglist>\n"));
189         client_write(HKEY("</room_messages>\n"));
190
191
192 }
193
194
195 void migr_export_rooms(void) {
196         char cmd[SIZ];
197         migr_global_message_list = fopen(migr_tempfilename1, "w");
198         if (migr_global_message_list != NULL) {
199                 CtdlForEachRoom(migr_export_rooms_backend, NULL);
200                 fclose(migr_global_message_list);
201         }
202
203         /*
204          * Process the 'global' message list.  (Sort it and remove dups.
205          * Dups are ok because a message may be in more than one room, but
206          * this will be handled by exporting the reference count, not by
207          * exporting the message multiple times.)
208          */
209         snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
210         if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
211         snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
212         if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
213
214
215         snprintf(cmd, sizeof cmd, "wc -l %s", migr_tempfilename1);
216         FILE *fp = popen(cmd, "r");
217         if (fp) {
218                 fgets(cmd, sizeof cmd, fp);
219                 pclose(fp);
220                 total_msgs = atoi(cmd);
221         }
222         else {
223                 total_msgs = 1; // any nonzero just to keep it from barfing
224         }
225         syslog(LOG_DEBUG, "Total messages to be exported: %d", total_msgs);
226 }
227
228
229 void migr_export_floors(void) {
230         struct floor qfbuf, *buf;
231         int i;
232
233         for (i=0; i < MAXFLOORS; ++i) {
234                 client_write(HKEY("<floor>\n"));
235                 cprintf("<f_num>%d</f_num>\n", i);
236                 CtdlGetFloor(&qfbuf, i);
237                 buf = &qfbuf;
238                 cprintf("<f_flags>%u</f_flags>\n", buf->f_flags);
239                 client_write(HKEY("<f_name>")); xml_strout(buf->f_name); client_write(HKEY("</f_name>\n"));
240                 cprintf("<f_ref_count>%d</f_ref_count>\n", buf->f_ref_count);
241                 cprintf("<f_ep_expire_mode>%d</f_ep_expire_mode>\n", buf->f_ep.expire_mode);
242                 cprintf("<f_ep_expire_value>%d</f_ep_expire_value>\n", buf->f_ep.expire_value);
243                 client_write(HKEY("</floor>\n"));
244         }
245 }
246
247
248 /*
249  * Return nonzero if the supplied string contains only characters which are valid in a sequence set.
250  */
251 int is_sequence_set(char *s) {
252         if (!s) return(0);
253
254         char *c = s;
255         char ch;
256         while (ch = *c++, ch) {
257                 if (!strchr("0123456789*,:", ch)) {
258                         return(0);
259                 }
260         }
261         return(1);
262 }
263
264
265 /* 
266  *  Traverse the visits file...
267  */
268 void migr_export_visits(void) {
269         visit vbuf;
270         struct cdbdata *cdbv;
271
272         cdb_rewind(CDB_VISIT);
273
274         while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
275                 memset(&vbuf, 0, sizeof(visit));
276                 memcpy(&vbuf, cdbv->ptr,
277                        ((cdbv->len > sizeof(visit)) ?
278                         sizeof(visit) : cdbv->len));
279                 cdb_free(cdbv);
280
281                 client_write(HKEY("<visit>\n"));
282                 cprintf("<v_roomnum>%ld</v_roomnum>\n", vbuf.v_roomnum);
283                 cprintf("<v_roomgen>%ld</v_roomgen>\n", vbuf.v_roomgen);
284                 cprintf("<v_usernum>%ld</v_usernum>\n", vbuf.v_usernum);
285
286                 client_write(HKEY("<v_seen>"));
287                 if ( (!IsEmptyStr(vbuf.v_seen)) && (is_sequence_set(vbuf.v_seen)) ) {
288                         xml_strout(vbuf.v_seen);
289                 }
290                 else {
291                         cprintf("%ld", vbuf.v_lastseen);
292                 }
293                 client_write(HKEY("</v_seen>"));
294
295                 if ( (!IsEmptyStr(vbuf.v_answered)) && (is_sequence_set(vbuf.v_answered)) ) {
296                         client_write(HKEY("<v_answered>"));
297                         xml_strout(vbuf.v_answered);
298                         client_write(HKEY("</v_answered>\n"));
299                 }
300
301                 cprintf("<v_flags>%u</v_flags>\n", vbuf.v_flags);
302                 cprintf("<v_view>%d</v_view>\n", vbuf.v_view);
303                 client_write(HKEY("</visit>\n"));
304         }
305 }
306
307
308 void migr_export_message(long msgnum) {
309         struct MetaData smi;
310         struct CtdlMessage *msg;
311         struct ser_ret smr;
312
313         /* We can use a static buffer here because there will never be more than
314          * one of this operation happening at any given time, and it's really best
315          * to just keep it allocated once instead of torturing malloc/free.
316          * Call this function with msgnum "-1" to free the buffer when finished.
317          */
318         static int encoded_alloc = 0;
319         static char *encoded_msg = NULL;
320
321         if (msgnum < 0) {
322                 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
323                         free(encoded_msg);
324                         encoded_alloc = 0;
325                         encoded_msg = NULL;
326                 }
327                 return;
328         }
329
330         /* Ok, here we go ... */
331
332         msg = CtdlFetchMessage(msgnum, 1);
333         if (msg == NULL) return;        /* fail silently */
334
335         client_write(HKEY("<message>\n"));
336         GetMetaData(&smi, msgnum);
337         cprintf("<msg_msgnum>%ld</msg_msgnum>\n", msgnum);
338         cprintf("<msg_meta_refcount>%d</msg_meta_refcount>\n", smi.meta_refcount);
339         cprintf("<msg_meta_rfc822_length>%ld</msg_meta_rfc822_length>\n", smi.meta_rfc822_length);
340         client_write(HKEY("<msg_meta_content_type>")); xml_strout(smi.meta_content_type); client_write(HKEY("</msg_meta_content_type>\n"));
341
342         client_write(HKEY("<msg_text>"));
343         CtdlSerializeMessage(&smr, msg);
344         CM_Free(msg);
345
346         /* Predict the buffer size we need.  Expand the buffer if necessary. */
347         int encoded_len = smr.len * 15 / 10 ;
348         if (encoded_len > encoded_alloc) {
349                 encoded_alloc = encoded_len;
350                 encoded_msg = realloc(encoded_msg, encoded_alloc);
351         }
352
353         if (encoded_msg == NULL) {
354                 /* Questionable hack that hopes it'll work next time and we only lose one message */
355                 encoded_alloc = 0;
356         }
357         else {
358                 /* Once we do the encoding we know the exact size */
359                 encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1);
360                 client_write(encoded_msg, encoded_len);
361         }
362
363         free(smr.ser);
364
365         client_write(HKEY("</msg_text>\n"));
366         client_write(HKEY("</message>\n"));
367 }
368
369
370 void migr_export_openids(void) {
371         struct cdbdata *cdboi;
372         long usernum;
373         char url[512];
374
375         cdb_rewind(CDB_EXTAUTH);
376         while (cdboi = cdb_next_item(CDB_EXTAUTH), cdboi != NULL) {
377                 if (cdboi->len > sizeof(long)) {
378                         client_write(HKEY("<openid>\n"));
379                         memcpy(&usernum, cdboi->ptr, sizeof(long));
380                         snprintf(url, sizeof url, "%s", (cdboi->ptr)+sizeof(long) );
381                         client_write(HKEY("<oid_url>"));
382                         xml_strout(url);
383                         client_write(HKEY("</oid_url>\n"));
384                         cprintf("<oid_usernum>%ld</oid_usernum>\n", usernum);
385                         client_write(HKEY("</openid>\n"));
386                 }
387                 cdb_free(cdboi);
388         }
389 }
390
391
392 void migr_export_configs(void) {
393         struct cdbdata *cdbcfg;
394         int keylen = 0;
395         char *key = NULL;
396         char *value = NULL;
397
398         cdb_rewind(CDB_CONFIG);
399         while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
400
401                 keylen = strlen(cdbcfg->ptr);
402                 key = cdbcfg->ptr;
403                 value = cdbcfg->ptr + keylen + 1;
404
405                 client_write("<config key=\"", 13);
406                 xml_strout(key);
407                 client_write("\">", 2);
408                 xml_strout(value);
409                 client_write("</config>\n", 10);
410                 cdb_free(cdbcfg);
411         }
412 }
413
414
415 void migr_export_messages(void) {
416         char buf[SIZ];
417         long msgnum;
418         int count = 0;
419         int progress = 0;
420         int prev_progress = 0;
421         CitContext *Ctx;
422
423         Ctx = CC;
424         migr_global_message_list = fopen(migr_tempfilename1, "r");
425         if (migr_global_message_list != NULL) {
426                 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
427                 while ((Ctx->kill_me == 0) && 
428                        (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
429                         msgnum = atol(buf);
430                         if (msgnum > 0L) {
431                                 migr_export_message(msgnum);
432                                 ++count;
433                         }
434                         progress = (count * 74 / total_msgs) + 25 ;
435                         if ((progress > prev_progress) && (progress < 100)) {
436                                 cprintf("<progress>%d</progress>\n", progress);
437                         }
438                         prev_progress = progress;
439                 }
440                 fclose(migr_global_message_list);
441         }
442         if (Ctx->kill_me == 0) {
443                 syslog(LOG_INFO, "Exported %d messages.", count);
444         }
445         else {
446                 syslog(LOG_ERR, "Export aborted due to client disconnect!");
447         }
448
449         migr_export_message(-1L);       /* This frees the encoding buffer */
450 }
451
452
453 void migr_do_export(void) {
454         CitContext *Ctx;
455
456         Ctx = CC;
457         cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
458         Ctx->dont_term = 1;
459
460         client_write(HKEY("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"));
461         client_write(HKEY("<citadel_migrate_data>\n"));
462         cprintf("<version>%d</version>\n", REV_LEVEL);
463         cprintf("<progress>%d</progress>\n", 0);
464
465         /* export the configuration database */
466         migr_export_configs();
467         cprintf("<progress>%d</progress>\n", 2);
468         
469         if (Ctx->kill_me == 0)  migr_export_users();
470         cprintf("<progress>%d</progress>\n", 7);
471         if (Ctx->kill_me == 0)  migr_export_openids();
472         cprintf("<progress>%d</progress>\n", 12);
473         if (Ctx->kill_me == 0)  migr_export_rooms();
474         cprintf("<progress>%d</progress>\n", 17);
475         if (Ctx->kill_me == 0)  migr_export_floors();
476         cprintf("<progress>%d</progress>\n", 18);
477         if (Ctx->kill_me == 0)  migr_export_visits();
478         cprintf("<progress>%d</progress>\n", 25);
479         if (Ctx->kill_me == 0)  migr_export_messages();
480         client_write(HKEY("</citadel_migrate_data>\n"));
481         cprintf("<progress>%d</progress>\n", 100);
482         client_write(HKEY("000\n"));
483         Ctx->dont_term = 0;
484 }
485
486
487 /******************************************************************************
488  *                              Import code                                   *
489  *    Here's the code that implements the import side.  It's going to end up  *
490  *        being one big loop with lots of global variables.  I don't care.    *
491  * You wouldn't run multiple concurrent imports anyway.  If this offends your *
492  * delicate sensibilities  then go rewrite it in Ruby on Rails or something.  *
493  ******************************************************************************/
494
495
496 int citadel_migrate_data = 0;           /* Are we inside a <citadel_migrate_data> tag pair? */
497 StrBuf *migr_chardata = NULL;
498 StrBuf *migr_MsgData = NULL;
499 struct ctdluser usbuf;
500 struct ctdlroom qrbuf;
501 char openid_url[512];
502 long openid_usernum = 0;
503 char FRname[ROOMNAMELEN];
504 struct floor flbuf;
505 int floornum = 0;
506 visit vbuf;
507 struct MetaData smi;
508 long import_msgnum = 0;
509
510 /*
511  * This callback stores up the data which appears in between tags.
512  */
513 void migr_xml_chardata(void *data, const XML_Char *s, int len)
514 {
515         StrBufAppendBufPlain(migr_chardata, s, len, 0);
516 }
517
518
519 void migr_xml_start(void *data, const char *el, const char **attr) {
520         int i;
521
522         /*** GENERAL STUFF ***/
523
524         /* Reset chardata_len to zero and init buffer */
525         FlushStrBuf(migr_chardata);
526         FlushStrBuf(migr_MsgData);
527
528         if (!strcasecmp(el, "citadel_migrate_data")) {
529                 ++citadel_migrate_data;
530
531                 /* As soon as it looks like the input data is a genuine Citadel XML export,
532                  * whack the existing database on disk to make room for the new one.
533                  */
534                 if (citadel_migrate_data == 1) {
535                         for (i = 0; i < MAXCDB; ++i) {
536                                 cdb_trunc(i);
537                         }
538                 }
539                 return;
540         }
541
542         if (citadel_migrate_data != 1) {
543                 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!", el);
544                 return;
545         }
546
547         /* When we begin receiving XML for one of these record types, clear out the associated
548          * buffer so we don't accidentally carry over any data from a previous record.
549          */
550         if (!strcasecmp(el, "user"))                    memset(&usbuf, 0, sizeof (struct ctdluser));
551         else if (!strcasecmp(el, "openid"))             memset(openid_url, 0, sizeof openid_url);
552         else if (!strcasecmp(el, "room"))               memset(&qrbuf, 0, sizeof (struct ctdlroom));
553         else if (!strcasecmp(el, "room_messages"))      memset(FRname, 0, sizeof FRname);
554         else if (!strcasecmp(el, "floor"))              memset(&flbuf, 0, sizeof (struct floor));
555         else if (!strcasecmp(el, "visit"))              memset(&vbuf, 0, sizeof (visit));
556
557         else if (!strcasecmp(el, "message")) {
558                 memset(&smi, 0, sizeof (struct MetaData));
559                 import_msgnum = 0;
560         }
561         else if (!strcasecmp(el, "config")) {
562                 if (ikey != NULL) {
563                         free(ikey);
564                         ikey = NULL;
565                 }
566                 while (*attr) {
567                         if (!strcasecmp(attr[0], "key")) {
568                                 ikey = strdup(attr[1]);
569                         }
570                         attr += 2;
571                 }
572         }
573
574 }
575
576
577 int migr_userrecord(void *data, const char *el)
578 {
579         if (!strcasecmp(el, "u_version"))                       usbuf.version = atoi(ChrPtr(migr_chardata));
580         else if (!strcasecmp(el, "u_uid"))                      usbuf.uid = atol(ChrPtr(migr_chardata));
581         else if (!strcasecmp(el, "u_password"))                 safestrncpy(usbuf.password, ChrPtr(migr_chardata), sizeof usbuf.password);
582         else if (!strcasecmp(el, "u_flags"))                    usbuf.flags = atoi(ChrPtr(migr_chardata));
583         else if (!strcasecmp(el, "u_timescalled"))              usbuf.timescalled = atol(ChrPtr(migr_chardata));
584         else if (!strcasecmp(el, "u_posted"))                   usbuf.posted = atol(ChrPtr(migr_chardata));
585         else if (!strcasecmp(el, "u_axlevel"))                  usbuf.axlevel = atoi(ChrPtr(migr_chardata));
586         else if (!strcasecmp(el, "u_usernum"))                  usbuf.usernum = atol(ChrPtr(migr_chardata));
587         else if (!strcasecmp(el, "u_lastcall"))                 usbuf.lastcall = atol(ChrPtr(migr_chardata));
588         else if (!strcasecmp(el, "u_USuserpurge"))              usbuf.USuserpurge = atoi(ChrPtr(migr_chardata));
589         else if (!strcasecmp(el, "u_fullname"))                 safestrncpy(usbuf.fullname, ChrPtr(migr_chardata), sizeof usbuf.fullname);
590         else if (!strcasecmp(el, "u_msgnum_bio"))               usbuf.msgnum_bio = atol(ChrPtr(migr_chardata));
591         else if (!strcasecmp(el, "u_msgnum_pic"))               usbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
592         else if (!strcasecmp(el, "u_emailaddrs"))               safestrncpy(usbuf.emailaddrs, ChrPtr(migr_chardata), sizeof usbuf.emailaddrs);
593         else if (!strcasecmp(el, "u_msgnum_inboxrules"))        usbuf.msgnum_inboxrules = atol(ChrPtr(migr_chardata));
594         else if (!strcasecmp(el, "u_lastproc_inboxrules"))      usbuf.lastproc_inboxrules = atol(ChrPtr(migr_chardata));
595         else return 0;
596         return 1;
597 }
598
599
600 int migr_roomrecord(void *data, const char *el)
601 {
602         if (!strcasecmp(el, "QRname"))                          safestrncpy(qrbuf.QRname, ChrPtr(migr_chardata), sizeof qrbuf.QRname);
603         else if (!strcasecmp(el, "QRpasswd"))                   safestrncpy(qrbuf.QRpasswd, ChrPtr(migr_chardata), sizeof qrbuf.QRpasswd);
604         else if (!strcasecmp(el, "QRroomaide"))                 qrbuf.QRroomaide = atol(ChrPtr(migr_chardata));
605         else if (!strcasecmp(el, "QRhighest"))                  qrbuf.QRhighest = atol(ChrPtr(migr_chardata));
606         else if (!strcasecmp(el, "QRgen"))                      qrbuf.QRgen = atol(ChrPtr(migr_chardata));
607         else if (!strcasecmp(el, "QRflags"))                    qrbuf.QRflags = atoi(ChrPtr(migr_chardata));
608         else if (!strcasecmp(el, "QRdirname"))                  safestrncpy(qrbuf.QRdirname, ChrPtr(migr_chardata), sizeof qrbuf.QRdirname);
609         else if (!strcasecmp(el, "QRfloor"))                    qrbuf.QRfloor = atoi(ChrPtr(migr_chardata));
610         else if (!strcasecmp(el, "QRmtime"))                    qrbuf.QRmtime = atol(ChrPtr(migr_chardata));
611         else if (!strcasecmp(el, "QRexpire_mode"))              qrbuf.QRep.expire_mode = atoi(ChrPtr(migr_chardata));
612         else if (!strcasecmp(el, "QRexpire_value"))             qrbuf.QRep.expire_value = atoi(ChrPtr(migr_chardata));
613         else if (!strcasecmp(el, "QRnumber"))                   qrbuf.QRnumber = atol(ChrPtr(migr_chardata));
614         else if (!strcasecmp(el, "QRorder"))                    qrbuf.QRorder = atoi(ChrPtr(migr_chardata));
615         else if (!strcasecmp(el, "QRflags2"))                   qrbuf.QRflags2 = atoi(ChrPtr(migr_chardata));
616         else if (!strcasecmp(el, "QRdefaultview"))              qrbuf.QRdefaultview = atoi(ChrPtr(migr_chardata));
617         else if (!strcasecmp(el, "msgnum_info"))                qrbuf.msgnum_info = atol(ChrPtr(migr_chardata));
618         else if (!strcasecmp(el, "msgnum_pic"))                 qrbuf.msgnum_pic = atol(ChrPtr(migr_chardata));
619         else return 0;
620         return 1;
621 }
622
623
624 int migr_floorrecord(void *data, const char *el)
625 {
626         if (!strcasecmp(el, "f_num"))                           floornum = atoi(ChrPtr(migr_chardata));
627         else if (!strcasecmp(el, "f_flags"))                    flbuf.f_flags = atoi(ChrPtr(migr_chardata));
628         else if (!strcasecmp(el, "f_name"))                     safestrncpy(flbuf.f_name, ChrPtr(migr_chardata), sizeof flbuf.f_name);
629         else if (!strcasecmp(el, "f_ref_count"))                flbuf.f_ref_count = atoi(ChrPtr(migr_chardata));
630         else if (!strcasecmp(el, "f_ep_expire_mode"))           flbuf.f_ep.expire_mode = atoi(ChrPtr(migr_chardata));
631         else if (!strcasecmp(el, "f_ep_expire_value"))          flbuf.f_ep.expire_value = atoi(ChrPtr(migr_chardata));
632         else return 0;
633         return 1;
634 }
635
636
637 int migr_visitrecord(void *data, const char *el)
638 {
639         if (!strcasecmp(el, "v_roomnum"))                       vbuf.v_roomnum = atol(ChrPtr(migr_chardata));
640         else if (!strcasecmp(el, "v_roomgen"))                  vbuf.v_roomgen = atol(ChrPtr(migr_chardata));
641         else if (!strcasecmp(el, "v_usernum"))                  vbuf.v_usernum = atol(ChrPtr(migr_chardata));
642
643         else if (!strcasecmp(el, "v_seen")) {
644                 int is_textual_seen = 0;
645                 int i;
646                 int max = StrLength(migr_chardata);
647
648                 vbuf.v_lastseen = atol(ChrPtr(migr_chardata));
649                 is_textual_seen = 0;
650                 for (i=0; i < max; ++i) 
651                         if (!isdigit(ChrPtr(migr_chardata)[i]))
652                                 is_textual_seen = 1;
653                 if (is_textual_seen)
654                         safestrncpy(vbuf.v_seen, ChrPtr(migr_chardata), sizeof vbuf.v_seen);
655         }
656
657         else if (!strcasecmp(el, "v_answered"))                 safestrncpy(vbuf.v_answered, ChrPtr(migr_chardata), sizeof vbuf.v_answered);
658         else if (!strcasecmp(el, "v_flags"))                    vbuf.v_flags = atoi(ChrPtr(migr_chardata));
659         else if (!strcasecmp(el, "v_view"))                     vbuf.v_view = atoi(ChrPtr(migr_chardata));
660         else return 0;
661         return 1;
662 }
663
664
665 void migr_xml_end(void *data, const char *el)
666 {
667         const char *ptr;
668         int msgcount = 0;
669         long msgnum = 0L;
670         long *msglist = NULL;
671         int msglist_alloc = 0;
672         /*** GENERAL STUFF ***/
673
674         if (!strcasecmp(el, "citadel_migrate_data")) {
675                 --citadel_migrate_data;
676                 return;
677         }
678
679         if (citadel_migrate_data != 1) {
680                 syslog(LOG_ALERT, "Out-of-sequence tag <%s> detected.  Warning: ODD-DATA!", el);
681                 return;
682         }
683
684         // syslog(LOG_DEBUG, "END TAG: <%s> DATA: <%s>\n", el, (migr_chardata_len ? migr_chardata : ""));
685
686         /*** CONFIG ***/
687
688         if (!strcasecmp(el, "config"))
689         {
690                 syslog(LOG_DEBUG, "Imported config key=%s", ikey);
691
692                 if (ikey != NULL) {
693                         CtdlSetConfigStr(ikey, (char *)ChrPtr(migr_chardata));
694                         free(ikey);
695                         ikey = NULL;
696                 }
697                 else {
698                         syslog(LOG_INFO, "Closed a <config> tag but no key name was supplied.");
699                 }
700         }
701
702         /*** USER ***/
703         else if ((!strncasecmp(el, HKEY("u_"))) && 
704                  migr_userrecord(data, el))
705                 ; /* Nothing to do anymore */
706         else if (!strcasecmp(el, "user")) {
707                 CtdlPutUser(&usbuf);
708                 syslog(LOG_INFO, "Imported user: %s", usbuf.fullname);
709         }
710
711         /*** OPENID ***/
712
713         else if (!strcasecmp(el, "oid_url"))                    safestrncpy(openid_url, ChrPtr(migr_chardata), sizeof openid_url);
714         else if (!strcasecmp(el, "oid_usernum"))                openid_usernum = atol(ChrPtr(migr_chardata));
715
716         else if (!strcasecmp(el, "openid")) {                   /* see serv_openid_rp.c for a description of the record format */
717                 char *oid_data;
718                 int oid_data_len;
719                 oid_data_len = sizeof(long) + strlen(openid_url) + 1;
720                 oid_data = malloc(oid_data_len);
721                 memcpy(oid_data, &openid_usernum, sizeof(long));
722                 memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
723                 cdb_store(CDB_EXTAUTH, openid_url, strlen(openid_url), oid_data, oid_data_len);
724                 free(oid_data);
725                 syslog(LOG_INFO, "Imported OpenID: %s (%ld)", openid_url, openid_usernum);
726         }
727
728         /*** ROOM ***/
729         else if ((!strncasecmp(el, HKEY("QR"))) && 
730                  migr_roomrecord(data, el))
731                 ; /* Nothing to do anymore */
732         else if (!strcasecmp(el, "room")) {
733                 CtdlPutRoom(&qrbuf);
734                 syslog(LOG_INFO, "Imported room: %s", qrbuf.QRname);
735         }
736
737         /*** ROOM MESSAGE POINTERS ***/
738
739         else if (!strcasecmp(el, "FRname"))                     safestrncpy(FRname, ChrPtr(migr_chardata), sizeof FRname);
740
741         else if (!strcasecmp(el, "FRmsglist")) {
742                 if (!IsEmptyStr(FRname)) {
743                         msgcount = 0;
744                         msglist_alloc = 1000;
745                         msglist = malloc(sizeof(long) * msglist_alloc);
746
747                         syslog(LOG_DEBUG, "Message list for: %s", FRname);
748
749                         ptr = ChrPtr(migr_chardata);
750                         while (*ptr != 0) {
751                                 while ((*ptr != 0) && (!isdigit(*ptr))) {
752                                         ++ptr;
753                                 }
754                                 if ((*ptr != 0) && (isdigit(*ptr))) {
755                                         msgnum = atol(ptr);
756                                         if (msgnum > 0L) {
757                                                 if (msgcount >= msglist_alloc) {
758                                                         msglist_alloc *= 2;
759                                                         msglist = realloc(msglist, sizeof(long) * msglist_alloc);
760                                                 }
761                                                 msglist[msgcount++] = msgnum;
762                                                 }
763                                         }
764                                         while ((*ptr != 0) && (isdigit(*ptr))) {
765                                                 ++ptr;
766                                         }
767                                 }
768                         }
769                         if (msgcount > 0) {
770                                 CtdlSaveMsgPointersInRoom(FRname, msglist, msgcount, 0, NULL, 1);
771                         }
772                         free(msglist);
773                         msglist = NULL;
774                         msglist_alloc = 0;
775                         syslog(LOG_DEBUG, "Imported %d messages.", msgcount);
776                         if (server_shutting_down) {
777                                 return;
778                 }
779         }
780
781         /*** FLOORS ***/
782         else if ((!strncasecmp(el, HKEY("f_"))) && 
783                  migr_floorrecord(data, el))
784                 ; /* Nothing to do anymore */
785
786         else if (!strcasecmp(el, "floor")) {
787                 CtdlPutFloor(&flbuf, floornum);
788                 syslog(LOG_INFO, "Imported floor #%d (%s)", floornum, flbuf.f_name);
789         }
790
791         /*** VISITS ***/
792         else if ((!strncasecmp(el, HKEY("v_"))) && migr_visitrecord(data, el)) {
793                 ; /* Nothing to do anymore */
794         }
795         else if (!strcasecmp(el, "visit")) {
796                 put_visit(&vbuf);
797                 syslog(LOG_INFO, "Imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
798         }
799
800         /*** MESSAGES ***/
801         
802         else if (!strcasecmp(el, "msg_msgnum"))                 smi.meta_msgnum = import_msgnum = atol(ChrPtr(migr_chardata));
803         else if (!strcasecmp(el, "msg_meta_refcount"))          smi.meta_refcount = atoi(ChrPtr(migr_chardata));
804         else if (!strcasecmp(el, "msg_meta_rfc822_length"))     smi.meta_rfc822_length = atoi(ChrPtr(migr_chardata));
805         else if (!strcasecmp(el, "msg_meta_content_type"))      safestrncpy(smi.meta_content_type, ChrPtr(migr_chardata), sizeof smi.meta_content_type);
806
807         else if (!strcasecmp(el, "msg_text"))
808         {
809                 long rc;
810                 struct CtdlMessage *msg;
811
812                 FlushStrBuf(migr_MsgData);
813                 StrBufDecodeBase64To(migr_chardata, migr_MsgData);
814
815                 msg = CtdlDeserializeMessage(import_msgnum, -1,
816                                              ChrPtr(migr_MsgData), 
817                                              StrLength(migr_MsgData));
818                 if (msg != NULL) {
819                         rc = CtdlSaveThisMessage(msg, import_msgnum, 0);
820                         if (rc == 0) {
821                                 PutMetaData(&smi);
822                         }
823                         CM_Free(msg);
824                 }
825                 else {
826                         rc = -1;
827                 }
828
829                 syslog(LOG_INFO,
830                        "%s message #%ld, size=%d, refcount=%d, bodylength=%ld, content-type: %s",
831                        (rc!= 0)?"failed to import ":"Imported ",
832                        import_msgnum,
833                        StrLength(migr_MsgData),
834                        smi.meta_refcount,
835                        smi.meta_rfc822_length,
836                        smi.meta_content_type
837                 );
838                 memset(&smi, 0, sizeof(smi));
839         }
840
841         /*** MORE GENERAL STUFF ***/
842
843         FlushStrBuf(migr_chardata);
844 }
845
846
847 /*
848  * Import begins here
849  */
850 void migr_do_import(void) {
851         StrBuf *Buf;
852         XML_Parser xp;
853         int Finished = 0;
854         
855         unbuffer_output();
856         migr_chardata = NewStrBufPlain(NULL, SIZ * 20);
857         migr_MsgData = NewStrBufPlain(NULL, SIZ * 20);
858         Buf = NewStrBufPlain(NULL, SIZ);
859         xp = XML_ParserCreate(NULL);
860         if (!xp) {
861                 cprintf("%d Failed to create XML parser instance\n", ERROR+INTERNAL_ERROR);
862                 return;
863         }
864         XML_SetElementHandler(xp, migr_xml_start, migr_xml_end);
865         XML_SetCharacterDataHandler(xp, migr_xml_chardata);
866
867         CC->dont_term = 1;
868
869         cprintf("%d sock it to me\n", SEND_LISTING);
870         unbuffer_output();
871
872         client_set_inbound_buf(SIZ * 10);
873
874         while (!Finished && client_read_random_blob(Buf, -1) >= 0) {
875                 if ((StrLength(Buf) > 4) && !strcmp(ChrPtr(Buf) + StrLength(Buf) - 4, "000\n")) {
876                         Finished = 1;
877                         StrBufCutAt(Buf, StrLength(Buf) - 4, NULL);
878                 }
879                 if (server_shutting_down)
880                         break;  // Should we break or return?
881                 
882                 if (StrLength(Buf) == 0)
883                         continue;
884
885                 XML_Parse(xp, ChrPtr(Buf), StrLength(Buf), 0);
886                 FlushStrBuf(Buf);
887         }
888
889         XML_Parse(xp, "", 0, 1);
890         XML_ParserFree(xp);
891         FreeStrBuf(&Buf);
892         FreeStrBuf(&migr_chardata);
893         FreeStrBuf(&migr_MsgData);
894         rebuild_euid_index();
895         rebuild_usersbynumber();
896         CtdlSetConfigInt("MM_fulltext_wordbreaker", -1);        // Set an invalid wordbreaker to force re-indexing
897         CC->dont_term = 0;
898 }
899
900
901 /******************************************************************************
902  *                         Dispatcher, Common code                            *
903  ******************************************************************************/
904 /*
905  * Dump out the pathnames of directories which can be copied "as is"
906  */
907 void migr_do_listdirs(void) {
908         cprintf("%d Don't forget these:\n", LISTING_FOLLOWS);
909         cprintf("files|%s\n",           ctdl_file_dir);
910         cprintf("messages|%s\n",        ctdl_message_dir);
911         cprintf("keys|%s\n",            ctdl_key_dir);
912         cprintf("000\n");
913 }
914
915
916 /******************************************************************************
917  *                    Repair database integrity                               *
918  ******************************************************************************/
919
920 StrBuf *PlainMessageBuf = NULL;
921 HashList *UsedMessageIDS = NULL;
922
923 int migr_restore_message_metadata(long msgnum, int refcount)
924 {
925         CitContext *CCC = MyContext();
926         struct MetaData smi;
927         struct CtdlMessage *msg;
928         char *mptr = NULL;
929
930         /* We can use a static buffer here because there will never be more than
931          * one of this operation happening at any given time, and it's really best
932          * to just keep it allocated once instead of torturing malloc/free.
933          * Call this function with msgnum "-1" to free the buffer when finished.
934          */
935         static int encoded_alloc = 0;
936         static char *encoded_msg = NULL;
937
938         if (msgnum < 0) {
939                 if ((encoded_alloc == 0) && (encoded_msg != NULL)) {
940                         free(encoded_msg);
941                         encoded_alloc = 0;
942                         encoded_msg = NULL;
943                         // todo FreeStrBuf(&PlainMessageBuf); PlainMessageBuf = NULL;
944                 }
945                 return 0;
946         }
947
948         if (PlainMessageBuf == NULL) {
949                 PlainMessageBuf = NewStrBufPlain(NULL, 10*SIZ);
950         }
951
952         /* Ok, here we go ... */
953
954         msg = CtdlFetchMessage(msgnum, 1);
955         if (msg == NULL) {
956                 return 1;
957         }
958
959         GetMetaData(&smi, msgnum);
960         smi.meta_msgnum = msgnum;
961         smi.meta_refcount = refcount;
962         
963         /* restore the content type from the message body: */
964         mptr = bmstrcasestr(msg->cm_fields[eMesageText], "Content-type:");
965         if (mptr != NULL) {
966                 char *aptr;
967                 safestrncpy(smi.meta_content_type, &mptr[13], sizeof smi.meta_content_type);
968                 striplt(smi.meta_content_type);
969                 aptr = smi.meta_content_type;
970                 while (!IsEmptyStr(aptr)) {
971                         if ((*aptr == ';')
972                             || (*aptr == ' ')
973                             || (*aptr == 13)
974                             || (*aptr == 10)) {
975                                 memset(aptr, 0, sizeof(smi.meta_content_type) - (aptr - smi.meta_content_type));
976                         }
977                         else aptr++;
978                 }
979         }
980
981         CCC->redirect_buffer = PlainMessageBuf;
982         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, QP_EADDR);
983         smi.meta_rfc822_length = StrLength(CCC->redirect_buffer);
984         CCC->redirect_buffer = NULL;
985
986         syslog(LOG_INFO,
987                "Setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s",
988                smi.meta_msgnum,
989                smi.meta_refcount,
990                smi.meta_rfc822_length,
991                smi.meta_content_type
992         );
993
994         PutMetaData(&smi);
995
996         CM_Free(msg);
997
998         return 0;
999 }
1000
1001
1002 void migr_check_room_msg(long msgnum, void *userdata) {
1003         fprintf(migr_global_message_list, "%ld %s\n", msgnum, CC->room.QRname);
1004 }
1005
1006
1007 void migr_check_rooms_backend(struct ctdlroom *buf, void *data) {
1008
1009         /* message list goes inside this tag */
1010
1011         CtdlGetRoom(&CC->room, buf->QRname);
1012         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, migr_check_room_msg, NULL);
1013 }
1014
1015
1016 void RemoveMessagesFromRooms(StrBuf *RoomNameVec, long msgnum) {
1017         struct MetaData smi;
1018         const char *Pos = NULL;
1019         StrBuf *oneRoom = NewStrBuf();
1020
1021         syslog(LOG_INFO, "removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
1022
1023         while (Pos != StrBufNOTNULL){
1024                 StrBufExtract_NextToken(oneRoom, RoomNameVec, &Pos, '|');
1025                 CtdlDeleteMessages(ChrPtr(oneRoom), &msgnum, 1, "");
1026         };
1027         GetMetaData(&smi, msgnum);
1028         AdjRefCount(msgnum, -smi.meta_refcount);
1029 }
1030
1031
1032 void migr_do_restore_meta(void) {
1033         char buf[SIZ];
1034         int failGetMessage;
1035         long msgnum = 0;
1036         int lastnum = 0;
1037         int refcount = 0;
1038         CitContext *Ctx;
1039         char *prn;
1040         StrBuf *RoomNames;
1041         char cmd[SIZ];
1042
1043         migr_global_message_list = fopen(migr_tempfilename1, "w");
1044         if (migr_global_message_list != NULL) {
1045                 CtdlForEachRoom(migr_check_rooms_backend, NULL);
1046                 fclose(migr_global_message_list);
1047         }
1048
1049         /*
1050          * Process the 'global' message list.  (Sort it and remove dups.
1051          * Dups are ok because a message may be in more than one room, but
1052          * this will be handled by exporting the reference count, not by
1053          * exporting the message multiple times.)
1054          */
1055         snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
1056         if (system(cmd) != 0) syslog(LOG_ALERT, "Error %d", errno);
1057
1058         RoomNames = NewStrBuf();
1059         Ctx = CC;
1060         migr_global_message_list = fopen(migr_tempfilename2, "r");
1061         if (migr_global_message_list != NULL) {
1062                 syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
1063                 while ((Ctx->kill_me == 0) && 
1064                        (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
1065                         msgnum = atol(buf);
1066                         if (msgnum == 0L) 
1067                                 continue;
1068                         if (lastnum == 0) {
1069                                 lastnum = msgnum;
1070                         }
1071                         prn = strchr(buf, ' ');
1072                         if (lastnum != msgnum) {
1073                                 failGetMessage = migr_restore_message_metadata(lastnum, refcount);
1074                                 if (failGetMessage) {
1075                                         RemoveMessagesFromRooms(RoomNames, lastnum);
1076                                 }
1077                                 refcount = 1;
1078                                 lastnum = msgnum;
1079                                 if (prn != NULL) {
1080                                         StrBufPlain(RoomNames, prn + 1, -1);
1081                                 }
1082                                 StrBufTrim(RoomNames);
1083                         }
1084                         else {
1085                                 if (prn != NULL) {
1086                                         if (StrLength(RoomNames) > 0) {
1087                                                 StrBufAppendBufPlain(RoomNames, HKEY("|"), 0);
1088                                         }
1089                                         StrBufAppendBufPlain(RoomNames, prn, -1, 1);
1090                                         StrBufTrim(RoomNames);
1091                                 }
1092                                 refcount ++;
1093                         }
1094                         lastnum = msgnum;
1095                 }
1096                 failGetMessage = migr_restore_message_metadata(msgnum, refcount);
1097                 if (failGetMessage) {
1098                         RemoveMessagesFromRooms(RoomNames, lastnum);
1099                 }
1100                 fclose(migr_global_message_list);
1101         }
1102
1103         migr_restore_message_metadata(-1L, -1); /* This frees the encoding buffer */
1104         cprintf("%d system analysis completed", CIT_OK);
1105         Ctx->kill_me = 1;
1106 }
1107
1108
1109 /******************************************************************************
1110  *                         Dispatcher, Common code                            *
1111  ******************************************************************************/
1112 void cmd_migr(char *cmdbuf) {
1113         char cmd[32];
1114         
1115         if (CtdlAccessCheck(ac_internal)) return;
1116         
1117         if (CtdlTrySingleUser()) {
1118                 CtdlDisableHouseKeeping();
1119                 CtdlMakeTempFileName(migr_tempfilename1, sizeof migr_tempfilename1);
1120                 CtdlMakeTempFileName(migr_tempfilename2, sizeof migr_tempfilename2);
1121
1122                 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);
1123                 if (!strcasecmp(cmd, "export")) {
1124                         migr_do_export();
1125                 }
1126                 else if (!strcasecmp(cmd, "import")) {
1127                         migr_do_import();
1128                 }
1129                 else if (!strcasecmp(cmd, "listdirs")) {
1130                         migr_do_listdirs();
1131                 }
1132                 else if (!strcasecmp(cmd, "restoremeta")) {
1133                         migr_do_restore_meta();
1134                 }
1135                 else {
1136                         cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
1137                 }
1138
1139                 unlink(migr_tempfilename1);
1140                 unlink(migr_tempfilename2);
1141
1142                 CtdlEnableHouseKeeping();
1143                 CtdlEndSingleUser();
1144         }
1145         else {
1146                 cprintf("%d The migrator is already running.\n", ERROR + RESOURCE_BUSY);
1147         }
1148 }
1149
1150
1151 /******************************************************************************
1152  *                              Module Hook                                  *
1153  ******************************************************************************/
1154
1155 CTDL_MODULE_INIT(migrate)
1156 {
1157         if (!threading)
1158         {
1159                 CtdlRegisterProtoHook(cmd_migr, "MIGR", "Across-the-wire migration");
1160         }
1161         
1162         /* return our module name for the log */
1163         return "migrate";
1164 }