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